DRF Serializers

Account serializers for Smarter API.

class smarter.apps.account.serializers.AccountContactSerializer(*args, **kwargs)[source]

Bases: SmarterCamelCaseSerializer

Serializer for the AccountContact model in the Smarter API.

This serializer exposes all fields of the AccountContact model, including a minimal account reference. Use it for endpoints that manage or display account contact information.

Parameters:
  • account – Instance of AccountMiniSerializer. Minimal account information.

  • ... – All other fields as defined in the AccountContact model.

Note

All fields are read-only in this serializer.

Example usage:

from smarter.apps.account.serializers import AccountContactSerializer
serializer = AccountContactSerializer(contact_instance)
data = serializer.data

See also

For full account details, use AccountSerializer.

get_fields()[source]

Return the serializer’s fields, all read-only.

Returns:

The fields.

class smarter.apps.account.serializers.AccountMiniSerializer(*args, **kwargs)[source]

Bases: SmarterCamelCaseSerializer

Serializer for a minimal representation of the Account model in the Smarter API.

This serializer is intended for scenarios where only the account number is required, such as embedding account references in related resources or optimizing API payload size.

Parameters:

account_number – String. The unique identifier for the account.

Note

Only the account_number field is included in serialization.

Tip

Use this serializer for nested relationships or summary views.

Example usage:

from smarter.apps.account.serializers import AccountMiniSerializer
serializer = AccountMiniSerializer(account_instance)
data = serializer.data

See also

For full account details, use AccountSerializer.

class smarter.apps.account.serializers.AccountSerializer(*args, **kwargs)[source]

Bases: MetaDataModelSerializer

Serializer for the Account model in the Smarter API.

This serializer provides full access to all fields of the Account model, making it suitable for detailed account data retrieval and updates via API endpoints.

Parameters:
  • account_number – String. The unique identifier for the account.

  • name – String. The account name.

  • ... – Other fields as defined in the Account model.

Important

All fields in the Account model are included in serialization and deserialization.

Example usage:

from smarter.apps.account.serializers import AccountSerializer
serializer = AccountSerializer(account_instance)
data = serializer.data

See also

For lightweight account representations, use AccountMiniSerializer.

class smarter.apps.account.serializers.BudgetSerializer(*args, **kwargs)[source]

Bases: MetaDataModelSerializer

Serializer for the Budget model, with the number of resources it is attached to, and of those it locks.

Example usage:

from smarter.apps.account.serializers import BudgetSerializer
data = BudgetSerializer(budget).data
get_locked(obj)[source]

The number of the Budget’s resources that are locked.

Parameters:

obj (Budget) – The Budget.

Return type:

int

Returns:

The number of its active constraints that have a lock.

get_manifest_url(obj)[source]

The URL of the Budget’s detail view, which renders its manifest.

Return type:

str

get_resources(obj)[source]

The number of resources that the Budget is attached to.

Parameters:

obj (Budget) – The Budget.

Return type:

int

Returns:

The number of its active constraints.

class smarter.apps.account.serializers.MetaDataWithOwnershipModelSerializer(*args, **kwargs)[source]

Bases: MetaDataModelSerializer

Serializer for models that extend MetaDataWithOwnershipModel, adding an ‘account’ field.

It also adds can_delete, which is True if the authenticated user may delete the resource now: they have ownership permission for it, and no other resource depends on it. Subclasses set Meta.kind to the resource’s SAM kind, which can_delete needs to find its broker.

It also adds detail_url, the url of the resource’s detail (smarter.apps.dashboard.views.views.api.resource_detail.ResourceDetailView): its manifest, the resources that it uses, and those that use it, which a web console list shows when one of its rows is chosen. Every subclass includes both, whatever its Meta.fields.

get_can_delete(obj)[source]

Return True if the authenticated user may delete the resource now.

The user can delete the object if a.) they have permission and b.) no dependencies existing on the object based on the SAMBroker enforcement rules.

The user must have ownership permission for the resource, as in MetaDataWithOwnershipModelManager.with_ownership_permission_for(), and no other resource may depend on it, as in AbstractBroker.has_dependents().

Parameters:

obj (MetaDataWithOwnershipModel) – The resource.

Returns:

Whether the user may delete the resource. False if the serializer has no request in its context, or does not know the resource’s kind. See user_can_delete().

Return type:

