def edit(request, username): """ Respond to an "/edit/XXX" URL. We let an admin user edit the given user's details. """ if not request.user.is_superuser: return HttpResponseRedirect(reverse(settings.ADMIN_HOME_VIEW)) try: user = User.objects.get(username=username) except User.DoesNotExist: raise Http404 if request.method == "GET": form = EditUserForm({'username' : user.username}) errMsg = None elif request.method == "POST": if request.POST.get("cancel") == "Cancel": return HttpResponseRedirect(reverse("user_editor.views.main")) form = EditUserForm(request.POST) errMsg = None # initially. if form.is_valid(): username = request.POST['username'] password1 = request.POST['password1'] password2 = request.POST['password2'] if username == "": errMsg = "You must enter a username." elif password1 != "" and password1 != password2: errMsg = "The entered password doesn't match." elif username != user.username: if User.objects.filter(username=username).count() > 0: errMsg = "There is already a user with that username." if errMsg == None: user.username = username if password1 != "": user.set_password(password1) user.save() return HttpResponseRedirect(reverse("user_editor.views.main")) # If we get here, display our form. return render_to_response("shared/templates/editForm.html", {'title' : "Whenua Admin", 'heading' : "Edit User", 'action' : reverse("user_editor.views.edit", args=[source.code]), 'errMsg' : errMsg, 'form' : form}, context_instance=RequestContext(request))
def add(request): """ Respond to a "/add" URL. We let an admin user add a new ordinary user. """ if not request.user.is_superuser: return HttpResponseRedirect(reverse(settings.ADMIN_HOME_VIEW)) if request.method == "GET": form = EditUserForm() errMsg = None elif request.method == "POST": if request.POST.get("cancel") == "Cancel": return HttpResponseRedirect(reverse("user_editor.views.main")) form = EditUserForm(request.POST) errMsg = None # initially. if form.is_valid(): username = request.POST['username'] password1 = request.POST['password1'] password2 = request.POST['password2'] if username == "": errMsg = "You must enter a username." elif password1 == "": errMsg = "You must enter a password." elif password1 != password2: errMsg = "The entered password doesn't match." elif User.objects.filter(username=username).count() > 0: errMsg = "There is already a user with that username." if errMsg == None: user = User.objects.create_user(username=username, email="", password=password1) user.save() return HttpResponseRedirect(reverse("user_editor.views.main")) # If we get here, display our form. return render_to_response("shared/templates/editForm.html", {'title' : "Whenua Admin", 'heading' : "Add User", 'action' : reverse("user_editor.views.add"), 'errMsg' : errMsg, 'form' : form}, context_instance=RequestContext(request))