Ejemplo n.º 1
0
def edit_admin(request, username):
    """ Respond to the "/superuser/edit_admin" URL.

        We let the user edit the given administrator.
    """
    if not request.user.is_superuser:
        return HttpResponseRedirect(reverse("tahua.admin_interface.views." +
                                            "main.main"))

    try:
        admin = Admin.objects.get(username=username)
    except Admin.DoesNotExist:
        raise Http404

    if request.method == "GET":
        form   = EditAdminForm({'username' : admin.username})
        errMsg = None
    elif request.method == "POST":
        if request.POST.get("cancel") == "Cancel":
            return HttpResponseRedirect(reverse("tahua.admin_interface." +
                                                "views.superuser.list_admins"))

        form = EditAdminForm(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 != admin.username:
                if Admin.objects.filter(username=username).count() > 0:
                    errMsg = "There is already an administrator with that " \
                           + "username."

            if errMsg == None:
                admin.username = username
                if password1 != "": admin.set_password(password1)
                admin.save()

                return HttpResponseRedirect(reverse("tahua.admin_interface." +
                                                    "views.superuser." +
                                                    "list_admins"))

    # If we get here, display our form.

    return render_to_response("shared/editForm.html",
                              {'title'   : "Tahua Admin",
                               'heading' : "Edit Administrator",
                               'errMsg'  : errMsg,
                               'form'    : form},
                              context_instance=RequestContext(request))
Ejemplo n.º 2
0
def add_admin(request):
    """ Respond to the "/superuser/add_admin" URL.

        We let the superuser add a new (regular) administrator to the system.
    """
    if not request.user.is_superuser:
        return HttpResponseRedirect(reverse("tahua.admin_interface.views." +
                                            "main.main"))

    if request.method == "GET":
        form   = EditAdminForm()
        errMsg = None
    elif request.method == "POST":
        if request.POST.get("cancel") == "Cancel":
            return HttpResponseRedirect(reverse("tahua.admin_interface." +
                                                "views.superuser.list_admins"))

        form = EditAdminForm(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 Admin.objects.filter(username=username).count() > 0:
                errMsg = "There is already an administrator with that " \
                       + "username."

            if errMsg == None:
                admin = Admin.objects.create_user(username=username,
                                                  email="",
                                                  password=password1)
                admin.save()

                return HttpResponseRedirect(reverse("tahua.admin_interface." +
                                                    "views.superuser." +
                                                    "list_admins"))

    # If we get here, display our form.

    return render_to_response("shared/editForm.html",
                              {'title'   : "Tahua Admin",
                               'heading' : "Add Administrator",
                               'errMsg'  : errMsg,
                               'form'    : form},
                              context_instance=RequestContext(request))