bool

get_detail_url(obj)[source]

Return the url of the resource’s detail, from its kind and record locator.

Parameters:

obj (MetaDataWithOwnershipModel) – The resource.

Returns:

The url, e.g. /api/web/dashboard/resource/Guardrail/guardrail-rMTAwx/, or None if the serializer does not know the resource’s kind, or the resource has no record locator.

Return type:

str | None

get_field_names(declared_fields, info)[source]

Return the field names to serialize, always including can_delete and detail_url.

DRF omits an inherited declared field from a subclass whose Meta.fields is a list that does not name it, so they are added here for every subclass.

Return type:

List[str]

kind_of(obj)[source]

Return the resource’s SAM kind: the serializer’s Meta.kind, or the resource’s own kind, as a plugin has.

Parameters:

obj (MetaDataWithOwnershipModel) – The resource.

Returns:

The kind, e.g. Guardrail, or None if neither says.

Return type:

str | None

class smarter.apps.account.serializers.UserMiniSerializer(*args, **kwargs)[source]

Bases: SmarterCamelCaseSerializer

Serializer for a minimal representation of the User model in the Smarter API.

This serializer is designed for use cases where only essential user information is required, such as listing users or embedding user data in related resources.

Parameters:
  • username – String. The user’s username.

  • email – String. The user’s email address.

Note

All fields are read-only and included in the serialized output.

Tip

Use this serializer for lightweight API responses to reduce payload size.

Example usage:

from smarter.apps.account.serializers import UserMiniSerializer
serializer = UserMiniSerializer(user_instance)
data = serializer.data

See also

For full user details, use UserSerializer.

class smarter.apps.account.serializers.UserProfileSerializer(*args, **kwargs)[source]

Bases: SmarterCamelCaseSerializer

Serializer for the UserProfile model in the Smarter API.

This serializer provides a minimal representation of a user profile, including nested user and account data. Use it for endpoints where a summary of user and account relationships is required.

Parameters:
  • user – Instance of UserMiniSerializer. Minimal user information.

  • account – Instance of AccountMiniSerializer. Minimal account information.

Note

Only the user and account fields are included in serialization.

Example usage:

from smarter.apps.account.serializers import UserProfileSerializer
serializer = UserProfileSerializer(profile_instance)
data = serializer.data

See also

For more detailed user or account data, use UserSerializer or AccountSerializer.

class smarter.apps.account.serializers.UserSerializer(*args, **kwargs)[source]

Bases: MetaDataModelSerializer

Serializer for the User model in the Smarter API.

This serializer converts Django User model instances to and from JSON using camelCase field names, making it suitable for API responses and requests.

Parameters:
  • id – Integer. The unique identifier for the user.

  • username – String. The user’s username.

  • first_name – String. The user’s first name.

  • last_name – String. The user’s last name.

  • email – String. The user’s email address.

  • is_staff – Boolean. Indicates if the user has staff privileges.

  • is_superuser – Boolean. Indicates if the user has superuser privileges.

Note

All fields listed in fields are included in serialization. Add more fields to the list if needed.

Example usage:

from smarter.apps.account.serializers import UserSerializer
serializer = UserSerializer(user_instance)
data = serializer.data
smarter.apps.account.serializers.user_can_delete(obj, kind, user, owned_pks=None)[source]

Return True if a user may delete a resource now.

The user must have ownership permission for the resource, as in MetaDataWithOwnershipModelManager.with_ownership_permission_for(), and no other resource may depend on it, as in AbstractBroker.has_dependents(). It is the canDelete of the resource’s api responses, e.g. MetaDataWithOwnershipModelSerializer.get_can_delete().

Note

A resource list calls this for every row that it shows, so it builds no broker: it asks the broker class whether any resource depends on this one, with a few EXISTS queries.

Parameters:
  • obj (MetaDataWithOwnershipModel) – The resource.

  • kind (str | None) – The resource’s SAM kind, e.g. LLMClient, which finds its broker.

  • user (object) – The user, e.g. the request’s user. Anything that is not a User, e.g. an anonymous user or None, may delete nothing.

  • owned_pks (Set | None) – The primary keys of the resources that the user has ownership permission for, among those of a list’s page, so that a page asks once rather than once per row. None asks the database for this resource alone.

Returns:

Whether the user may delete the resource.

Return type:

bool