Ticket #6085: 6085.patch

File 6085.patch, 10.6 KB (added by Honza Král, 17 years ago)
  • django/contrib/admin/sites.py

    diff --git a/django/contrib/admin/sites.py b/django/contrib/admin/sites.py
    index 471678f..4e95a06 100644
    a b class AdminSite(object):  
    171171        """
    172172        Handles the "user change password" task
    173173        """
    174         from django.contrib.admin.views.auth import user_change_password
     174        from django.contrib.auth.views import user_change_password
    175175        return user_change_password(request, id)
    176176
    177177    def i18n_javascript(self, request):
  • deleted file django/contrib/admin/views/auth.py

    diff --git a/django/contrib/admin/views/auth.py b/django/contrib/admin/views/auth.py
    deleted file mode 100644
    index 5c0f954..0000000
    + -  
    1 from django.contrib.admin.views.decorators import staff_member_required
    2 from django.contrib.auth.forms import UserCreationForm, AdminPasswordChangeForm
    3 from django.contrib.auth.models import User
    4 from django.core.exceptions import PermissionDenied
    5 from django import oldforms, template
    6 from django.shortcuts import render_to_response, get_object_or_404
    7 from django.http import HttpResponseRedirect
    8 from django.utils.html import escape
    9 from django.utils.translation import ugettext as _
    10 
    11 def user_add_stage(request):
    12     if not request.user.has_perm('auth.change_user'):
    13         raise PermissionDenied
    14     manipulator = UserCreationForm()
    15     if request.method == 'POST':
    16         new_data = request.POST.copy()
    17         errors = manipulator.get_validation_errors(new_data)
    18         if not errors:
    19             new_user = manipulator.save(new_data)
    20             msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': 'user', 'obj': new_user}
    21             if "_addanother" in request.POST:
    22                 request.user.message_set.create(message=msg)
    23                 return HttpResponseRedirect(request.path)
    24             else:
    25                 request.user.message_set.create(message=msg + ' ' + _("You may edit it again below."))
    26                 return HttpResponseRedirect('../%s/' % new_user.id)
    27     else:
    28         errors = new_data = {}
    29     form = oldforms.FormWrapper(manipulator, new_data, errors)
    30     return render_to_response('admin/auth/user/add_form.html', {
    31         'title': _('Add user'),
    32         'form': form,
    33         'is_popup': '_popup' in request.REQUEST,
    34         'add': True,
    35         'change': False,
    36         'has_add_permission': True,
    37         'has_delete_permission': False,
    38         'has_change_permission': True,
    39         'has_file_field': False,
    40         'has_absolute_url': False,
    41         'auto_populated_fields': (),
    42         'opts': User._meta,
    43         'username_help_text': User._meta.get_field('username').help_text,
    44     }, context_instance=template.RequestContext(request))
    45 user_add_stage = staff_member_required(user_add_stage)
    46 
    47 def user_change_password(request, id):
    48     if not request.user.has_perm('auth.change_user'):
    49         raise PermissionDenied
    50     user = get_object_or_404(User, pk=id)
    51     manipulator = AdminPasswordChangeForm(user)
    52     if request.method == 'POST':
    53         new_data = request.POST.copy()
    54         errors = manipulator.get_validation_errors(new_data)
    55         if not errors:
    56             new_user = manipulator.save(new_data)
    57             msg = _('Password changed successfully.')
    58             request.user.message_set.create(message=msg)
    59             return HttpResponseRedirect('..')
    60     else:
    61         errors = new_data = {}
    62     form = oldforms.FormWrapper(manipulator, new_data, errors)
    63     return render_to_response('admin/auth/user/change_password.html', {
    64         'title': _('Change password: %s') % escape(user.username),
    65         'form': form,
    66         'is_popup': '_popup' in request.REQUEST,
    67         'add': True,
    68         'change': False,
    69         'has_delete_permission': False,
    70         'has_change_permission': True,
    71         'has_absolute_url': False,
    72         'opts': User._meta,
    73         'original': user,
    74         'show_save': True,
    75     }, context_instance=template.RequestContext(request))
    76 user_change_password = staff_member_required(user_change_password)
  • new file django/contrib/auth/admin.py

    diff --git a/django/contrib/auth/admin.py b/django/contrib/auth/admin.py
    new file mode 100644
    index 0000000..97d284c
    - +  
     1from django.contrib.auth.forms import UserCreationForm
     2from django.contrib.auth.models import User, Group
     3from django.core.exceptions import PermissionDenied
     4from django import oldforms, template
     5from django.shortcuts import render_to_response
     6from django.http import HttpResponseRedirect
     7from django.utils.translation import ugettext, ugettext_lazy as _
     8from django.contrib import admin
     9
     10class GroupAdmin(admin.ModelAdmin):
     11    search_fields = ('name',)
     12    filter_horizontal = ('permissions',)
     13
     14class UserAdmin(admin.ModelAdmin):
     15    fieldsets = (
     16        (None, {'fields': ('username', 'password')}),
     17        (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
     18        (_('Permissions'), {'fields': ('is_staff', 'is_active', 'is_superuser', 'user_permissions')}),
     19        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
     20        (_('Groups'), {'fields': ('groups',)}),
     21    )
     22    list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff')
     23    list_filter = ('is_staff', 'is_superuser')
     24    search_fields = ('username', 'first_name', 'last_name', 'email')
     25    filter_horizontal = ('user_permissions',)
     26
     27    def add_view(self, request):
     28        if not self.has_change_permission(request):
     29            raise PermissionDenied
     30        manipulator = UserCreationForm()
     31        if request.method == 'POST':
     32            new_data = request.POST.copy()
     33            errors = manipulator.get_validation_errors(new_data)
     34            if not errors:
     35                new_user = manipulator.save(new_data)
     36                msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': 'user', 'obj': new_user}
     37                if "_addanother" in request.POST:
     38                    request.user.message_set.create(message=msg)
     39                    return HttpResponseRedirect(request.path)
     40                else:
     41                    request.user.message_set.create(message=msg + ' ' + ugettext("You may edit it again below."))
     42                    return HttpResponseRedirect('../%s/' % new_user.id)
     43        else:
     44            errors = new_data = {}
     45        form = oldforms.FormWrapper(manipulator, new_data, errors)
     46        return render_to_response('admin/auth/user/add_form.html', {
     47            'title': _('Add user'),
     48            'form': form,
     49            'is_popup': '_popup' in request.REQUEST,
     50            'add': True,
     51            'change': False,
     52            'has_add_permission': True,
     53            'has_delete_permission': False,
     54            'has_change_permission': True,
     55            'has_file_field': False,
     56            'has_absolute_url': False,
     57            'auto_populated_fields': (),
     58            'opts': User._meta,
     59            'username_help_text': User._meta.get_field('username').help_text,
     60        }, context_instance=template.RequestContext(request))
     61
     62admin.site.register(Group, GroupAdmin)
     63admin.site.register(User, UserAdmin)
     64
  • django/contrib/auth/models.py

    diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py
    index e694c76..22e0078 100644
    a b class AnonymousUser(object):  
    366366        return False
    367367
    368368# Register the admin options for these models.
    369 # TODO: Maybe this should live in a separate module admin.py, but how would we
    370 # ensure that module was loaded?
    371 
    372 from django.contrib import admin
    373 
    374 class GroupAdmin(admin.ModelAdmin):
    375     search_fields = ('name',)
    376     filter_horizontal = ('permissions',)
    377 
    378 class UserAdmin(admin.ModelAdmin):
    379     fieldsets = (
    380         (None, {'fields': ('username', 'password')}),
    381         (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
    382         (_('Permissions'), {'fields': ('is_staff', 'is_active', 'is_superuser', 'user_permissions')}),
    383         (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    384         (_('Groups'), {'fields': ('groups',)}),
    385     )
    386     list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff')
    387     list_filter = ('is_staff', 'is_superuser')
    388     search_fields = ('username', 'first_name', 'last_name', 'email')
    389     filter_horizontal = ('user_permissions',)
    390 
    391     def add_view(self, request):
    392         from django.contrib.admin.views.auth import user_add_stage
    393         return user_add_stage(request)
    394 
    395 admin.site.register(Group, GroupAdmin)
    396 admin.site.register(User, UserAdmin)
     369from django.contrib.auth import admin
  • django/contrib/auth/views.py

    diff --git a/django/contrib/auth/views.py b/django/contrib/auth/views.py
    index d3d8b4c..eb66036 100644
    a b  
    11from django.contrib.auth.forms import AuthenticationForm
    2 from django.contrib.auth.forms import PasswordResetForm, PasswordChangeForm
     2from django.contrib.auth.forms import PasswordResetForm, PasswordChangeForm, AdminPasswordChangeForm
     3from django.core.exceptions import PermissionDenied
    34from django import oldforms
    45from django.shortcuts import render_to_response
    56from django.template import RequestContext
    password_change = login_required(password_change)  
    9798
    9899def password_change_done(request, template_name='registration/password_change_done.html'):
    99100    return render_to_response(template_name, context_instance=RequestContext(request))
     101
     102def user_change_password(request, id):
     103    if not request.user.has_perm('auth.change_user'):
     104        raise PermissionDenied
     105    user = get_object_or_404(User, pk=id)
     106    manipulator = AdminPasswordChangeForm(user)
     107    if request.method == 'POST':
     108        new_data = request.POST.copy()
     109        errors = manipulator.get_validation_errors(new_data)
     110        if not errors:
     111            new_user = manipulator.save(new_data)
     112            msg = _('Password changed successfully.')
     113            request.user.message_set.create(message=msg)
     114            return HttpResponseRedirect('..')
     115    else:
     116        errors = new_data = {}
     117    form = oldforms.FormWrapper(manipulator, new_data, errors)
     118    return render_to_response('admin/auth/user/change_password.html', {
     119        'title': _('Change password: %s') % escape(user.username),
     120        'form': form,
     121        'is_popup': '_popup' in request.REQUEST,
     122        'add': True,
     123        'change': False,
     124        'has_delete_permission': False,
     125        'has_change_permission': True,
     126        'has_absolute_url': False,
     127        'opts': User._meta,
     128        'original': user,
     129        'show_save': True,
     130    }, context_instance=template.RequestContext(request))
Back to Top