mirror of
https://github.com/jazzband/django-authority.git
synced 2026-03-16 22:20:28 +00:00
Added site-wide admin action (1.1 only) for editing permissions of arbitrary objects.
This commit is contained in:
parent
5ce1b10a7d
commit
f9f0f63805
5 changed files with 230 additions and 1 deletions
|
|
@ -10,7 +10,8 @@ authority.autodiscover()
|
|||
handler500 # Pyflakes
|
||||
|
||||
urlpatterns = patterns('',
|
||||
(r'^admin/(.*)', admin.site.root),
|
||||
#(r'^admin/(.*)', admin.site.root),
|
||||
('^admin/', include(admin.site.urls)),
|
||||
(r'^perms/', include('authority.urls')),
|
||||
(r'^accounts/login/$', 'django.contrib.auth.views.login'),
|
||||
url(r'^(?P<url>[\/0-9A-Za-z]+)$', 'example.exampleapp.views.top_secret'),
|
||||
|
|
|
|||
|
|
@ -27,3 +27,6 @@ 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
|
||||
|
|
|
|||
102
src/authority/actions.py
Normal file
102
src/authority/actions.py
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
from django import forms, template
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.contrib.admin import helpers, site, ACTION_CHECKBOX_NAME
|
||||
from django.shortcuts import render_to_response
|
||||
from django.utils.encoding import force_unicode
|
||||
from django.utils.html import escape
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.text import capfirst
|
||||
from django.utils.translation import ugettext_lazy, ugettext as _
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.forms.formsets import all_valid
|
||||
|
||||
try:
|
||||
set
|
||||
except NameError:
|
||||
from sets import Set as set # Python 2.3 fallback
|
||||
|
||||
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
|
||||
selected = request.POST.getlist(ACTION_CHECKBOX_NAME)
|
||||
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()
|
||||
return None
|
||||
|
||||
context = {
|
||||
'errors': ActionErrorList(formsets),
|
||||
'title': _('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")
|
||||
|
||||
if actions:
|
||||
site.add_action(edit_permissions)
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
{% load i18n %}
|
||||
<div class="inline-group">
|
||||
<div class="tabular inline-related {% if forloop.last %}last-related{% endif %}">
|
||||
{{ inline_admin_formset.formset.management_form }}
|
||||
<fieldset class="module">
|
||||
<h2>{{ inline_admin_formset.formset.instance }}</h2>
|
||||
{{ inline_admin_formset.formset.non_form_errors }}
|
||||
<table>
|
||||
<thead><tr>
|
||||
{% for field in inline_admin_formset.fields %}
|
||||
{% if not field.is_hidden %}
|
||||
<th {% if forloop.first %}colspan="2"{% endif %}>{{ field.label|capfirst }}</th>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% if inline_admin_formset.formset.can_delete %}<th>{% trans "Delete?" %}</th>{% endif %}
|
||||
</tr></thead>
|
||||
|
||||
{% for inline_admin_form in inline_admin_formset %}
|
||||
{% if inline_admin_form.form.non_field_errors %}
|
||||
<tr><td colspan="{{ inline_admin_form.field_count }}">{{ inline_admin_form.form.non_field_errors }}</td></tr>
|
||||
{% endif %}
|
||||
<tr class="{% cycle row1,row2 %} {% if inline_admin_form.original or inline_admin_form.show_url %}has_original{% endif %}">
|
||||
|
||||
<td class="original">
|
||||
{% if inline_admin_form.original or inline_admin_form.show_url %}<p>
|
||||
{% if inline_admin_form.original %} {{ inline_admin_form.original }}{% endif %}
|
||||
{% if inline_admin_form.show_url %}<a href="../../../r/{{ inline_admin_form.original_content_type_id }}/{{ inline_admin_form.original.id }}/">{% trans "View on site" %}</a>{% endif %}
|
||||
</p>{% endif %}
|
||||
{% if inline_admin_form.has_auto_field %}{{ inline_admin_form.pk_field.field }}{% endif %}
|
||||
{{ inline_admin_form.fk_field.field }}
|
||||
{% spaceless %}
|
||||
{% for fieldset in inline_admin_form %}
|
||||
{% for line in fieldset %}
|
||||
{% for field in line %}
|
||||
{% if field.is_hidden %} {{ field.field }} {% endif %}
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
{% endspaceless %}
|
||||
</td>
|
||||
|
||||
{% for fieldset in inline_admin_form %}
|
||||
{% for line in fieldset %}
|
||||
{% for field in line %}
|
||||
<td class="{{ field.field.name }}">
|
||||
{{ field.field.errors.as_ul }}
|
||||
{{ field.field }}
|
||||
</td>
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
|
||||
{% if inline_admin_formset.formset.can_delete %}
|
||||
<td class="delete">{% if inline_admin_form.original %}{{ inline_admin_form.deletion_field.field }}{% endif %}</td>
|
||||
{% endif %}
|
||||
|
||||
</tr>
|
||||
|
||||
{% endfor %}
|
||||
|
||||
</table>
|
||||
|
||||
</fieldset>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
57
src/authority/templates/admin/permission_change_form.html
Normal file
57
src/authority/templates/admin/permission_change_form.html
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
{% extends "admin/base_site.html" %}
|
||||
{% load i18n admin_modify adminmedia %}
|
||||
|
||||
{% block extrahead %}{{ block.super }}
|
||||
<script type="text/javascript" src="../../../jsi18n/"></script>
|
||||
{{ media }}
|
||||
{% endblock %}
|
||||
|
||||
{% block extrastyle %}{{ block.super }}<link rel="stylesheet" type="text/css" href="{% admin_media_prefix %}css/forms.css" />{% endblock %}
|
||||
|
||||
{% block coltype %}{% if ordered_objects %}colMS{% else %}colM{% endif %}{% endblock %}
|
||||
|
||||
{% block bodyclass %}{{ opts.app_label }}-{{ opts.object_name.lower }} change-form{% endblock %}
|
||||
|
||||
{% block breadcrumbs %}{% if not is_popup %}
|
||||
<div class="breadcrumbs">
|
||||
<a href="../../">{% trans "Home" %}</a> ›
|
||||
<a href="../">{{ app_label|capfirst|escape }}</a> ›
|
||||
<a href="./">{{ opts.verbose_name_plural|capfirst }}</a> ›
|
||||
{% trans "Permissions" %}
|
||||
</div>
|
||||
{% endif %}{% endblock %}
|
||||
|
||||
{% block content %}<div id="content-main">
|
||||
<form action="{{ form_url }}" method="post" id="{{ opts.module_name }}_form">
|
||||
<div>
|
||||
{% if is_popup %}<input type="hidden" name="_popup" value="1" />{% endif %}
|
||||
{% if save_on_top %}{% submit_row %}{% endif %}
|
||||
{% if errors %}
|
||||
<p class="errornote">
|
||||
{% blocktrans count errors|length as counter %}Please correct the error below.{% plural %}Please correct the errors below.{% endblocktrans %}
|
||||
</p>
|
||||
<ul class="errorlist">{% for error in adminform.form.non_field_errors %}<li>{{ error }}</li>{% endfor %}</ul>
|
||||
{% endif %}
|
||||
|
||||
{% for fieldset in adminform %}
|
||||
{% include "admin/includes/fieldset.html" %}
|
||||
{% endfor %}
|
||||
|
||||
{% for inline_admin_formset in inline_admin_formsets %}
|
||||
{% include inline_admin_formset.opts.template %}
|
||||
{% endfor %}
|
||||
|
||||
{% block after_related_objects %}{% endblock %}
|
||||
|
||||
<div class="submit-row">
|
||||
<input type="submit" value="{% trans 'Save' %}" class="default" name="_save" {{ onclick_attrib }}/>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
{% for obj in queryset %}
|
||||
<input type="hidden" name="{{ action_checkbox_name }}" value="{{ obj.pk }}" />
|
||||
{% endfor %}
|
||||
<input type="hidden" name="action" value="edit_permissions" />
|
||||
<input type="hidden" name="post" value="yes" />
|
||||
</form></div>
|
||||
{% endblock %}
|
||||
Loading…
Reference in a new issue