UserProfile Model

Account UserProfile model.

class smarter.apps.account.models.user_profile.SmarterBaseModelManager(*args, **kwargs)[source]

Bases: Manager

Custom manager for MetaDataWithOwnershipModel that returns a SmarterBaseQuerySetWithPermissions to enable permission-based filtering by user_profile.

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

Returns a SmarterBaseQuerySetWithPermissions with alias applied.

Return type:

SmarterBaseQuerySetWithPermissions

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

Returns a SmarterBaseQuerySetWithPermissions with annotate applied.

Return type:

SmarterBaseQuerySetWithPermissions

complex_filter(filter_obj)[source]

Returns a SmarterBaseQuerySetWithPermissions with the applied complex filter.

Return type:

SmarterBaseQuerySetWithPermissions

difference(*other_qs)[source]

Returns a SmarterBaseQuerySetWithPermissions representing the difference of querysets.

Return type:

SmarterBaseQuerySetWithPermissions

distinct(*field_names)[source]

Returns a SmarterBaseQuerySetWithPermissions with distinct applied.

Return type:

SmarterBaseQuerySetWithPermissions

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

Returns a SmarterBaseQuerySetWithPermissions with the applied exclusion.

Return type:

SmarterBaseQuerySetWithPermissions

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

Returns a SmarterBaseQuerySetWithPermissions with the applied filter.

Return type:

SmarterBaseQuerySetWithPermissions

get_queryset()[source]

Returns a SmarterBaseQuerySetWithPermissions for the model.

Return type:

SmarterBaseQuerySetWithPermissions

intersection(*other_qs)[source]

Returns a SmarterBaseQuerySetWithPermissions representing the intersection of querysets.

Return type:

SmarterBaseQuerySetWithPermissions

none()[source]

Returns an empty SmarterBaseQuerySetWithPermissions.

Return type:

SmarterBaseQuerySetWithPermissions

order_by(*field_names)[source]

Returns a SmarterBaseQuerySetWithPermissions with order_by applied.

Return type:

SmarterBaseQuerySetWithPermissions

Overloads:
  • self, clear (None) → SmarterBaseQuerySetWithPermissions[_GenericTypeVar]

  • self, lookups (str | Prefetch) → SmarterBaseQuerySetWithPermissions[_GenericTypeVar]

Returns a SmarterBaseQuerySetWithPermissions with prefetch_related applied.

select_for_update(**kwargs)[source]

Returns a SmarterBaseQuerySetWithPermissions with select_for_update applied.

Return type:

SmarterBaseQuerySetWithPermissions

Overloads:
  • self, clear (None) → SmarterBaseQuerySetWithPermissions[_GenericTypeVar]

  • self, fields (str) → SmarterBaseQuerySetWithPermissions[_GenericTypeVar]

Returns a SmarterBaseQuerySetWithPermissions with select_related applied.

union(*other_qs, all=False)[source]

Returns a SmarterBaseQuerySetWithPermissions representing the union of querysets.

Return type:

SmarterBaseQuerySetWithPermissions

with_ownership_permission_for(user)[source]

Returns a queryset of resources that the authenticated user in the given request has full management (ownership) permission for.

Permission logic:

  • If the user is not authenticated, they have no access.

  • If the user is a superuser, they have ownership permission for all resources.

  • If the user is a staff user, they have ownership permission for resources that are:
    • Owned by their UserProfile, OR

    • Owned by any UserProfile within their Account.

  • If the user is a regular authenticated user, they have ownership permission only for resources they own.

Parameters:

user (User) – django.contrib.auth.models.User The user to check.

Return type:

SmarterBaseQuerySetWithPermissions

Returns:

django.db.models.QuerySet A queryset of this resource if the user has permission to fully manage it, or an empty queryset if not.

with_read_permission_for(user)[source]

A custom Smarter pipeline for filtering any MetaDataWithOwnership queryset based on the Smarter permissions scheme for the authenticated user in the given request.

Returns a queryset of the resource if the user has permission to read it, or an empty queryset if not.

Permission logic:

  • If the user is not authenticated, they have no access.

  • If the user is a superuser, they have access to all resources.

  • If the user is a regular authenticated user, they have access to resources that are:
    • Owned by their UserProfile, OR

    • Owned by their Account admin UserProfile, OR

    • Owned by the Smarter admin UserProfile.

Parameters:
  • user (User) – django.contrib.auth.models.User The user to check.

  • queryset – Optional[django.db.models.QuerySet] An optional queryset to filter. If not provided, the method will default to filtering all instances

Return type:

SmarterBaseQuerySetWithPermissions

Returns:

django.db.models.QuerySet A queryset of this resource if the user has permission to read it, or an empty queryset if not.

class smarter.apps.account.models.user_profile.SmarterBaseQuerySetWithPermissions(model=None, query=None, using=None, hints=None)[source]

Bases: QuerySet

Custom queryset for permission-based resource filtering by user profile.

