Django Admin
Custom Django admin site and model admin classes for the dashboard app.
This module rebuilds the Django admin site with fine-grained, role-based
access control. Instead of using Django’s default AdminSite, a
RestrictedAdminSite instance is registered. Django lets only active
is_staff users into the site, so it is a tool for the platform’s
superusers and for each account’s staff. Everyone else uses the web console.
The site enforces these tiers across all registered models:
Superuser: every row of every model, with add, change and delete.
Staff (account admin): the rows of their own account, of the models registered with
SmarterCustomerModelAdminorSmarterStaffOnlyModelAdmin. They may change and delete the rows they have ownership permission for, and add none.Anyone else: no access.
Module-level helpers
smarter_admin_user()Returns the request’s authenticated
User, or None.smarter_is_staff()Returns
Trueif the requesting user is a staff member or superuser.smarter_has_ud_permission()Returns
Trueif the requesting user may update or delete the given object, based on ownership and account association.smarter_scoped_queryset()Limits a queryset of owned resources to those the user has ownership permission for.
Model admin classes
SmarterModelAdminMixinWhat every Smarter model admin does: deletes a SAM resource with its broker, exports SAM resources as manifests, and keeps its lists and forms fast on large tables.
SmarterCustomerModelAdminShows staff the rows of their own account; change and delete need ownership permission.
SmarterStaffOnlyModelAdminThe same, and checks that the user is staff at every step.
SmarterSuperUserOnlyModelAdminRestricts all operations to superusers only.
SmarterAutoModelAdminandsmarter_auto_model_admin()A superuser-only admin whose list, filters and search are built from the model’s fields, for the models that have no admin of their own.
Admin site
RestrictedAdminSiteCustom
AdminSitewhose header names the current user’s role and the version.smarter_restricted_admin_siteThe singleton
RestrictedAdminSiteinstance used throughout the project (name="restricted_admin_site").
Registered models
EmailContactList: registered withEmailContactListAdmin(superuser-only).Django’s
LogEntry, the admin’s own audit log: registered withLogEntryAdmin(superuser-only, read-only).
- smarter.apps.dashboard.admin.AUTO_LIST_DISPLAY_LIMIT = 8
The most columns that
SmarterAutoModelAdminlists.
- smarter.apps.dashboard.admin.DELETE_ERRORS_ATTRIBUTE = 'smarter_admin_delete_errors'
The request attribute in which
SmarterModelAdminMixin.delete_model()keeps why a delete failed.
- class smarter.apps.dashboard.admin.EmailContactListAdmin(model, admin_site)[source]
Bases:
SmarterSuperUserOnlyModelAdminCustom admin for the EmailContactList model: the emails that the landing page collected.
They belong to no account, so only superusers see them.
- date_hierarchy = 'created_at'
- list_display = ['email', 'created_at', 'updated_at']
- property media
- ordering = ('-created_at',)
- search_fields = ['email']
- class smarter.apps.dashboard.admin.LogEntryAdmin(model, admin_site)[source]
Bases:
SmarterSuperUserOnlyModelAdminThe admin’s own audit log, Django’s
LogEntry, read-only, for superusers.Django writes an entry for each add, change and delete made in the admin: who, when, which row and what changed. Changes made with manifests are journaled in the Smarter Journal instead.
- date_hierarchy = 'action_time'
- get_readonly_fields(request, obj=None)[source]
Make every field read-only.
- Parameters:
request – The admin request.
obj – The entry, or None.
- Returns:
Every field’s name.
- has_add_permission(request, obj=None)[source]
Never add an entry by hand: Django writes them.
- Parameters:
request (
HttpRequest) – The admin request.obj – Unused.
- Return type:
- Returns:
False.
- has_change_permission(request, obj=None)[source]
Never change an entry: the log is an audit trail.
- Parameters:
request (
HttpRequest) – The admin request.obj – The entry, or None.
- Return type:
- Returns:
False.
- has_delete_permission(request, obj=None)[source]
Never delete an entry: the log is an audit trail.
- Parameters:
request (
HttpRequest) – The admin request.obj – The entry, or None.
- Return type:
- Returns:
False.
- list_display = ['action_time', 'user', 'content_type', 'object_repr', 'action_flag', 'change_message']
- list_filter = ['action_flag', 'content_type']
- property media
- ordering = ['-action_time']
- search_fields = ['object_repr', 'change_message', 'user__username']
- smarter.apps.dashboard.admin.OWNED_RESOURCE_SEARCH_FIELDS = ('name', 'description', 'user_profile__user__username')
What the list of an owned resource searches, unless its admin names its own
search_fields.('name', 'description', 'user_profile__user__username')
- smarter.apps.dashboard.admin.RAW_ID_MODELS: tuple[type[Model], ...] = (<class 'django.contrib.auth.models.User'>, <class 'smarter.apps.account.models.user_profile.UserProfile'>, <class 'smarter.apps.account.models.account.Account'>)
The related models whose foreign keys a form shows as an id with a lookup, not a dropdown.
Each of these tables grows with the platform’s users, and a dropdown would render every row.
( <class 'django.contrib.auth.models.User'>, <class 'smarter.apps.account.models.user_profile.UserProfile'>, <class 'smarter.apps.account.models.account.Account'>, )
- class smarter.apps.dashboard.admin.RestrictedAdminSite(name='admin')[source]
Bases:
AdminSiteCustom admin site that restricts access to certain apps and models and modifies the admin console header title.
- each_context(request)[source]
Return the admin’s template context, whose header names the request’s user’s role.
The header is computed for each request and put in its context, never kept on the site: the site is one object, which concurrent requests share.
- Parameters:
request (
HttpRequest) – The admin request.- Returns:
The template context of every admin page.
- has_all_permission(request)[source]
Whether the request’s user is authenticated.
- Parameters:
request – The admin request.
- Returns:
True if the user is authenticated.
- site_header = 'Smarter Admin Console v9999.9999.9999.dev9999'
- smarter.apps.dashboard.admin.SITE_HEADER = 'Smarter Admin Console v9999.9999.9999.dev9999'
The admin console’s header, before the user’s role.
- class smarter.apps.dashboard.admin.SmarterAutoModelAdmin(model, admin_site)[source]
Bases:
SmarterSuperUserOnlyModelAdminA superuser-only admin whose list is built from its model’s fields, by
smarter_auto_model_admin().smarter.urls.consoleregisters one for each model that has no admin of its own, e.g. a third-party app’s.- property media
- class smarter.apps.dashboard.admin.SmarterCustomerModelAdmin(model, admin_site)[source]
Bases:
SmarterModelAdminMixinThe admin of an account’s resources: staff see the rows of their own account.
The admin site lets only staff in. Its list shows the rows of
get_queryset(): for a model of owned resources, those that the user has ownership permission for, bysmarter_scoped_queryset(). An admin of any other model scopes its rows in its ownget_queryset(), e.g. by the ownership of a parent resource. A row may be viewed if it is in the list, and changed or deleted with ownership permission (smarter_has_ud_permission()). Only superusers add rows.- get_queryset(request)[source]
Return the rows that the request’s user may see.
- Parameters:
request (
HttpRequest) – The admin request.- Returns:
The rows, scoped by
smarter_scoped_queryset().
- has_add_permission(request, obj=None)[source]
Let superusers add rows, and no one else: resources are created with manifests.
An anonymous user gets False without a warning.
- Parameters:
request (
HttpRequest) – The admin request.obj – The parent row of an inline, or None.
- Return type:
- Returns:
True for a superuser.
- has_change_permission(request, obj=None)[source]
Let a user change a row they have ownership permission for.
- Parameters:
request (
HttpRequest) – The admin request.obj – The row, or None for the list, which only a superuser may change.
- Return type:
- Returns:
The result of
smarter_has_ud_permission().
- has_delete_permission(request, obj=None)[source]
Let a user delete a row they have ownership permission for.
- Parameters:
request (
HttpRequest) – The admin request.obj – The row, or None for the list’s delete action, which only a superuser may use.
- Return type:
- Returns:
The result of
smarter_has_ud_permission().
- has_module_permission(request)[source]
Show the app’s module to any authenticated user.
An anonymous visitor, e.g. of the admin’s login page, gets False without a warning. Its
request.useris a lazyAnonymousUser, which is expected.- Parameters:
request (
HttpRequest) – The admin request.- Return type:
- Returns:
True if the request’s resolved user is an authenticated User.
- has_view_permission(request, obj=None)[source]
Let a user view the list, whose rows
get_queryset()scopes, and each row in it.- Parameters:
request (
HttpRequest) – The admin request.obj – The row, or None for the list.
- Returns:
True for a superuser; for another authenticated user, True for the list, and for a row of it.
- property media
- class smarter.apps.dashboard.admin.SmarterModelAdminMixin(model, admin_site)[source]
Bases:
ModelAdminWhat every Smarter model admin does, whatever its permission tier.
Deletes a SAM resource with its broker, as
smarter deletedoes. The delete page lists the resources that depend on it as protected, so the admin refuses the delete, as the broker would. A delete that the page allows is done by the broker, which invalidates the resource’s caches and journals the delete. Rows of other models are deleted as Django deletes them.Exports SAM resources as manifests: the list’s Export the selected resources as manifests action downloads each one’s manifest, as
smarter describereturns it, with a Secret’s value redacted.Searches every list of owned resources, by name, description and owner, unless its admin names its own
search_fields.Keeps its lists and forms fast on large tables: a list joins the foreign keys that it shows, and a form shows a foreign key to a user, user profile or account as an id with a lookup, not a dropdown of every row.
- delete_model(request, obj)[source]
Delete a row: a SAM resource with its broker, any other row as Django does.
Why a broker didn’t delete its resource is kept on the request, for
response_delete().- Parameters:
request (
HttpRequest) – The admin request.obj – The row.
- Return type:
- delete_queryset(request, queryset)[source]
Delete the rows of the list’s delete action: each SAM resource with its broker, the others as Django does.
A resource that its broker didn’t delete is reported.
- Parameters:
request (
HttpRequest) – The admin request.queryset (
QuerySet) – The selected rows.
- Return type:
- export_manifests(request, queryset)[source]
Download the selected resources’ manifests: one YAML file, or a zip file of several.
Each manifest is the one that
smarter describereturns to the resource’s owner, at<account number>/<kind>/<name>.yamlin the zip file, as Download My Data writes it. A Secret’s value is redacted. A resource that has no manifest is reported, and left out.- Parameters:
request (
HttpRequest) – The admin request.queryset (
QuerySet) – The selected rows.
- Returns:
The file, or None, to show the list again, if no resource had a manifest.
- formfield_for_foreignkey(db_field, request, **kwargs)[source]
Show a foreign key to a large table as an id with a lookup, not a dropdown of every row.
- Parameters:
db_field – The model’s foreign key.
request – The admin request.
kwargs – The form field’s arguments.
- Returns:
The form field.
- formfield_for_manytomany(db_field, request, **kwargs)[source]
Show a many-to-many field to a large table as ids with a lookup, not a list of every row.
- Parameters:
db_field – The model’s many-to-many field.
request – The admin request.
kwargs – The form field’s arguments.
- Returns:
The form field, or None for a field whose intermediary model the admin doesn’t show.
- get_actions(request, *args, **kwargs)[source]
Add the export action to the list of a model whose rows are SAM resources.
- Parameters:
request (
HttpRequest) – The admin request.args – Django’s other arguments, if any.
kwargs – Django’s other keyword arguments, if any.
- Returns:
The admin’s actions.
- get_deleted_objects(objs, request)[source]
List what a delete would delete, and, as protected, the resources that depend on a SAM resource.
Django’s delete page refuses a delete that has protected objects, so a resource that others depend on can’t be deleted in the admin, as
smarter deleterefuses it.- Parameters:
objs – The rows to delete.
request (
HttpRequest) – The admin request.
- Returns:
Django’s deleted objects, model count, permissions needed and protected objects, with each dependent added to the protected objects.
Join the foreign keys that the list shows, unless the admin names its own.
Django joins only the non-null foreign keys of a list by default. A user profile is shown with its user and account, so those are joined too.
- Parameters:
request (
HttpRequest) – The admin request.- Returns:
The admin’s own
list_select_related, or the relations of the list’s columns.
- get_search_fields(request)[source]
Search an owned resource’s name, description and owner, unless the admin names its own
search_fields.- Parameters:
request (
HttpRequest) – The admin request.- Returns:
The admin’s own
search_fields,OWNED_RESOURCE_SEARCH_FIELDSfor a model of owned resources, or none.
- property media
- response_delete(request, obj_display, obj_id)[source]
Show the list again after a delete, with why it failed, if its broker didn’t delete the resource.
- Parameters:
request (
HttpRequest) – The admin request.obj_display – The row’s string form.
obj_id – The row’s primary key.
- Returns:
Django’s response, or a redirect to the list with the error.
- class smarter.apps.dashboard.admin.SmarterStaffOnlyModelAdmin(model, admin_site)[source]
Bases:
SmarterCustomerModelAdminSmarterCustomerModelAdmin, which also checks that the user is staff at every step.The admin site already lets only staff in. This checks it again, for the models whose rows only an account’s administrators should ever see, e.g. its contacts and its api keys.
- has_change_permission(request, obj=None)[source]
Let staff change a row they have ownership permission for.
- Parameters:
request (
HttpRequest) – The admin request.obj – The object, or None for the model as a whole.
- Return type:
- Returns:
True if the user has the permission.
- has_delete_permission(request, obj=None)[source]
Let staff delete a row they have ownership permission for.
- Parameters:
request (
HttpRequest) – The admin request.obj – The object, or None for the model as a whole.
- Return type:
- Returns:
True if the user has the permission.
- has_module_permission(request)[source]
Override the default module permission logic to restrict access to staff users and superusers only.
- Return type:
- has_view_permission(request, obj=None)[source]
Let staff view the list, whose rows
get_queryset()scopes, and each row in it.- Parameters:
request (
HttpRequest) – The admin request.obj – The row, or None for the list.
- Returns:
True if the user is staff and
SmarterCustomerModelAdmin.has_view_permission()agrees.
- property media
- class smarter.apps.dashboard.admin.SmarterSuperUserOnlyModelAdmin(model, admin_site)[source]
Bases:
SmarterModelAdminMixinCustomized Django Admin console model class that restricts module access to superusers only.
- has_add_permission(request, obj=None)[source]
Override the default add permission logic to restrict access to superusers only.
- Parameters:
request (
HttpRequest) – The admin request.obj – The object, or None for the model as a whole.
- Return type:
- Returns:
True if the user has the permission.
- has_change_permission(request, obj=None)[source]
Override the default change permission logic to restrict access to superusers only.
- Parameters:
request (
HttpRequest) – The admin request.obj – The object, or None for the model as a whole.
- Return type:
- Returns:
True if the user has the permission.
- has_delete_permission(request, obj=None)[source]
Override the default delete permission logic to restrict access to superusers only.
- Parameters:
request (
HttpRequest) – The admin request.obj – The object, or None for the model as a whole.
- Return type:
- Returns:
True if the user has the permission.
- has_module_permission(request)[source]
Override the default module permission logic to restrict access to superusers only.
- Return type:
- has_view_permission(request, obj=None)[source]
Override the default view permission logic to restrict access to superusers only.
- Parameters:
request (
HttpRequest) – The admin request.obj – The object, or None for the model as a whole.
- Returns:
True if the user has the permission.
- property media
- smarter.apps.dashboard.admin.UNSEARCHABLE_FIELD_NAMES = frozenset({'code', 'digest', 'handle', 'password', 'salt', 'secret', 'session_key', 'sha256', 'token', 'version'})
credentials, hashes and versions.
- Type:
The text fields that
smarter_auto_model_admin()never searches
- smarter.apps.dashboard.admin.UNSEARCHABLE_FIELD_SUFFIXES = ('_key', '_secret', '_token', '_digest', '_hash')
The suffixes of the field names that
smarter_auto_model_admin()never searches.('_key', '_secret', '_token', '_digest', '_hash')
- smarter.apps.dashboard.admin.smarter_admin_account_ids(user)[source]
Return the ids of the accounts that a user belongs to, which a staff user administers.
- smarter.apps.dashboard.admin.smarter_admin_role(request)[source]
Return the role of the request’s user, for the admin console’s header.
- Parameters:
request (
HttpRequest) – The admin request.- Return type:
- Returns:
guest,unknown,superuser,account admin(staff), orcustomer - <first name> <last name>.
- smarter.apps.dashboard.admin.smarter_admin_user(request)[source]
Return the request’s authenticated user.
An anonymous visitor, e.g. of the admin’s login page, gets None without a warning.
- Parameters:
request (
HttpRequest) – The admin request.- Return type:
- Returns:
The resolved
User, or None if the request has no authenticated user.
- smarter.apps.dashboard.admin.smarter_auto_model_admin(model)[source]
Return a
SmarterAutoModelAdminfor a model, whose list is built from the model’s fields.Its list shows up to
AUTO_LIST_DISPLAY_LIMITof the model’s short fields, filters by its boolean and choice fields, searches the fields thatsmarter_auto_search_field()returns, and has a date hierarchy ofcreated_at. Its timestamps are read-only.- Parameters:
- Return type:
- Returns:
A subclass of
SmarterAutoModelAdmin, with the model’s options as its class attributes.
- smarter.apps.dashboard.admin.smarter_auto_search_field(field)[source]
Return what a list searches for one of its model’s fields, or None.
A short text field without choices is searched, unless it holds a credential, a hash or a version (see
UNSEARCHABLE_FIELD_NAMESandUNSEARCHABLE_FIELD_SUFFIXES). A foreign key is searched by the related row’sname, or itsusername, if it has one.
- smarter.apps.dashboard.admin.smarter_has_ud_permission(request, obj=None)[source]
Helper method to determine if the user has permission to Update or Delete (UD) an object based on ownership and account association.
param request: HttpRequest object containing user information param obj: The object for which update/delete permission is being checked (optional) rtype: bool return: True if the user has update/delete permission for the object, False otherwise
- Return type:
- smarter.apps.dashboard.admin.smarter_is_staff(request)[source]
Helper method to determine if the user is a staff member.
param request: HttpRequest object containing user information rtype: bool return: True if the user is a staff member, False otherwise
- Return type:
- smarter.apps.dashboard.admin.smarter_queryset_contains(queryset, obj)[source]
Return whether an object is a row of a queryset.
- smarter.apps.dashboard.admin.smarter_raw_id_fields(model)[source]
Return the foreign keys of a model that a form should show as an id with a lookup.
- smarter.apps.dashboard.admin.smarter_resource_kind(obj)[source]
Return the SAM kind of a row that is a SAM resource with a broker.
- smarter.apps.dashboard.admin.smarter_scoped_queryset(queryset, user)[source]
Return the rows of a queryset that a user may see in the admin.
A superuser sees every row. Of a model of owned resources, another user sees those they have ownership permission for: a staff user, those of their own account. The rows of any other model are returned as they are, and the model’s admin scopes them in its own
get_queryset(), e.g. by the ownership of a parent resource.