mirror of
https://github.com/jazzband/django-authority.git
synced 2026-05-25 07:33:44 +00:00
Move the admin actions to the admin.py to make it easier to handle the registering it with the default admin site. Fixed a bug where the short description of the admin action wasn't lazy. Updated docs.
This commit is contained in:
parent
51fdf7a069
commit
6a2b56a8d0
4 changed files with 102 additions and 112 deletions
|
|
@ -39,24 +39,21 @@ You also have to modify your root URLConf (e.g. ``urls.py``) to include the
|
|||
app's URL configuration and automatically discover all the permission
|
||||
classes you defined::
|
||||
|
||||
from django.contrib import admin
|
||||
import authority
|
||||
|
||||
|
||||
admin.autodiscover()
|
||||
authority.autodiscover()
|
||||
|
||||
|
||||
# ...
|
||||
|
||||
urlpatterns += patterns('',
|
||||
(r'^authority/', include('authority.urls')),
|
||||
)
|
||||
|
||||
If you're using Django 1.1 and Django's admin interface, make sure you place
|
||||
``authority.autodiscover()`` **before** ``admin.autodiscover()``::
|
||||
|
||||
authority.autodiscover()
|
||||
admin.autodiscover()
|
||||
|
||||
This is because django-authority automatically adds a `site-wide action`_ to the
|
||||
admin-site and it prevents errors if you later decide to remove this action.
|
||||
See :ref:`handling-admin` how to remove the admin action.
|
||||
If you're using Django 1.1 this will automatically add a `site-wide action`_
|
||||
to the admin site which can be removed as shown here: :ref:`handling-admin`.
|
||||
|
||||
That's all (for now).
|
||||
|
||||
.. _site-wide action: http://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/
|
||||
.. _site-wide action: http://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/
|
||||
|
|
|
|||
|
|
@ -29,6 +29,3 @@ def autodiscover():
|
|||
__import__("%s.permissions" % app)
|
||||
app_path = sys.modules["%s.permissions" % app]
|
||||
LOADING = False
|
||||
|
||||
# Register the "edit_permission" action with the default admin site
|
||||
from authority import actions
|
||||
|
|
|
|||
|
|
@ -1,95 +0,0 @@
|
|||
from django import forms, template
|
||||
from django.contrib.admin import helpers, site
|
||||
from django.shortcuts import render_to_response
|
||||
from django.utils.encoding import force_unicode
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.translation import ugettext
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.forms.formsets import all_valid
|
||||
from django.http import HttpResponseRedirect
|
||||
|
||||
try:
|
||||
from django.contrib.admin import actions
|
||||
except ImportError:
|
||||
actions = False
|
||||
|
||||
from authority.admin import PermissionInline
|
||||
|
||||
class ActionPermissionInline(PermissionInline):
|
||||
raw_id_fields = ()
|
||||
template = 'admin/edit_inline/action_tabular.html'
|
||||
|
||||
class ActionErrorList(forms.util.ErrorList):
|
||||
def __init__(self, inline_formsets):
|
||||
for inline_formset in inline_formsets:
|
||||
self.extend(inline_formset.non_form_errors())
|
||||
for errors_in_inline_form in inline_formset.errors:
|
||||
self.extend(errors_in_inline_form.values())
|
||||
|
||||
def edit_permissions(modeladmin, request, queryset):
|
||||
opts = modeladmin.model._meta
|
||||
app_label = opts.app_label
|
||||
inline = ActionPermissionInline(queryset.model, modeladmin.admin_site)
|
||||
formsets = []
|
||||
for obj in queryset:
|
||||
prefixes = {}
|
||||
FormSet = inline.get_formset(request, obj)
|
||||
prefix = "%s-%s" % (FormSet.get_default_prefix(), obj.pk)
|
||||
prefixes[prefix] = prefixes.get(prefix, 0) + 1
|
||||
if prefixes[prefix] != 1:
|
||||
prefix = "%s-%s-%s" % (prefix, prefixes[prefix])
|
||||
if request.POST.get('post'):
|
||||
formset = FormSet(data=request.POST, files=request.FILES,
|
||||
instance=obj, prefix=prefix)
|
||||
else:
|
||||
formset = FormSet(instance=obj, prefix=prefix)
|
||||
formsets.append(formset)
|
||||
|
||||
media = modeladmin.media
|
||||
inline_admin_formsets = []
|
||||
for formset in formsets:
|
||||
fieldsets = list(inline.get_fieldsets(request))
|
||||
inline_admin_formset = helpers.InlineAdminFormSet(inline, formset, fieldsets)
|
||||
inline_admin_formsets.append(inline_admin_formset)
|
||||
media = media + inline_admin_formset.media
|
||||
|
||||
ordered_objects = opts.get_ordered_objects()
|
||||
if request.POST.get('post'):
|
||||
if all_valid(formsets):
|
||||
for formset in formsets:
|
||||
formset.save()
|
||||
# redirect to full request path to make sure we keep filter
|
||||
return HttpResponseRedirect(request.get_full_path())
|
||||
|
||||
context = {
|
||||
'errors': ActionErrorList(formsets),
|
||||
'title': ugettext('Permissions for %s') % force_unicode(opts.verbose_name_plural),
|
||||
'inline_admin_formsets': inline_admin_formsets,
|
||||
'root_path': modeladmin.admin_site.root_path,
|
||||
'app_label': app_label,
|
||||
'change': True,
|
||||
'ordered_objects': ordered_objects,
|
||||
'form_url': mark_safe(''),
|
||||
'opts': opts,
|
||||
'target_opts': queryset.model._meta,
|
||||
'content_type_id': ContentType.objects.get_for_model(queryset.model).id,
|
||||
'save_as': False,
|
||||
'save_on_top': False,
|
||||
'is_popup': False,
|
||||
'media': mark_safe(media),
|
||||
'show_delete': False,
|
||||
'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME,
|
||||
'queryset': queryset,
|
||||
"object_name": force_unicode(opts.verbose_name),
|
||||
}
|
||||
template_name = getattr(modeladmin, 'permission_change_form_template', [
|
||||
"admin/%s/%s/permission_change_form.html" % (app_label, opts.object_name.lower()),
|
||||
"admin/%s/permission_change_form.html" % app_label,
|
||||
"admin/permission_change_form.html"
|
||||
])
|
||||
return render_to_response(template_name, context,
|
||||
context_instance=template.RequestContext(request))
|
||||
edit_permissions.short_description = ugettext("Permissions for selected %(verbose_name_plural)s")
|
||||
|
||||
if actions:
|
||||
site.add_action(edit_permissions, name='edit_permissions')
|
||||
|
|
@ -1,7 +1,19 @@
|
|||
from django import forms
|
||||
from django import forms, template
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.utils.translation import ugettext, ugettext_lazy as _
|
||||
from django.shortcuts import render_to_response
|
||||
from django.utils.encoding import force_unicode
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.forms.formsets import all_valid
|
||||
from django.contrib import admin
|
||||
from django.contrib.admin import helpers
|
||||
from django.contrib.contenttypes import generic
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
|
||||
try:
|
||||
from django.contrib.admin import actions
|
||||
except ImportError:
|
||||
actions = False
|
||||
|
||||
from authority.models import Permission
|
||||
from authority.widgets import GenericForeignKeyRawIdWidget
|
||||
|
|
@ -20,6 +32,82 @@ class PermissionInline(generic.GenericTabularInline):
|
|||
return db_field.formfield(**kwargs)
|
||||
return super(PermissionInline, self).formfield_for_dbfield(db_field, **kwargs)
|
||||
|
||||
class ActionPermissionInline(PermissionInline):
|
||||
raw_id_fields = ()
|
||||
template = 'admin/edit_inline/action_tabular.html'
|
||||
|
||||
class ActionErrorList(forms.util.ErrorList):
|
||||
def __init__(self, inline_formsets):
|
||||
for inline_formset in inline_formsets:
|
||||
self.extend(inline_formset.non_form_errors())
|
||||
for errors_in_inline_form in inline_formset.errors:
|
||||
self.extend(errors_in_inline_form.values())
|
||||
|
||||
def edit_permissions(modeladmin, request, queryset):
|
||||
opts = modeladmin.model._meta
|
||||
app_label = opts.app_label
|
||||
inline = ActionPermissionInline(queryset.model, modeladmin.admin_site)
|
||||
formsets = []
|
||||
for obj in queryset:
|
||||
prefixes = {}
|
||||
FormSet = inline.get_formset(request, obj)
|
||||
prefix = "%s-%s" % (FormSet.get_default_prefix(), obj.pk)
|
||||
prefixes[prefix] = prefixes.get(prefix, 0) + 1
|
||||
if prefixes[prefix] != 1:
|
||||
prefix = "%s-%s-%s" % (prefix, prefixes[prefix])
|
||||
if request.POST.get('post'):
|
||||
formset = FormSet(data=request.POST, files=request.FILES,
|
||||
instance=obj, prefix=prefix)
|
||||
else:
|
||||
formset = FormSet(instance=obj, prefix=prefix)
|
||||
formsets.append(formset)
|
||||
|
||||
media = modeladmin.media
|
||||
inline_admin_formsets = []
|
||||
for formset in formsets:
|
||||
fieldsets = list(inline.get_fieldsets(request))
|
||||
inline_admin_formset = helpers.InlineAdminFormSet(inline, formset, fieldsets)
|
||||
inline_admin_formsets.append(inline_admin_formset)
|
||||
media = media + inline_admin_formset.media
|
||||
|
||||
ordered_objects = opts.get_ordered_objects()
|
||||
if request.POST.get('post'):
|
||||
if all_valid(formsets):
|
||||
for formset in formsets:
|
||||
formset.save()
|
||||
# redirect to full request path to make sure we keep filter
|
||||
return HttpResponseRedirect(request.get_full_path())
|
||||
|
||||
context = {
|
||||
'errors': ActionErrorList(formsets),
|
||||
'title': ugettext('Permissions for %s') % force_unicode(opts.verbose_name_plural),
|
||||
'inline_admin_formsets': inline_admin_formsets,
|
||||
'root_path': modeladmin.admin_site.root_path,
|
||||
'app_label': app_label,
|
||||
'change': True,
|
||||
'ordered_objects': ordered_objects,
|
||||
'form_url': mark_safe(''),
|
||||
'opts': opts,
|
||||
'target_opts': queryset.model._meta,
|
||||
'content_type_id': ContentType.objects.get_for_model(queryset.model).id,
|
||||
'save_as': False,
|
||||
'save_on_top': False,
|
||||
'is_popup': False,
|
||||
'media': mark_safe(media),
|
||||
'show_delete': False,
|
||||
'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME,
|
||||
'queryset': queryset,
|
||||
"object_name": force_unicode(opts.verbose_name),
|
||||
}
|
||||
template_name = getattr(modeladmin, 'permission_change_form_template', [
|
||||
"admin/%s/%s/permission_change_form.html" % (app_label, opts.object_name.lower()),
|
||||
"admin/%s/permission_change_form.html" % app_label,
|
||||
"admin/permission_change_form.html"
|
||||
])
|
||||
return render_to_response(template_name, context,
|
||||
context_instance=template.RequestContext(request))
|
||||
edit_permissions.short_description = _("Permissions for selected %(verbose_name_plural)s")
|
||||
|
||||
class PermissionAdmin(admin.ModelAdmin):
|
||||
list_display = ('codename', 'content_type', 'user', 'group')
|
||||
list_filter = ('content_type',)
|
||||
|
|
@ -53,3 +141,6 @@ class PermissionAdmin(admin.ModelAdmin):
|
|||
return super(PermissionAdmin, self).queryset(request).filter(creator=user)
|
||||
|
||||
admin.site.register(Permission, PermissionAdmin)
|
||||
|
||||
if actions:
|
||||
admin.site.add_action(edit_permissions)
|
||||
|
|
|
|||
Loading…
Reference in a new issue