2006-09-08 13:38:38 +08:00
|
|
|
from django.contrib.admin.views.decorators import staff_member_required
|
2006-12-30 15:16:25 +08:00
|
|
|
from django.contrib.auth.forms import UserCreationForm, AdminPasswordChangeForm
|
2006-08-04 12:18:12 +08:00
|
|
|
from django.contrib.auth.models import User
|
2006-10-25 00:46:46 +08:00
|
|
|
from django.core.exceptions import PermissionDenied
|
2006-12-16 02:00:50 +08:00
|
|
|
from django import oldforms, template
|
2006-12-30 15:16:25 +08:00
|
|
|
from django.shortcuts import render_to_response, get_object_or_404
|
2006-08-04 12:18:12 +08:00
|
|
|
from django.http import HttpResponseRedirect
|
2006-12-30 15:16:25 +08:00
|
|
|
from django.utils.html import escape
|
2006-08-04 12:18:12 +08:00
|
|
|
|
|
|
|
def user_add_stage(request):
|
2006-09-08 13:38:38 +08:00
|
|
|
if not request.user.has_perm('auth.change_user'):
|
|
|
|
raise PermissionDenied
|
2006-08-04 12:18:12 +08:00
|
|
|
manipulator = UserCreationForm()
|
|
|
|
if request.method == 'POST':
|
|
|
|
new_data = request.POST.copy()
|
|
|
|
errors = manipulator.get_validation_errors(new_data)
|
|
|
|
if not errors:
|
|
|
|
new_user = manipulator.save(new_data)
|
|
|
|
msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': 'user', 'obj': new_user}
|
2007-04-26 21:30:48 +08:00
|
|
|
if "_addanother" in request.POST:
|
2006-08-04 12:18:12 +08:00
|
|
|
request.user.message_set.create(message=msg)
|
|
|
|
return HttpResponseRedirect(request.path)
|
|
|
|
else:
|
|
|
|
request.user.message_set.create(message=msg + ' ' + _("You may edit it again below."))
|
|
|
|
return HttpResponseRedirect('../%s/' % new_user.id)
|
|
|
|
else:
|
|
|
|
errors = new_data = {}
|
2006-12-16 02:00:50 +08:00
|
|
|
form = oldforms.FormWrapper(manipulator, new_data, errors)
|
2006-08-04 12:18:12 +08:00
|
|
|
return render_to_response('admin/auth/user/add_form.html', {
|
|
|
|
'title': _('Add user'),
|
|
|
|
'form': form,
|
2007-04-26 21:30:48 +08:00
|
|
|
'is_popup': '_popup' in request.REQUEST,
|
2006-08-04 12:18:12 +08:00
|
|
|
'add': True,
|
|
|
|
'change': False,
|
|
|
|
'has_delete_permission': False,
|
|
|
|
'has_change_permission': True,
|
|
|
|
'has_file_field': False,
|
|
|
|
'has_absolute_url': False,
|
|
|
|
'auto_populated_fields': (),
|
|
|
|
'bound_field_sets': (),
|
|
|
|
'first_form_field_id': 'id_username',
|
|
|
|
'opts': User._meta,
|
|
|
|
'username_help_text': User._meta.get_field('username').help_text,
|
|
|
|
}, context_instance=template.RequestContext(request))
|
2006-09-08 13:38:38 +08:00
|
|
|
user_add_stage = staff_member_required(user_add_stage)
|
2006-12-30 15:16:25 +08:00
|
|
|
|
|
|
|
def user_change_password(request, id):
|
|
|
|
if not request.user.has_perm('auth.change_user'):
|
|
|
|
raise PermissionDenied
|
|
|
|
user = get_object_or_404(User, pk=id)
|
|
|
|
manipulator = AdminPasswordChangeForm(user)
|
|
|
|
if request.method == 'POST':
|
|
|
|
new_data = request.POST.copy()
|
|
|
|
errors = manipulator.get_validation_errors(new_data)
|
|
|
|
if not errors:
|
|
|
|
new_user = manipulator.save(new_data)
|
|
|
|
msg = _('Password changed successfully.')
|
|
|
|
request.user.message_set.create(message=msg)
|
|
|
|
return HttpResponseRedirect('..')
|
|
|
|
else:
|
|
|
|
errors = new_data = {}
|
|
|
|
form = oldforms.FormWrapper(manipulator, new_data, errors)
|
|
|
|
return render_to_response('admin/auth/user/change_password.html', {
|
|
|
|
'title': _('Change password: %s') % escape(user.username),
|
|
|
|
'form': form,
|
2007-04-26 21:30:48 +08:00
|
|
|
'is_popup': '_popup' in request.REQUEST,
|
2006-12-30 15:16:25 +08:00
|
|
|
'add': True,
|
|
|
|
'change': False,
|
|
|
|
'has_delete_permission': False,
|
|
|
|
'has_change_permission': True,
|
|
|
|
'has_absolute_url': False,
|
|
|
|
'first_form_field_id': 'id_password1',
|
|
|
|
'opts': User._meta,
|
|
|
|
'original': user,
|
|
|
|
'show_save': True,
|
|
|
|
}, context_instance=template.RequestContext(request))
|
|
|
|
user_change_password = staff_member_required(user_change_password)
|