Ticket #5132: auth.py

File auth.py, 3.3 KB (added by javierder@…, 17 years ago)

Patched version

Line 
1from django.contrib.admin.views.decorators import staff_member_required
2from django.contrib.auth.forms import UserCreationForm, AdminPasswordChangeForm
3from django.contrib.auth.models import User
4from django.core.exceptions import PermissionDenied
5from django import oldforms, template
6from django.shortcuts import render_to_response, get_object_or_404
7from django.http import HttpResponseRedirect
8from django.utils.html import escape
9
10def user_add_stage(request):
11 if not request.user.has_perm('auth.change_user'):
12 raise PermissionDenied
13 manipulator = UserCreationForm()
14 if request.method == 'POST':
15 new_data = request.POST.copy()
16 errors = manipulator.get_validation_errors(new_data)
17 if not errors:
18 new_user = manipulator.save(new_data)
19 msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': 'user', 'obj': new_user}
20 if request.POST.has_key("_addanother"):
21 request.user.message_set.create(message=msg)
22 return HttpResponseRedirect(request.path)
23 else:
24 request.user.message_set.create(message=msg + ' ' + _("You may edit it again below."))
25 return HttpResponseRedirect('../%s/' % new_user.id)
26 else:
27 errors = new_data = {}
28 form = oldforms.FormWrapper(manipulator, new_data, errors)
29 return render_to_response('admin/auth/user/add_form.html', {
30 'title': _('Add user'),
31 'form': form,
32 'is_popup': request.REQUEST.has_key('_popup'),
33 'add': True,
34 'change': False,
35 'has_delete_permission': False,
36 'has_change_permission': True,
37 'has_file_field': False,
38 'has_absolute_url': False,
39 'auto_populated_fields': (),
40 'bound_field_sets': (),
41 'first_form_field_id': 'id_username',
42 'opts': User._meta,
43 'username_help_text': User._meta.get_field('username').help_text,
44 }, context_instance=template.RequestContext(request))
45user_add_stage = staff_member_required(user_add_stage)
46
47def 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': request.REQUEST.has_key('_popup'),
67 'add': True,
68 'change': False,
69 'has_delete_permission': False,
70 'has_change_permission': True,
71 'has_absolute_url': False,
72 'first_form_field_id': 'id_password1',
73 'opts': User._meta,
74 'original': user,
75 'show_save': True,
76 }, context_instance=template.RequestContext(request))
77user_change_password = staff_member_required(user_change_password)
Back to Top