Skip to content

Commit

Permalink
a number of fixes for voting start and end times
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Adida authored and Ben Adida committed Nov 30, 2009
1 parent 58dbb34 commit 609753a
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 33 deletions.
4 changes: 2 additions & 2 deletions __init__.py
Expand Up @@ -16,5 +16,5 @@

# get the short path for the URL
def get_election_url(election):
from views import one_election_view
return settings.URL_HOST + reverse(one_election_view, args=[election.uuid])
from views import election_shortcut
return settings.URL_HOST + reverse(election_shortcut, args=[election.short_name])
27 changes: 19 additions & 8 deletions models.py
Expand Up @@ -32,11 +32,13 @@
# a counter incrementing function that is meant to run in a transaction
def __increment_counter(key, counter_name, amount):
obj = db.get(key)
setattr(obj, counter_name, getattr(obj, counter_name) + amount)
new_value = getattr(obj, counter_name) + amount
setattr(obj, counter_name, new_value)
obj.put()
return new_value

def increment_counter(key, counter_name, amount = 1):
db.run_in_transaction(__increment_counter, key, counter_name, amount)
return db.run_in_transaction(__increment_counter, key, counter_name, amount)

class Election(db.Model, electionalgs.Election):
admin = db.ReferenceProperty(User)
Expand Down Expand Up @@ -135,7 +137,12 @@ def get_by_uuid(cls, uuid):
def get_by_short_name(cls, short_name):
query = cls.all()
query.filter('short_name = ', short_name)
return query.fetch(1)[0]
elections = query.fetch(1)

if len(elections) > 0:
return elections[0]
else:
return None

def user_eligible_p(self, user):
"""
Expand Down Expand Up @@ -195,16 +202,16 @@ def generate_voters_hash(self):
self.voters_hash = utils.hash_b64(voters_json)

def increment_voters(self):
increment_counter(self.key(), 'num_voters')

# increment global counter
counters.increment(GLOBAL_COUNTER_VOTERS)

return increment_counter(self.key(), 'num_voters')

def increment_cast_votes(self):
increment_counter(self.key(), 'num_cast_votes')

# increment global counter
counters.increment(GLOBAL_COUNTER_CAST_VOTES)

return increment_counter(self.key(), 'num_cast_votes')

def put(self, *args, **kwargs):
"""
Expand Down Expand Up @@ -338,7 +345,11 @@ def put(self, *args, **kwargs):

# do the increment afterwards in case of an exception which prevents the creation
if increment_p:
self.election.increment_voters()
new_num_voters = self.election.increment_voters()
if self.election.use_voter_aliases:
self.alias = 'V' + str(new_num_voters)
# recursive only once, at least it should be
self.save()

def store_vote(self, cast_vote):
cast_vote.save()
Expand Down
2 changes: 1 addition & 1 deletion signals.py
Expand Up @@ -10,7 +10,7 @@
election_created = django.dispatch.Signal(providing_args=["election"])

# when a vote is cast
vote_cast = django.dispatch.Signal(providing_args=["user", "election", "cast_vote"])
vote_cast = django.dispatch.Signal(providing_args=["user", "voter", "election", "cast_vote"])

# when an election is tallied
election_tallied = django.dispatch.Signal(providing_args=["election"])
18 changes: 16 additions & 2 deletions templates/election_cast_confirm.html
Expand Up @@ -46,9 +46,13 @@ <h3>Past Votes Cast for this Election</h3>
</ul>
-->
{% else %}
<em>no vote cast yet</em>
<!--<em>no vote cast yet</em>-->
{% endif %}
<br /><br />

<br />
{% if election.voting_has_started %}
{% if not election.voting_has_stopped %}
<br />
<div id="cast_form">
<form method="post" action="" onsubmit="show_waiting()">
<input type="hidden" name="csrf_token" value="{{csrf_token}}" />
Expand All @@ -67,6 +71,16 @@ <h3>Past Votes Cast for this Election</h3>
<img src="/static/helios/loading.gif" />
</div>
{% else %}
<p style="font-size:1.4em;">
voting has stopped, sorry.
</p>
{% endif %}
{% else %}
<p style="font-size:1.4em;">
voting has not yet begun, sorry.
</p>
{% endif %}
{% else %}
<p>
{% if election.openreg %}
<b>Sorry, you are <em><u>not eligible</u></em> for this election.</b><br />
Expand Down
30 changes: 21 additions & 9 deletions templates/election_view.html
Expand Up @@ -18,8 +18,10 @@
<ul>
{% if election.voting_has_started %}
{% else %}
{% if not election.frozen_at %}
<li> <a href="edit">election parameters</a></li>
{% endif %}
{% endif %}
<li> Registration
<b>{{ election.registration_status_pretty }}</b>
{% if not election.frozen_at %}<br />
Expand Down Expand Up @@ -90,10 +92,9 @@ <h2 class="title">{{ election.name }}</h2>
</div>