This queryset adds permission-aware filtering for resources owned by a specific user profile.

See also

with_ownership_permission_for(user)[source]

Returns a queryset of resources that the authenticated user in the given request has full management (ownership) permission for.

Only users with staff or superuser status are permitted to manage resources.

Parameters:

user (User) – django.contrib.auth.models.User The user to check.

Return type:

SmarterBaseQuerySetWithPermissions

Returns:

django.db.models.QuerySet A queryset of this resource if the user has permission to fully manage it, or an empty queryset if not.

with_read_permission_for(user)[source]

A pipeline for filtering a queryset of this resource based on the permissions of the authenticated user in the given request.

Return a queryset of this resource if the user has permission to read it, or an empty queryset if not.

Parameters:
  • user (User) – django.contrib.auth.models.User The user to check.

  • queryset – Optional[django.db.models.QuerySet] An optional queryset to filter. If not provided, the method will default to filtering all instances

Return type:

SmarterBaseQuerySetWithPermissions

Returns:

django.db.models.QuerySet A queryset of this resource if the user has permission to read it, or an empty queryset if not.

class smarter.apps.account.models.user_profile.UserProfile(*args, **kwargs)[source]

Bases: MetaDataModel

UserProfile model for associating Django users with Smarter accounts.

Establishes a link between a Django User and an Account, enabling centralized management of billing, identity, and resource ownership.

Parameters:
  • user – ForeignKey to django.contrib.auth.models.User. The user associated with this profile.

  • account – ForeignKey to Account. The related Smarter account.

  • is_test – Boolean. Indicates if this profile is for testing purposes.

Important

The combination of user and account must be unique. Duplicate profiles for the same user and account are not allowed.

Example usage:

from smarter.apps.account.models import UserProfile
profile = UserProfile.objects.create(user=user, account=account)
profile.add_to_account_contacts(is_primary=True)
Parameters:
  • id (Unknown) – Primary key: ID

  • created_at (Unknown) – Created at

  • updated_at (Unknown) – Updated at

  • name (Unknown) – Name. Name in camelCase, e.g., ‘apiKey’, no special characters.

  • description (Unknown) – Description. A brief description of this resource. Be verbose, but not too verbose.

  • version (Unknown) – Version. Semantic version in the format MAJOR.MINOR.PATCH, e.g., ‘1.0.0’.

  • annotations (Unknown) – Annotations. Key-value pairs for annotating this resource.

  • profile_image_url (Unknown) – Profile image url. URL to the user’s profile image, provided via oauth.

Relationship fields:

Parameters:
  • tags (Unknown) – Tags. Tags for categorizing and organizing this resource. (related name: userprofile)

  • tagged_items (Unknown) – Tagged items (related name: +)

Reverse relationships:

Parameters:
  • smarterauthtoken (Unknown) – All API Keys of this user profile (related name of user_profile)

  • charge (Unknown) – All charge of this user profile (related name of user_profile)

  • llmclient (Unknown) – All LLMClients of this user profile (related name of user_profile)

  • connectionbase (Unknown) – All connection bases of this user profile (related name of user_profile)

  • pluginmeta (Unknown) – All Plugins of this user profile (related name of user_profile)

  • prompt (Unknown) – All Chats of this user profile (related name of user_profile)

  • provider (Unknown) – All Providers of this user profile (related name of user_profile)

  • secrets (Unknown) – All secrets of this user profile (related name of user_profile)

  • vectorestoremeta (Unknown) – All Vectorstore Metadata of this user profile (related name of user_profile)

exception DoesNotExist

Bases: ObjectDoesNotExist

exception MultipleObjectsReturned

Bases: MultipleObjectsReturned

exception NotUpdated

Bases: ObjectNotUpdated, DatabaseError

account

ForeignKey to Account

Account (related name: user_profiles)

Type:

Type

account_id

Internal field, use account instead.

add_to_account_contacts(is_primary=False)[source]

Add the user to the account’s contact list.

Creates or updates an AccountContact entry for the user, ensuring their email and name are registered with the account. Optionally sets the contact as primary.

Parameters:

is_primary (bool) – Boolean. If True, marks the contact as the primary contact for the account. Defaults to False.

Important

Ensures every user associated with an account is also listed as a contact, supporting notifications and account management.

Example usage:

profile.add_to_account_contacts(is_primary=True)

See also

AccountContact

classmethod admin_for_account(account)[source]

Return the designated user for the given account.

This method finds the first staff user associated with the account. If no staff user exists, it returns the first available user. If the account has no users, an admin user is created and returned.

Parameters:

account (Account) – Instance of Account. The account for which to find the designated user.

Return type:

User

Returns:

django.contrib.auth.models.User The designated user for the account.

Attention

If no staff or regular users exist for the account, an admin user is automatically created. You must set the password manually.

Error

Logs an error if no admin or user is found for the account.

Example usage:

