/
votehandler.py
83 lines (77 loc) · 3.74 KB
/
votehandler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Copyright 2012 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from basehandler import BaseHandler
from channelapihelper import ChannelApiHelper
import constants
from google.appengine.api import users
from model import Vote, Candidate, Election
from webapp2_extras.appengine.users import login_required
from webob.exc import HTTPUnauthorized, HTTPBadRequest
class VoteHandler(BaseHandler):
@login_required
def get(self, election_id, candidate_id):
election, candidate = self.ValidateElectionAndCandidate(election_id,
candidate_id)
message = ""
canvote = True
# TODO: All this is ugly, and needs to be a) refactored and b) not rely
# on a poorly named method that returns strings representing the election
# state.
election_active_state = election.CheckStartEndTime()
if election_active_state == constants.NOT_STARTED:
message = "This election has not started yet."
canvote = False
elif election_active_state == constants.ENDED:
message = "This election has ended."
canvote = False
if election.HasAlreadyVoted(users.get_current_user()):
message = "You've already voted in this election."
canvote = False
self.render_template("vote.html", title=election.title,
name=candidate.name, message=message,
canvote=canvote, show_ads=election.ads_enabled)
def post(self, election_id, candidate_id):
if self.request.get("cancel_button") == "True":
self.render_template("vote.html", message="Your vote has been discarded")
return
election, candidate = self.ValidateElectionAndCandidate(election_id,
candidate_id)
current_user = users.get_current_user()
if current_user is None:
raise HTTPUnauthorized("You must be logged in to vote.")
if election.HasAlreadyVoted(current_user):
raise HTTPUnauthorized("You've already voted in this election.")
election_active_state = election.CheckStartEndTime()
if election_active_state == constants.NOT_STARTED:
raise HTTPBadRequest("This election has not started yet.")
elif election_active_state == constants.ENDED:
raise HTTPBadRequest("This election has ended.")
voter_id = election.GenerateVoterId(current_user)
Vote(parent=candidate, voter=voter_id, election=str(election.key())).put()
self.NotifyChannels(election)
self.render_template("vote.html", canvote=False,
message="Thanks! Your vote for %s was registered." % candidate.name)
def NotifyChannels(self, election):
message = election.GetElectionStateAsJson()
ChannelApiHelper(election).NotifyChannels(message)
def ValidateElectionAndCandidate(self, election_id, candidate_id):
election = Election.get_by_id(long(election_id))
if election is None:
raise HTTPBadRequest("Couldn't find election with id '%s'." %
election_id)
candidate = Candidate.get_by_id(long(candidate_id), parent=election)
if candidate is None:
raise HTTPBadRequest("Couldn't find candidate with id '%s' "
"in election id '%s'." % (candidate_id, election_id))
return election, candidate