{% if election.result %}
<div class="highlight-box">
This election is complete. The tally is just below.
Check the <a href="#audit">auditing information</a>.
</div>
<span class="highlight-box round">
This election is complete. Tally and Audit info below.
</span><br />

<h3>Tally</h3>
{% for question in election.pretty_result %}
Expand All @@ -108,20 +109,25 @@ <h3>Tally</h3>
{% else %}

{% if election.voting_has_stopped %}
<div class="highlight-box">
This election is closed. The tally will be computed soon.
</div>
<span class="highlight-box round">
Election closed. Tally will be computed soon.
</span><br />
{% else %}

{% if election.voting_has_started %}
<span class="highlight-box round" style="font-size: 1.6em; margin-right: 10px;">
<a href="/booth/vote.html?election_url={% url helios.views.one_election election.uuid %}">Vote in this election </a>
</span><br /><br />
{% else %}
<span class="highlight-box round">
voting is not yet open
</span> <br /><br />
{% endif %}

{% if user %}
{% if voter %}
<p style="padding-top:5px;">You are registered to vote in this election.
<p style="padding-top:5px;">
You are registered to vote in this election.
{% if election.use_voter_aliases %}
Your voter alias is {{voter.alias}}.
{% endif %}
Expand Down Expand Up @@ -189,7 +195,13 @@ <h3>Tally</h3>
{% endfor %}
[<a href="{% url helios.views.list_trustees_view election.uuid %}">more info</a>]
</p>


{% if not election.voting_has_started %}
<p style="font-size: 1.2em;">
<a href="/booth/vote.html?election_url={% url helios.views.one_election election.uuid %}">preview booth</a>
</p>
{% endif %}

{% endif %}

{% if election.result %}
Expand Down
3 changes: 3 additions & 0 deletions urls.py
Expand Up @@ -11,6 +11,9 @@
(r'^$', home),
(r'^about$', about),

# election shortcut by shortname
(r'^e/(?P<election_short_name>[^/]+)$', election_shortcut),

# election
(r'^elections/params$', election_params),
(r'^elections/verifier$', election_verifier),
Expand Down
24 changes: 13 additions & 11 deletions views.py
Expand Up @@ -81,6 +81,13 @@ def election_verifier(request):
def election_single_ballot_verifier(request):
return render_template(request, "ballot_verifier")

def election_shortcut(request, election_short_name):
election = Election.get_by_short_name(election_short_name)
if election:
return HttpResponseRedirect(reverse(one_election_view, args=[election.uuid]))
else:
return Http404

@election_view()
def election_keygenerator(request, election):
"""
Expand Down Expand Up @@ -307,6 +314,10 @@ def one_election_cast_confirm(request, election):
if request.method == "POST":
check_csrf(request)

# voting has not started or has ended
if (not election.voting_has_started()) or election.voting_has_stopped():
return HttpResponseRedirect("/")

# if user is not logged in
# bring back to the confirmation page to let him know
if not user or not voter:
Expand All @@ -317,13 +328,13 @@ def one_election_cast_confirm(request, election):
# store it
voter.store_vote(cast_vote)
else:
return HttpResponse("vote does not verify: " + utils.to_json(cast_vote.vote.toJSONDict()))
return HttpResponse("vote does not verify: \n\n" + utils.to_json(cast_vote.vote.toJSONDict()))

# remove the vote from the store
del request.session['encrypted_vote']

# send the signal
signals.vote_cast.send(sender=election, election=election, user=user, cast_vote=cast_vote)
signals.vote_cast.send(sender=election, election=election, user=user, voter=voter, cast_vote=cast_vote)

return HttpResponseRedirect(reverse(one_election_cast_done, args=[election.uuid]))

Expand Down Expand Up @@ -431,9 +442,6 @@ def _register_voter(election, user):
voter_uuid = str(uuid.uuid1())
voter = Voter(uuid= voter_uuid, voter_type = user.user_type, voter_id = user.user_id, election = election)

if election.use_voter_aliases:
voter.alias = "V" + str(election.num_voters+1)

voter.put()
return voter

Expand Down Expand Up @@ -593,8 +601,6 @@ def voters_upload(request, election):
voters_csv_lines = request.POST['voters_csv'].split("\n")
reader = csv.reader(voters_csv_lines)

voter_alias = election.num_voters + 1

for voter in reader:

# bad line
Expand All @@ -621,10 +627,6 @@ def voters_upload(request, election):
voter_uuid = str(uuid.uuid1())
voter = Voter(uuid= voter_uuid, voter_type = voter_type, voter_id = voter_id, name = name, election = election)

if election.use_voter_aliases:
voter.alias = "V" + str(voter_alias)
voter_alias += 1

voter.put()

return HttpResponseRedirect(reverse(voters_manage, args=[election.uuid]))
Expand Down

0 comments on commit 609753a

Please sign in to comment.