Budget

Smarter API Budget Manifest handler.

A Budget is managed by superusers: only they may apply or delete one. Anyone may get and describe the budgets that are attached to a resource that they may see.

class smarter.apps.account.manifest.brokers.budget.SAMBudgetBroker(*args, **kwargs)[source]

Bases: AbstractBroker

Broker for SAM Budget manifests.

The broker converts between Budget manifests and the Budget Django ORM model and its ResourceConstraint attachments, and implements the smarter CLI commands for Budgets: apply, describe, get, delete and example_manifest. spec.resources is declarative: applying a manifest attaches the budget to the resources that it lists, and detaches it from the others.

property ORMMetaModelClass: Type[Budget]

Return the Django ORM meta model class for the broker.

Returns:

The Django ORM meta model class definition for the broker.

Return type:

Type[MetaDataWithOwnershipModel]

property ORMModelClass: Type[Budget]

Return the Django ORM model class for the broker.

Returns:

The Django ORM model class definition for the broker.

Return type:

Type[MetaDataWithOwnershipModel]

property SerializerClass: Type[ModelSerializer]

The Django ORM model serializer class for the Budget.

__init__(*args, **kwargs)[source]
apply(request, *args, **kwargs)[source]

Create or update the Budget from the manifest, then attach it to spec.resources, and detach it from the others.

Only superusers may apply a Budget.

Return type:

SmarterJournaledJsonResponse

property budget: Budget | None

The Budget with the broker’s name, if it exists.

It is never created here: apply() does that.

delete(request, *args, **kwargs)[source]

Delete the Budget, which detaches it from its resources and removes their locks.

Superusers only.

Return type:

SmarterJournaledJsonResponse

deploy(request, *args, **kwargs)[source]

Deploy a resource.

Parameters:
  • request (HttpRequest) – The HTTP request object.

  • args – Additional positional arguments.

  • kwargs – Additional keyword arguments.

Returns:

A SmarterJournaledJsonResponse containing the result of the deploy operation.

Return type:

SmarterJournaledJsonResponse

describe(request, *args, **kwargs)[source]

Return the Budget as a manifest, with the budget versus the actual spending of each resource.

Return type:

SmarterJournaledJsonResponse

django_orm_to_manifest_dict()[source]

Convert the Budget into a manifest dict, with its status.

Return type:

Optional[dict]

example_manifest(request, *args, **kwargs)[source]

Return an example Budget manifest: a monthly allowance for a student.

Return type:

SmarterJournaledJsonResponse

property formatted_class_name: str

The class name, for logging.

get(request, *args, **kwargs)[source]

Return the Budgets that the user may see, optionally filtered by name.

Return type:

SmarterJournaledJsonResponse

property is_superuser: bool
is_visible_budget(budget)[source]

Superusers see every budget.

Others see those attached to a resource that they may see.

Return type:

bool

property kind: str

Budget.

Type:

The manifest kind

logs(request, *args, **kwargs)[source]

Get logs for a resource.

Parameters:
  • request (HttpRequest) – The HTTP request object.

  • args – Additional positional arguments.

  • kwargs – Additional keyword arguments.

Returns:

A SmarterJournaledJsonResponse containing the logs for the resource.

Return type:

SmarterJournaledJsonResponse

property manifest: SAMBudget | None

The Budget manifest, as a Pydantic model, from the manifest loader.

manifest_to_django_orm()[source]

Convert the manifest into a dict of Django ORM Budget fields.

Raises:

SAMBrokerErrorNotReady – If the manifest is not loaded.

Return type:

dict[str, Any]

property orm_instance: Budget | None

Return the Django ORM model instance for the broker.

There are multiple strategies to retrieve the ORM instance:

  1. If the instance is already cached in self._orm_instance, return it.

  2. If the broker is not ready or the name is not set, log a warning and return None.

  3. Attempt to retrieve the ORM instance using the user_profile and name. If not found, attempt to retrieve using the admin user_profile for the account. If still not found, attempt to retrieve using the Smarter platform admin user_profile.

  4. Cache the retrieved instance for future access.

Returns:

The Django ORM model instance for the broker.

Return type:

Optional[MetaDataWithOwnershipModel]

property orm_meta_instance: Budget | None

The Budget.

Budgets are not owned, so the base class’s lookup by owner does not apply.

prompt(request, *args, **kwargs)[source]

Invoke a prompt operation.

This abstract method should be implemented by subclasses to provide prompt-based interactions with the broker resource.

Parameters:
  • request (HttpRequest) – The HTTP request object.

  • args – Additional positional arguments.

  • kwargs – Additional keyword arguments.

Returns:

A SmarterJournaledJsonResponse containing the prompt response.

Return type:

SmarterJournaledJsonResponse

property ready: bool

A broker is ready if it has a manifest, or an account.

resolve_account(account_number)[source]
Return type:

Account

resolve_locator(resource)[source]

The record locator of a spec.resources entry.

Return type:

str

status_for(budget)[source]

The budget versus the actual spending of each resource that the user may see.

Return type:

list[dict[str, Any]]

undeploy(request, *args, **kwargs)[source]

Undeploy a resource.

Parameters:
  • request (HttpRequest) – The HTTP request object.

  • args – Additional positional arguments.

  • kwargs – Additional keyword arguments.

Returns:

A SmarterJournaledJsonResponse containing the result of the undeploy operation.

Return type:

SmarterJournaledJsonResponse

exception smarter.apps.account.manifest.brokers.budget.SAMBudgetBrokerError(message=None, thing=None, command=None, stack_trace=None)[source]

Bases: SAMBrokerError

Base exception for Smarter API Budget Broker handling.

property get_formatted_err_message