Utils

Account utilities.

  • smarter.apps.account.utils.accounts: an account’s admins, users and profiles, the account number in a url, and the platform-staff check. Its public names are exported here, so that callers import them from smarter.apps.account.utils.

  • smarter.apps.account.utils.hierarchy: the account hierarchy, and the resources that it shares.

  • smarter.apps.account.utils.data_export: Download My Data, a zip file of the manifests of every resource that a user owns.

hierarchy and data_export are imported by their own module paths, not from here. The cached lookups of accounts and users, and smarter_cached_objects, are in smarter.apps.account.caching.

Patch a name where it is looked up: a test that changes how accounts behaves inside patches smarter.apps.account.utils.accounts, not this package.

smarter.apps.account.utils.account_number_from_url(url=None, invalidate=False)[source]

Extract the account number from a Smarter platform URL, using caching for performance.

Parameters:
  • url (str | None) – String. The URL to parse for an account number.

  • invalidate (bool | None) – Boolean, optional. If True, invalidates the cache before fetching.

Return type:

str | None

Returns:

The extracted account number as a string, or None if not found.

Note

The function validates the URL format before extraction.

Warning

If the URL does not contain a valid account number, None is returned.

Tip

Use invalidate=True after updating URLs or account number patterns to ensure cache consistency.

Example usage:

# Extract account number from a URL
account_number = account_number_from_url(url="https://hr.3141-5926-5359.alpha.api.example.com/")

# Result: '3141-5926-5359'

# Invalidate cache before fetching
account_number = account_number_from_url(url="https://hr.3141-5926-5359.alpha.api.example.com/", invalidate=True)
smarter.apps.account.utils.delete_created_admin_user(account_number)[source]

Delete the admin user that get_cached_admin_user_for_account() created for an account, once it has no profile.

That user exists only for its account, and is a superuser. When the account is deleted, its profile goes with it, but the user would stay: a superuser that belongs to nothing.

Parameters:

account_number (str) – The deleted account’s number.

Return type:

bool

Returns:

True if a user was deleted.

smarter.apps.account.utils.get_user_profiles_for_account(account)[source]

Retrieve a list of user profiles associated with a given account.

Parameters:

account (Account) – Account instance. The account for which to retrieve user profiles.

Return type:

list[UserProfile]

Returns:

List of UserProfile instances, or None if no profiles exist.

Important

The account parameter is required. If not provided, an exception is raised.

Warning

If the account has no associated user profiles, None is returned.

Example usage:

# Get all user profiles for an account
profiles = get_user_profiles_for_account(account)
smarter.apps.account.utils.get_users_for_account(account)[source]

Retrieve a list of users associated with a given account.

Parameters:

account (Account) – Account instance. The account for which to retrieve users.

Return type:

list[User]

Returns:

List of User instances.

Important

The account parameter is required. If not provided, an exception is raised.

Warning

If the account has no associated users, an empty list is returned.

Example usage:

# Get all users for an account
users = get_users_for_account(account)
smarter.apps.account.utils.is_platform_staff(user)[source]

Return whether a user is one of the platform’s own staff: an active staff user of the Smarter account.

User.is_staff alone means that a user administers each account they belong to, so many customer accounts’ admins have it, and some of them are superusers too. (An account admin may instead have UserProfile.is_account_admin, which administers one account and is never platform staff.) The platform’s staff are the staff users of the Smarter account (SMARTER_ACCOUNT_NUMBER), which runs the platform. Users who sign up themselves also join the Smarter account, but not as staff.

This is a security check, so it is never cached: a user who stops being staff, or leaves the Smarter account, loses access at once.

Side effects: one database query, for the user’s profile in the Smarter account.

Parameters:

user (object | None) – The user, or anything else, e.g. AnonymousUser or None, which is not staff.

Return type:

bool

Returns:

True if the user is active, staff, and has a profile in the Smarter account.

smarter.apps.account.utils.set_account_admin(user_profile, is_account_admin=True)[source]

Make the profile’s user the administrator of its account, and only of that account.

It grants the role, UserProfile.is_account_admin, and never takes it away: is_account_admin=False leaves the profile as it is. An account’s administrator is never made a superuser, who may read every account.

Parameters:
  • user_profile (UserProfile) – The user’s profile in the account.

  • is_account_admin (bool) – Whether to make them its administrator.

Return type:

None

smarter.apps.account.utils.valid_resource_owners_for_user(user_profile)[source]

Get a list of valid owners for the given user profile.

This function retrieves all user profiles associated with the same account as the provided user profile. These profiles are considered valid owners for ORM resources created by the user.

Parameters:

user_profile (UserProfile | None) – The UserProfile instance representing the user.

Returns:

A list of UserProfile instances that are valid ORM resource owners.

Return type:

list[UserProfile]

See also

  • UserProfile

Example usage:

from smarter.apps.account.models import UserProfile
from smarter.apps.account.utils import valid_resource_owners_for_user

user_profile = UserProfile.objects.get(user__username="exampleuser")
owners = valid_resource_owners_for_user(user_profile)
print("Valid resource owners:", [owner.user.username for owner in owners])