Skip to content
Snippets Groups Projects
Commit ca54abef authored by Ben Adida's avatar Ben Adida
Browse files

added tinyhash implementation for simple URLs to each vote

parent 0f454a46
Branches
No related tags found
No related merge requests found
......@@ -683,6 +683,33 @@ class CastVote(models.Model, electionalgs.CastVote):
def voter_hash(self):
return self.voter.hash
def set_tinyhash(self):
"""
find a tiny version of the hash for a URL slug.
"""
safe_hash = self.vote_hash
for c in ['/', '+']:
safe_hash = safe_hash.replace(c,'')
length = 8
while True:
vote_tinyhash = safe_hash[:length]
if CastVote.objects.filter(vote_tinyhash = vote_tinyhash).count() == 0:
break
length += 1
self.vote_tinyhash = vote_tinyhash
def save(self, *args, **kwargs):
"""
override this just to get a hook
"""
# not saved yet? then we generate a tiny hash
if not self.vote_tinyhash:
self.set_tinyhash()
super(CastVote, self).save(*args, **kwargs)
@classmethod
def get_by_voter(cls, voter):
return cls.objects.filter(voter = voter).order_by('-cast_at')
......
{% extends TEMPLATE_BASE %}
{% block title %}{{cast_vote.vote_tinyhash}} — {{election.name}}{% endblock %}
{% block content %}
<h2 class="title">Cast Vote {{cast_vote.vote_tinyhash}}</h2>
cast in <a href="{% url helios.views.one_election_view election.uuid %}">{{election.name}}</a><br />
by {{voter.name}}
<br /><br />
{% endblock %}
......@@ -18,6 +18,9 @@ urlpatterns = patterns('',
# election shortcut by shortname
(r'^e/(?P<election_short_name>[^/]+)$', election_shortcut),
# vote shortcut
(r'^v/(?P<vote_tinyhash>[^/]+)$', castvote_shortcut),
# trustee login
(r'^t/(?P<election_short_name>[^/]+)/(?P<trustee_email>[^/]+)/(?P<trustee_secret>[^/]+)$', trustee_login),
......
......@@ -99,6 +99,15 @@ def election_shortcut(request, election_short_name):
else:
raise Http404
def castvote_shortcut(request, vote_tinyhash):
try:
cast_vote = CastVote.objects.get(vote_tinyhash = vote_tinyhash)
except CastVote.DoesNotExist:
raise Http404
# FIXME: consider privacy of election
return render_template(request, 'castvote', {'cast_vote' : cast_vote, 'voter': cast_vote.voter, 'election': cast_vote.voter.election})
@trustee_check
def trustee_keygenerator(request, election, trustee):
"""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment