Ticket #10964: group_admin.patch
File group_admin.patch, 5.5 KB (added by , 16 years ago) |
---|
-
django/contrib/admin/templates/admin/auth/group_form.html
1 {% extends "admin/change_form.html" %} 2 3 {% block extrahead %} 4 {{ block.super }} 5 {{ form.media }} 6 {% endblock %} 7 8 {% load i18n %} 9 10 {% block after_field_sets %} 11 <fieldset class="module aligned"> 12 13 <div class="form-row"> 14 {{ form.name.errors }} 15 <label for="id_name" class="required">{% trans 'Name' %}:</label> {{ form.name }} 16 <p class="help">{{ form.name.help_text }}</p> 17 </div> 18 19 <div class="form-row"> 20 {{ form.permissions.errors }} 21 <label for="id_permissions">{% trans 'Permissions' %}:</label> {{ form.permissions }} 22 <p class="help">{{ form.permissions.help_text }}</p> 23 </div> 24 25 <div class="form-row"> 26 {{ form.users.errors }} 27 <label for="id_users">{% trans 'Users' %}:</label> {{ form.users }} 28 <p class="help">{{ form.users.help_text }}</p> 29 </div> 30 31 <script type="text/javascript">document.getElementById("id_name").focus();</script> 32 </fieldset> 33 {% endblock %} -
django/contrib/auth/admin.py
1 1 from django import template 2 2 from django.conf import settings 3 3 from django.contrib import admin 4 from django.contrib.auth.forms import UserCreationForm, UserChangeForm, AdminPasswordChangeForm 4 from django.contrib.auth.forms import UserCreationForm, UserChangeForm, AdminPasswordChangeForm, GroupForm 5 5 from django.contrib.auth.models import User, Group 6 6 from django.core.exceptions import PermissionDenied 7 7 from django.http import HttpResponseRedirect, Http404 … … 11 11 from django.utils.translation import ugettext, ugettext_lazy as _ 12 12 13 13 class GroupAdmin(admin.ModelAdmin): 14 15 16 filter_horizontal =('permissions',)14 search_fields = ('name',) 15 ordering = ('name',) 16 filter_horizontal=('permissions',) 17 17 18 def add_view(self, request, form_url='', extra_context=None): 19 if request.method == 'POST': 20 form = GroupForm(request.POST) 21 if form.is_valid(): 22 form.save() 23 else: 24 form = GroupForm() 25 dictionary = {'title': _(u'Add group'), 'form': form, 26 'is_popup': '_popup' in request.REQUEST, 'add': True, 27 'change': False, 'has_add_permission': True, 28 'has_delete_permission': False, 29 'has_change_permission': True, 'has_file_field': False, 30 'has_absolute_url': False, 'auto_populated_fields': (), 31 'opts': self.model._meta, 'save_as': False, 32 'root_path': self.admin_site.root_path, 33 'app_label': self.model._meta.app_label} 34 return render_to_response('admin/auth/group_form.html', dictionary) 35 36 def change_view(self, request, object_id, extra_context=None): 37 if request.method == 'POST': 38 form = GroupForm(request.POST, 39 instance=Group.objects.get(id=object_id)) 40 if form.is_valid(): 41 form.save() 42 else: 43 form = GroupForm(instance=Group.objects.get(id=object_id)) 44 dictionary = {'title': _(u'Edit group'), 'form': form, 45 'is_popup': '_popup' in request.REQUEST, 'add': True, 46 'change': False, 'has_add_permission': True, 47 'has_delete_permission': False, 48 'has_change_permission': True, 'has_file_field': False, 49 'has_absolute_url': False, 'auto_populated_fields': (), 50 'opts': self.model._meta, 'save_as': False, 51 'root_path': self.admin_site.root_path, 52 'app_label': self.model._meta.app_label} 53 return render_to_response('admin/auth/group_form.html', dictionary) 54 18 55 class UserAdmin(admin.ModelAdmin): 19 56 fieldsets = ( 20 57 (None, {'fields': ('username', 'password')}), -
django/contrib/auth/forms.py
1 from django.contrib.auth.models import User 1 from django.contrib.auth.models import User, Group 2 2 from django.contrib.auth import authenticate 3 3 from django.contrib.auth.tokens import default_token_generator 4 4 from django.contrib.sites.models import Site 5 5 from django.template import Context, loader 6 6 from django import forms 7 from django.contrib.admin.widgets import FilteredSelectMultiple 7 8 from django.utils.translation import ugettext_lazy as _ 8 9 from django.utils.http import int_to_base36 9 10 … … 205 206 if commit: 206 207 self.user.save() 207 208 return self.user 209 210 class GroupForm(forms.ModelForm): 211 212 def __init__(self, *args, **kwargs): 213 forms.ModelForm.__init__(self, *args, **kwargs) 214 initial = {} 215 users = User.objects.all().order_by('last_name') 216 if 'instance' in kwargs: 217 for user in users: 218 if user in kwargs['instance'].user_set.all(): 219 initial[str(user.id)] = '' 220 dictionary = {'queryset': users, 221 'widget': FilteredSelectMultiple('Users', False), 222 'initial': initial, 'required': False} 223 self.fields['users'] = forms.ModelMultipleChoiceField(**dictionary) 224 225 def save(self, commit=True): 226 group = forms.ModelForm.save(self, commit) 227 for user in group.user_set.all(): 228 if not user in self.cleaned_data['users']: 229 user.groups.remove(group) 230 user.save() 231 for user in self.cleaned_data['users']: 232 user.groups.add(group) 233 user.save() 234 return group 235 236 class Meta: 237 model = Group