14.3.122. crate_anon.crateweb.extra.admin

crate_anon/crateweb/extra/admin.py


Copyright (C) 2015, University of Cambridge, Department of Psychiatry. Created by Rudolf Cardinal (rnc1001@cam.ac.uk).

This file is part of CRATE.

CRATE is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

CRATE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with CRATE. If not, see <https://www.gnu.org/licenses/>.


Extensions to Django admin site classes.

class crate_anon.crateweb.extra.admin.AddOnlyModelAdmin(model, admin_site)[source]

ModelAdmin that allows add, but not edit or delete.

Optional extra class attribute: fields_for_viewing.

actions = None
get_changelist(request: HttpRequest, **kwargs) Type[ChangeList][source]

Return the ChangeList class for use on the changelist page.

get_fields(request: HttpRequest, obj=None) List[str][source]

Hook for specifying fields.

get_readonly_fields(request: HttpRequest, obj=None) List[str][source]

Hook for specifying custom readonly fields.

has_delete_permission(request: HttpRequest, obj=None) bool[source]

Return True if the given request has permission to change the given Django model instance, the default implementation doesn’t examine the obj parameter.

Can be overridden by the user in subclasses. In such case it should return True if the given request has permission to delete the obj model instance. If obj is None, this should return True if the given request has permission to delete any object of the given type.

class crate_anon.crateweb.extra.admin.AllStaffReadOnlyModelAdmin(model, admin_site)[source]

ReadOnlyModelAdmin that allows access to all staff, not just superusers. (No easy way to make this work via multiple inheritance.)

has_change_permission(request: HttpRequest, obj=None) bool[source]

Return True if the given request has permission to change the given Django model instance, the default implementation doesn’t examine the obj parameter.

Can be overridden by the user in subclasses. In such case it should return True if the given request has permission to change the obj model instance. If obj is None, this should return True if the given request has permission to change any object of the given type.

has_module_permission(request: HttpRequest) bool[source]

Return True if the given request has any permission in the given app label.

Can be overridden by the user in subclasses. In such case it should return True if the given request has permission to view the module on the admin index page and access the module’s index page. Overriding it does not restrict access to the add, change or delete views. Use ModelAdmin.has_(add|change|delete)_permission for that.

class crate_anon.crateweb.extra.admin.EditOnceOnlyModelAdmin(model, admin_site)[source]

ModelAdmin that allows editing, but not add or delete.

Designed for e.g. when you have a fixed set of PKs. In that situation, ensure the PK field is in readonly_fields.

actions = None
has_add_permission(request: HttpRequest, obj=None) bool[source]

Return True if the given request has permission to add an object. Can be overridden by the user in subclasses.

has_delete_permission(request: HttpRequest, obj=None) bool[source]

Return True if the given request has permission to change the given Django model instance, the default implementation doesn’t examine the obj parameter.

Can be overridden by the user in subclasses. In such case it should return True if the given request has permission to delete the obj model instance. If obj is None, this should return True if the given request has permission to delete any object of the given type.

class crate_anon.crateweb.extra.admin.EditOnlyModelAdmin(model, admin_site)[source]

ModelAdmin that allows editing, but not add or delete.

Designed for e.g. when you have a fixed set of PKs. In that situation, ensure the PK field is in readonly_fields.

actions = None
has_add_permission(request: HttpRequest, obj=None) bool[source]

Return True if the given request has permission to add an object. Can be overridden by the user in subclasses.

has_delete_permission(request: HttpRequest, obj=None) bool[source]

Return True if the given request has permission to change the given Django model instance, the default implementation doesn’t examine the obj parameter.

Can be overridden by the user in subclasses. In such case it should return True if the given request has permission to delete the obj model instance. If obj is None, this should return True if the given request has permission to delete any object of the given type.

class crate_anon.crateweb.extra.admin.ReadOnlyChangeList(*args, **kwargs)[source]

Variant of django.contrib.admin.views.main.ChangeList that that changes the text for a read-only context.

__init__(*args, **kwargs) None[source]
class crate_anon.crateweb.extra.admin.ReadOnlyModelAdmin(model, admin_site)[source]

ModelAdmin that allows users to view (“change”), but not add/edit/delete.

You also need to do this:

my_admin_site.index_template = 'admin/viewchange_admin_index.html'

… to give a modified admin/index.html that says “View/change” not “Change”.

actions = None
get_changelist(request: HttpRequest, **kwargs) Type[ChangeList][source]

Return the ChangeList class for use on the changelist page.

has_add_permission(request: HttpRequest, obj=None) bool[source]

Return True if the given request has permission to add an object. Can be overridden by the user in subclasses.

has_delete_permission(request: HttpRequest, obj=None) bool[source]

Return True if the given request has permission to change the given Django model instance, the default implementation doesn’t examine the obj parameter.

Can be overridden by the user in subclasses. In such case it should return True if the given request has permission to delete the obj model instance. If obj is None, this should return True if the given request has permission to delete any object of the given type.

save_model(request: HttpRequest, obj, form: ModelForm, change: bool)[source]

Given a model instance save it to the database.