def main(request): """ Respond to the "/admin" URL. """ if admin_sessions.user_logged_in(request): return redirect("/admin/menu") else: return redirect("/admin/login")
def menu(request): """ Respond to the "/admin" URL. """ if not admin_sessions.user_logged_in(request): return redirect("/admin/login") return render(request, "menu.html")
def params(request): """ Respond to the "/admin/report_params" URL. """ if not admin_sessions.user_logged_in(request): return redirect("/admin/login") return render(request, "report_params.html")
def edit_user(request, user_id): """ Respond to the "/admin/edit_user """ if not admin_sessions.user_logged_in(request): return redirect("/admin/login") return HttpResponse("More to come...")
def logout(request): """ Respond to the "/admin/logout" URL. """ if not admin_sessions.user_logged_in(request): return redirect("/admin/login") session_token = request.COOKIES["mm_admin"] admin_sessions.delete(session_token) return redirect("/admin/login")
def login(request): """ Respond to the "/admin/login" URL. """ if admin_sessions.user_logged_in(request): return redirect("/admin/menu") if request.method == "POST": password = request.POST["password"] else: password = None if password != None: if hashlib.md5(password.lower()).hexdigest() == PASSWORD_DIGEST: session_id = admin_sessions.create() response = redirect("/admin/menu") response.set_cookie("mm_admin", session_id) return response else: err_msg = "incorrect password" else: err_msg = None return render(request, "login.html", {"err_msg": err_msg})
def user_list(request): """ Respond to the "/admin/user_list" URL. """ if not admin_sessions.user_logged_in(request): return redirect("/admin/login") if request.method == "GET": params = request.GET elif request.method == "POST": params = request.POST else: raise RuntimeError("Unknown request.method: " + repr(request.method)) user_list = User.objects.order_by("username", "phone_number") paginator = Paginator(user_list, 10) # Show 10 users per page. page_num = params.get("page", 0) try: page = paginator.page(page_num) except (PageNotAnInteger, EmptyPage): # Invalid page -> show the first page. page = paginator.page(1) if request.method == "GET": # We're displaying the page for the first time. Process our CGI # parameters, if any. confirm = request.GET.get("confirm") elif request.method == "POST": # Respond to the user clicking on one of our buttons. # Did the user click on one of our "Edit" buttons? We redirect the # user to the "Edit" page for the associated user. for user in user_list: editValue = request.POST.get("edit-" + str(user.id)) if editValue == "Edit": return redirect("/admin/edit_user/" + str(user.id)) # Did the user click on one of our "Delete" buttons? We firstly # display the confirmation button beside the entry, and only delete the # entry if the user confirms. for user in user_list: deleteValue = request.POST.get("del-" + str(user.id)) if deleteValue == "Delete": # The user clicked on the "Delete" button for the first time -> # redisplay the page with the confirmation buttons. return redirect("/admin/user_list?page=" + str(page_num) + "&confirm=" + str(user.id)) elif deleteValue == "Yes": # The user clicked on our "Yes" confirmation button. Delete # this user and redisplay the page. user.delete() return redirect("/admin/user_list?page=" + str(page_num)) elif deleteValue == "No": # The user clicked on the "No" confirmation button. Redisplay # the page without the confirmation buttons. return redirect("/admin/user_list?page=" + str(page_num)) # If we get here, we're going to display the page again. Grab our # "confirm" CGI parameter so the page can display the appropriate # confirmation buttons. confirm = request.POST.get("confirm") return render(request, "user_list.html", {'page' : page, 'confirm' : confirm})