diff --git a/helios/models.py b/helios/models.py index a925c0c1d3683ee5bcd3880801bea79a3995e870..953c3978dc29f00c0e72b39664ef69448dcc5b5a 100644 --- a/helios/models.py +++ b/helios/models.py @@ -682,6 +682,33 @@ class CastVote(models.Model, electionalgs.CastVote): @property 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): diff --git a/helios/templates/castvote.html b/helios/templates/castvote.html new file mode 100644 index 0000000000000000000000000000000000000000..42e7894b1179423c9b319f48a97e7bb33ec53c67 --- /dev/null +++ b/helios/templates/castvote.html @@ -0,0 +1,13 @@ +{% 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 %} diff --git a/helios/urls.py b/helios/urls.py index 5c49d3b7ed08f95a2458d4075edca608bab6cab2..e83f4d419019240b7657e87d49a43c5ef4c5d2a3 100644 --- a/helios/urls.py +++ b/helios/urls.py @@ -17,6 +17,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), diff --git a/helios/views.py b/helios/views.py index 9571a5e6ba6113ce15a2ab51e833124721ebe976..223cec80ca08e663c198d46740be3edded1992cd 100644 --- a/helios/views.py +++ b/helios/views.py @@ -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): """