Utility functions for template tags¶
Like
_add_link_to_context(), but allows for using a specific named permission, and any named url on the modeladmin, with optional url parameters.Uses
_add_link_to_context()internally once it has established the permissions are OK.Always returns a dictionary with two keys, whose values may be empty strings.
Parameters: - admin_site – the string name of an admin site; eg: admin
- request – the current
WSGIRequest - opts – the _meta Options object to get the app_label and module_name for the desired URL.
- permname – The permission name to find; eg: add, change, delete
- viewname – The name of the view to find; eg: changelist, donkey
- url_params – a list of items to be passed as args to the underlying use of reverse.
- query – querystring to append.
Returns: a dictionary containing link and verbose_name keys, whose values are the reversed URL and the display name of the object. Both may be blank.
Return type: dictionary
Find out if a model is in our known list and at has least 1 permission. If it’s in there, try and reverse the URL to return a dictionary for the final Inclusion Tag’s context.
Always returns a dictionary with two keys, whose values may be empty strings.
Parameters: - admin_site – the string name of an admin site; eg: admin
- request – the current
WSGIRequest - opts – the _meta Options object to get the app_label and module_name for the desired URL.
- permname – The permission name to find; eg: add, change, delete
- url_params – a list of items to be passed as args to the underlying use of reverse.
- query – querystring to append.
Returns: a dictionary containing link and verbose_name keys, whose values are the reversed URL and the display name of the object. Both may be blank.
Return type: dictionary
Minor wrapper around
reverse(), catching theNoReverseMatchthat may be thrown, and instead returning an empty unicode string.Parameters: - urlname – the view, or named URL to be reversed
- params – any parameters (as args, not kwargs) required to create the correct URL.
Returns: the URL discovered, or an empty string
Return type: unicode string
If we’re not at 1.6, the changelist uses “pop” in the querystring.
Changed in version 0.8.1: Returns a tuple of the querystring and a boolean of whether or not
Given the name of an
AdminSiteinstance, try to resolve that into an actual objectNote
This function is exposed in the public API as
get_admin_site(), which uses memoization to cache discoveredAdminSiteobjects.Parameters: admin_site – the string name of an AdminSitenamed and mounted on the project.Returns: an AdminSiteinstance matching that given in the admin_site parameter.Return type: AdminSiteorNone
A pulled-up-and-out version of the sorting the standard Django
AdminSitedoes on the index view.Parameters: modeladmins – dictionary of modeladmins Returns: the same modeladmins, with their ordering changed. Return type: list
Given a context, determine whether a
Userexists, and if they see anything.Changed in version 0.8.1: if
DEBUGisTrue, then better error messages are displayed to the user, as a reminder of what settings need to be in place. Previously it was dependent on having aLOGGINGconfiguration that would show the messages.Parameters: context – a RequestContext. Accepts anyContextlike object, but it explicitly tests for a request key and request.userReturns: whether or not the given context should allow further processing. Return type: TrueorFalse
The public API implementation of
_get_admin_site(), wrapped to use memoization.
Taken from
AdminSite, find allModelAdminclasses attached to the given admin and compile a dictionary ofModeltypes visible to the currentUser, limiting the methods available (add/edit/history/delete) as appropriate.Always returns a dictionary, though it may be empty, and thus evaluate as Falsy.
Parameters: - request – the current request, for permissions checking etc.
- admin_site – a concrete
AdminSitenamed and mounted on the project.
Returns: visible
ModelAdminclasses.Return type: dictionary