user = UserProfile.admin_for_account(account)

See also

UserProfile

annotations

JSONField

Annotations. Key-value pairs for annotating this resource.

Type:

Type

property cached_account: Account

Retrieve the associated Account instance with caching. This significantly reduces the number of database queries when accessing the account from the user profile.

Returns:

Account The associated Account instance.

Example usage:

account = user_profile.cached_account
if account:
    print(account.company_name)
property cached_user: User

Retrieve the associated User instance with caching. This significantly reduces the number of database queries when accessing the user from the user profile.

Returns:

User The associated User instance.

Example usage:

user = profile.cached_user
if user:
    print(user.email)
charge

Reverse ForeignKey from Charge

All charge of this user profile (related name of user_profile)

Type:

Type

connectionbase_set

Reverse ForeignKey from ConnectionBase

All connection bases of this user profile (related name of user_profile)

Type:

Type

created_at

DateTimeField

Created at

Timestamp indicating when the model instance was created.

This field is automatically set to the current date and time when the instance is first created. It is indexed in the database for efficient querying.

Type:

Type

description

TextField

Description. A brief description of this resource. Be verbose, but not too verbose.

Type:

Type

classmethod get_cached_object(*args, invalidate=False, pk=None, name=None, user=None, username=None, account=None, **kwargs)[source]

Retrieve a model instance by primary key or name, using caching to optimize performance. This method is selectively overridden in models that inherit from MetaDataModel to provide class-specific function parameters.

Example usage:

# Retrieve by primary key
instance = MyModel.get_cached_object(pk=1)
# Retrieve by name
instance = MyModel.get_cached_object(name="exampleName")
Parameters:
  • pk (Optional[int]) – The primary key of the model instance to retrieve.

  • name (Optional[str]) – The name of the model instance to retrieve.

Returns:

The model instance if found, otherwise None.

Return type:

UserProfile

classmethod get_cached_objects(invalidate=False, user=None, **kwargs)[source]

Retrieve a queryset of UserProfile instances associated with the given user, using caching to optimize performance.

Parameters:
  • invalidate (Optional[bool]) – Boolean. If True, invalidates the cache for the user’s profiles before retrieving.

  • user (Optional[User]) – Optional[User]. If provided, retrieves profiles associated with this user. If not provided, retrieves all profiles.

Returns:

QuerySet[UserProfile]. A queryset of UserProfile instances associated with the given user, or all profiles if no user is specified.

Return type:

QuerySet

id

BigAutoField

Primary key: ID

Type:

Type

is_test

BooleanField

Is test. Indicates if this profile is used for unit testing purposes.

Type:

Type

llmclient_set

Reverse ForeignKey from LLMClient

All LLMClients of this user profile (related name of user_profile)

Type:

Type

name

CharField

Name. Name in camelCase, e.g., ‘apiKey’, no special characters.

Type:

Type

objects: SmarterBaseModelManager = <smarter.apps.account.models.user_profile.SmarterBaseModelManager object>
pluginmeta_set

Reverse ForeignKey from PluginMeta

All Plugins of this user profile (related name of user_profile)

Type:

Type

profile_image_url

URLField

Profile image url. URL to the user’s profile image, provided via oauth.

Type:

Type

prompt_set

Reverse ForeignKey from Prompt

All Chats of this user profile (related name of user_profile)

Type:

Type

provider_set

Reverse ForeignKey from Provider

All Providers of this user profile (related name of user_profile)

Type:

Type

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

Save the UserProfile instance and ensure account contacts are updated.

This method validates that both user and account are set, saves the profile, and, if newly created, adds the user to the account’s contact list. It also emits a signal for new user creation.

Parameters:
  • args – Positional arguments passed to the parent save method.

  • kwargs – Keyword arguments passed to the parent save method.

Note

On first save, ensures at least one primary contact exists for the account.

Example usage:

profile.save()
secrets

Reverse ForeignKey from Secret

All secrets of this user profile (related name of user_profile)

Type:

Type

smarterauthtoken_set

Reverse ForeignKey from SmarterAuthToken

All API Keys of this user profile (related name of user_profile)

Type:

Type

tagged_items

Reverse GenericRelation from UserProfile

All + of this tagged item (related name of tagged_items)

Type:

Type

tags = <taggit.managers._TaggableManager object>
updated_at

DateTimeField

Updated at

Timestamp indicating when the model instance was last updated.

This field is automatically updated to the current date and time whenever the instance is saved. It is indexed in the database for efficient querying.

Type:

Type

user

ForeignKey to User

User (related name: user_profile)

Type:

Type

user_id

Internal field, use user instead.

vectorestoremeta_set

Reverse ForeignKey from VectorestoreMeta

All Vectorstore Metadata of this user profile (related name of user_profile)

Type:

Type

version

CharField

Version. Semantic version in the format MAJOR.MINOR.PATCH, e.g., ‘1.0.0’.

Type:

Type