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[Budget, Budget]

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]

Budget.

Type:

The Django model of the Budget’s metadata

property ORMModelClass: Type[Budget]

Budget.

Type:

The Django model of the Budget

property SerializerClass: Type[ModelSerializer]

The Django ORM model serializer class for the Budget.

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

Initialize the broker from a manifest, a manifest file, or the Django ORM.

A manifest takes precedence over a file path. With neither, the broker is initialized from the ORM instance named name and owned by the user profile, if there is one.

Parameters:
  • request – The request, or None. It may also be passed positionally in args.

  • args – Positional arguments, among which the request, user, account, user profile and manifest are found by their types.

  • name – The resource’s name.

  • kind – The resource’s SAM kind, e.g. Guardrail.

  • api_version – The manifest’s api version.

  • manifest – The manifest, as a Pydantic model or as a dict, e.g. from parse_manifest().

  • file_path – The path of a manifest file, read when no manifest is passed.

  • url – Accepted, for compatibility, but not used.

  • kwargs – user, account and user_profile, and the rest, which are passed on to SmarterRequestMixin.

Raises:
  • SmarterValueError – If the kind, name or manifest is invalid.

  • SAMBrokerError – If the manifest’s kind is not the broker’s kind.

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

dependencies()[source]

Return brokers for the resources that depend on this Budget.

No resource refers to a Budget. A Budget refers to the resources that it constrains, and deleting it detaches it from them.

Returns:

An empty list.

Return type:

List[AbstractBroker | SAMDependencyReference]

classmethod dependents(instance)[source]

Return the resources that depend on a Budget.

No resource refers to a Budget. A Budget refers to the resources that it constrains, and deleting it detaches it from them.

Parameters:

instance (Budget) – The Budget.

Returns:

An empty list.

Return type:

List[Tuple[str | None, Iterable[Model]]]

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

Refuse to deploy: a Budget is not deployable.

Parameters:

request (HttpRequest) – The request.

Raises:

SAMBrokerErrorNotImplemented – always.

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:

dict[str, Any] | None

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

Whether the broker’s user is a superuser.

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]

Return the Budget’s logs.

A Budget has none.

Parameters:

request (HttpRequest) – The request.

Return type:

SmarterJournaledJsonResponse

Returns:

A successful response with empty data.

property manifest: SAMBudget | None

The Budget manifest, as a Pydantic model, from the client’s manifest data.

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

The broker’s Budget, or None if it does not exist yet.

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]

Refuse to prompt: a Budget is not an LLM resource.

Parameters:

request (HttpRequest) – The request.

Raises:

SAMBrokerErrorNotImplemented – always.

Return type:

SmarterJournaledJsonResponse

property ready: bool

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

classmethod references(instance)[source]

Return the resources that a Budget uses: none.

A Budget limits the resources of its spec.resources, but does not use them.

Parameters:

instance (Budget) – The Budget.

Returns:

An empty list.

Return type:

List[Tuple[str | None, Iterable[Model]]]

resolve_account(account_number)[source]

Return the Account of a manifest’s accountNumber.

Parameters:

account_number (str | None) – The account number, or None for the broker’s own Account.

Return type:

Account

Returns:

The Account.

Raises:

SAMBudgetBrokerError – when no account number is given and the broker has no Account, or when no Account has the account number.

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]

Refuse to undeploy: a Budget is not deployable.

Parameters:

request (HttpRequest) – The request.

Raises:

SAMBrokerErrorNotImplemented – always.

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

The label of this error, used in its string form.