def login(): # if user is logged in already, just send them to the home page if loggedIn(): return redirect("/") # validate POST data error = None result = False if not request.form["username"]: error = "Username must not be left blank." elif not request.form["password"]: error = "Password must not be left blank." else: result = tryLogin(request.form) if result: # valid login # get and setup various session data setupSession(request.form["username"]) curElection = getCurElection() # get today's election if curElection: voted = votedAlready(curElection, session["id"]) else: voted = False return render_template("index.html", logged_in=True, election_happening=curElection, voted=voted) else: # failed login if not error: error = "Invalid username/password combination. Try again." return render_template("index.html", error=error, logged_in=False)
def login(): #if user is logged in already, just send them to the home page if loggedIn(): return redirect("/") #validate POST data error = None result = False if not request.form["username"]: error = "Username must not be left blank." elif not request.form["password"]: error = "Password must not be left blank." else: result = tryLogin(request.form) if result: #valid login #get and setup various session data setupSession(request.form["username"]) curElection = getCurElection() #get today's election if curElection: voted = votedAlready(curElection, session["id"]) else: voted = False return render_template("index.html", logged_in=True, election_happening=curElection, voted=voted) else: #failed login if not error: error = "Invalid username/password combination. Try again." return render_template("index.html", error=error, logged_in=False)
def vote_page(): if not loggedIn(): #not logged in, make em register return redirect("/register") #setup voted variable voted = False if request.method == "GET": curElection = getCurElection() if curElection: voted = votedAlready(curElection, session["id"]) if not voted: #didn't vote yet candidates = getCandidates(curElection) return render_template("vote.html", logged_in=True, election_happening=True, listLen=len(candidates), ticket=candidates, voted=False) else: #already voted return render_template("vote.html", logged_in=True, election_happening=True, voted=True) elif request.method == "POST": #user voted, now we need to process the data if there's an election today curElection = getCurElection() #when this if statement is true, the election being voted in today is valid if curElection: voted = votedAlready(curElection, session["id"]) if not voted: #make sure they didn't vote yet candidates = getCandidates(curElection) error = None result = False candidate_id = request.form["candidate"] #user should also put their password in to vote data = {"username" : session["username"], "password" : request.form["password"]} if not tryLogin(data): error = "Invalid password." elif not validCandidateID(curElection, candidate_id): error = "Invalid candidate ID given. Voter fraud detected - not counting vote." voted = True else: result = vote(curElection, candidate_id, userid=session["id"]) if result: #vote is valid return redirect("/") else: #vote is invalid if not error: error = "There was a problem with your vote. Please try again." return render_template("vote.html", logged_in=True, error=error, voted=voted, election_happening=True, ticket=candidates, listLen=len(candidates)) #there is no election today or they already voted return render_template("vote.html", logged_in=True, election_happening=curElection, voted=voted)