From 94cd4ae362ab82aad942326e38cc30102f590047 Mon Sep 17 00:00:00 2001
From: Marco Ciotola <848222@stud.unive.it>
Date: Thu, 7 Mar 2019 19:31:03 +0100
Subject: [PATCH] [DJ1.10] Update helios.urls to have names and be reversed by
 name

---
 helios/templates/voters_list.html |  2 +-
 helios/url_names.py               | 16 ++++++++++++++++
 helios/urls.py                    | 32 ++++++++++++++++---------------
 helios/views.py                   | 20 +++++++++----------
 server_ui/templates/index.html    | 10 +++++-----
 5 files changed, 49 insertions(+), 31 deletions(-)
 create mode 100644 helios/url_names.py

diff --git a/helios/templates/voters_list.html b/helios/templates/voters_list.html
index 9c1f611..3b345a0 100644
--- a/helios/templates/voters_list.html
+++ b/helios/templates/voters_list.html
@@ -143,7 +143,7 @@ Voters {{voters_page.start_index}} - {{voters_page.end_index}} (of {{total_voter
 {% if election.use_voter_aliases %}
 <td>{{voter.alias}}</td>
 {% endif %}
-<td><tt style="font-size: 1.4em;">{% if voter.vote_hash %}{{voter.vote_hash}} <span style="font-size:0.8em;">[<a href="{% url "helios.views.castvote_shortcut" vote_tinyhash=voter.vote_tinyhash %}">view</a>]</span>{% else %}&mdash;{% endif %}</tt></td>
+<td><tt style="font-size: 1.4em;">{% if voter.vote_hash %}{{voter.vote_hash}} <span style="font-size:0.8em;">[<a href="{% url "shortcut@vote" vote_tinyhash=voter.vote_tinyhash %}">view</a>]</span>{% else %}&mdash;{% endif %}</tt></td>
 </tr>
 {% endfor %}
 </table>
diff --git a/helios/url_names.py b/helios/url_names.py
new file mode 100644
index 0000000..96b0eb1
--- /dev/null
+++ b/helios/url_names.py
@@ -0,0 +1,16 @@
+COOKIE_TEST="cookie@test"
+COOKIE_TEST_2="cookie@test2"
+COOKIE_NO="cookie@no"
+
+ELECTION_SHORTCUT="shortcut@election"
+ELECTION_SHORTCUT_VOTE="shortcut@election@vote"
+CAST_VOTE_SHORTCUT="shortcut@vote"
+
+TRUSTEE_LOGIN="trustee@login"
+
+ELECTIONS_PARAMS="elections@params"
+ELECTIONS_VERIFIER="elections@verifier"
+ELECTIONS_VERIFIER_SINGLE_BALLOT="elections@verifier@single-ballot"
+ELECTIONS_NEW="elections@new"
+ELECTIONS_ADMINISTERED="elections@administered"
+ELECTIONS_VOTED="elections@voted"
diff --git a/helios/urls.py b/helios/urls.py
index 91762ab..6d278a9 100644
--- a/helios/urls.py
+++ b/helios/urls.py
@@ -1,32 +1,34 @@
 # -*- coding: utf-8 -*-
 from django.conf.urls import url, include
 
-from views import *
+import url_names as names
+import views
 
 urlpatterns = [
-  url(r'^autologin$', admin_autologin),
-  url(r'^testcookie$', test_cookie),
-  url(r'^testcookie_2$', test_cookie_2),
-  url(r'^nocookies$', nocookies),
+  url(r'^autologin$', views.admin_autologin),
+  url(r'^testcookie$', views.test_cookie, name=names.COOKIE_TEST),
+  url(r'^testcookie_2$', views.test_cookie_2, name=names.COOKIE_TEST_2),
+  url(r'^nocookies$', views.nocookies, name=names.COOKIE_NO),
   url(r'^stats/', include('helios.stats_urls')),
 
   # election shortcut by shortname
-  url(r'^e/(?P<election_short_name>[^/]+)$', election_shortcut),
-  url(r'^e/(?P<election_short_name>[^/]+)/vote$', election_vote_shortcut),
+  url(r'^e/(?P<election_short_name>[^/]+)$', views.election_shortcut, name=names.ELECTION_SHORTCUT),
+  url(r'^e/(?P<election_short_name>[^/]+)/vote$', views.election_vote_shortcut, name=names.ELECTION_SHORTCUT_VOTE),
 
   # vote shortcut
-  url(r'^v/(?P<vote_tinyhash>[^/]+)$', castvote_shortcut),
+  url(r'^v/(?P<vote_tinyhash>[^/]+)$', views.castvote_shortcut, name=names.CAST_VOTE_SHORTCUT),
   
   # trustee login
-  url(r'^t/(?P<election_short_name>[^/]+)/(?P<trustee_email>[^/]+)/(?P<trustee_secret>[^/]+)$', trustee_login),
+  url(r'^t/(?P<election_short_name>[^/]+)/(?P<trustee_email>[^/]+)/(?P<trustee_secret>[^/]+)$', views.trustee_login,
+      name=names.TRUSTEE_LOGIN),
   
   # election
-  url(r'^elections/params$', election_params),
-  url(r'^elections/verifier$', election_verifier),
-  url(r'^elections/single_ballot_verifier$', election_single_ballot_verifier),
-  url(r'^elections/new$', election_new),
-  url(r'^elections/administered$', elections_administered),
-  url(r'^elections/voted$', elections_voted),
+  url(r'^elections/params$', views.election_params, name=names.ELECTIONS_PARAMS),
+  url(r'^elections/verifier$', views.election_verifier, name=names.ELECTIONS_VERIFIER),
+  url(r'^elections/single_ballot_verifier$', views.election_single_ballot_verifier, name=names.ELECTIONS_VERIFIER_SINGLE_BALLOT),
+  url(r'^elections/new$', views.election_new, name=names.ELECTIONS_NEW),
+  url(r'^elections/administered$', views.elections_administered, name=names.ELECTIONS_ADMINISTERED),
+  url(r'^elections/voted$', views.elections_voted, name=names.ELECTIONS_VOTED),
   
   url(r'^elections/(?P<election_uuid>[^/]+)', include('helios.election_urls')),
 ]
diff --git a/helios/views.py b/helios/views.py
index 4fa9812..1dfe9c7 100644
--- a/helios/views.py
+++ b/helios/views.py
@@ -19,7 +19,7 @@ import election_url_names as election_names
 from crypto import algs, electionalgs, elgamal
 from crypto import utils as cryptoutils
 from workflows import homomorphic
-from helios import utils, VOTERS_EMAIL, VOTERS_UPLOAD
+from helios import utils, VOTERS_EMAIL, VOTERS_UPLOAD, url_names
 from view_utils import SUCCESS, FAILURE, return_json, render_template, render_template_raw
 
 from helios_auth.security import check_csrf, login_required, get_user, save_in_session_across_logouts
@@ -58,16 +58,16 @@ ELGAMAL_PARAMS_LD_OBJECT = datatypes.LDObject.instantiate(ELGAMAL_PARAMS, dataty
 from django.conf import settings
 
 def get_election_url(election):
-  return settings.URL_HOST + reverse(election_shortcut, args=[election.short_name])
+  return settings.URL_HOST + reverse(url_names.ELECTION_SHORTCUT, args=[election.short_name])
 
 def get_election_badge_url(election):
   return settings.URL_HOST + reverse(election_names.ELECTION_BADGE, args=[election.uuid])
 
 def get_election_govote_url(election):
-  return settings.URL_HOST + reverse(election_vote_shortcut, args=[election.short_name])  
+  return settings.URL_HOST + reverse(url_names.ELECTION_SHORTCUT_VOTE, args=[election.short_name])
 
 def get_castvote_url(cast_vote):
-  return settings.URL_HOST + reverse(castvote_shortcut, args=[cast_vote.vote_tinyhash])
+  return settings.URL_HOST + reverse(url_names.CAST_VOTE_SHORTCUT, args=[cast_vote.vote_tinyhash])
 
 
 ##
@@ -126,7 +126,7 @@ def election_shortcut(request, election_short_name):
 def _election_vote_shortcut(request, election):
   vote_url = "%s/booth/vote.html?%s" % (settings.SECURE_URL_HOST, urllib.urlencode({'election_url' : reverse(election_names.ELECTION_HOME, args=[election.uuid])}))
   
-  test_cookie_url = "%s?%s" % (reverse(test_cookie), urllib.urlencode({'continue_url' : vote_url}))
+  test_cookie_url = "%s?%s" % (reverse(url_names.COOKIE_TEST), urllib.urlencode({'continue_url' : vote_url}))
 
   return HttpResponseRedirect(test_cookie_url)
   
@@ -306,7 +306,7 @@ def one_election_view(request, election):
 
   vote_url = "%s/booth/vote.html?%s" % (settings.SECURE_URL_HOST, urllib.urlencode({'election_url' : reverse(election_names.ELECTION_HOME, args=[election.uuid])}))
 
-  test_cookie_url = "%s?%s" % (reverse(test_cookie), urllib.urlencode({'continue_url' : vote_url}))
+  test_cookie_url = "%s?%s" % (reverse(url_names.COOKIE_TEST), urllib.urlencode({'continue_url' : vote_url}))
   
   if user:
     voter = Voter.get_by_election_and_user(election, user)
@@ -353,20 +353,20 @@ def one_election_view(request, election):
 def test_cookie(request):
   continue_url = request.GET['continue_url']
   request.session.set_test_cookie()
-  next_url = "%s?%s" % (reverse(test_cookie_2), urllib.urlencode({'continue_url': continue_url}))
+  next_url = "%s?%s" % (reverse(url_names.COOKIE_TEST_2), urllib.urlencode({'continue_url': continue_url}))
   return HttpResponseRedirect(settings.SECURE_URL_HOST + next_url)  
 
 def test_cookie_2(request):
   continue_url = request.GET['continue_url']
 
   if not request.session.test_cookie_worked():
-    return HttpResponseRedirect(settings.SECURE_URL_HOST + ("%s?%s" % (reverse(nocookies), urllib.urlencode({'continue_url': continue_url}))))
+    return HttpResponseRedirect(settings.SECURE_URL_HOST + ("%s?%s" % (reverse(url_names.COOKIE_NO), urllib.urlencode({'continue_url': continue_url}))))
 
   request.session.delete_test_cookie()
   return HttpResponseRedirect(continue_url)  
 
 def nocookies(request):
-  retest_url = "%s?%s" % (reverse(test_cookie), urllib.urlencode({'continue_url' : request.GET['continue_url']}))
+  retest_url = "%s?%s" % (reverse(url_names.COOKIE_TEST), urllib.urlencode({'continue_url' : request.GET['continue_url']}))
   return render_template(request, 'nocookies', {'retest_url': retest_url})
 
 ##
@@ -438,7 +438,7 @@ def trustee_login(request, election_short_name, trustee_email, trustee_secret):
 def trustee_send_url(request, election, trustee_uuid):
   trustee = Trustee.get_by_election_and_uuid(election, trustee_uuid)
   
-  url = settings.SECURE_URL_HOST + reverse(trustee_login, args=[election.short_name, trustee.email, trustee.secret])
+  url = settings.SECURE_URL_HOST + reverse(url_names.TRUSTEE_LOGIN, args=[election.short_name, trustee.email, trustee.secret])
   
   body = """
 
diff --git a/server_ui/templates/index.html b/server_ui/templates/index.html
index 6c2821e..4b1fcec 100644
--- a/server_ui/templates/index.html
+++ b/server_ui/templates/index.html
@@ -28,7 +28,7 @@ More than <b>2,000,000 votes</b> have been cast using Helios.
 </p>
 
 {% if create_p %}
-<a class="button" href="{% url "helios.views.election_new" %}">create an election</a>
+<a class="button" href="{% url "elections@new" %}">create an election</a>
 {% endif %}
 
   {% else %}
@@ -40,7 +40,7 @@ More than <b>2,000,000 votes</b> have been cast using Helios.
   <p>
     {% for election in elections %}
     <div class="panel">
-      <a style="font-size: 1.4em;" href="{% url "helios.views.election_shortcut" election.short_name %}">{{election.name}}</a>{% if settings.SHOW_USER_INFO %}<br /> by {{election.admin.display_html_small|safe}}{% endif %}
+      <a style="font-size: 1.4em;" href="{% url "shortcut@election" election.short_name %}">{{election.name}}</a>{% if settings.SHOW_USER_INFO %}<br /> by {{election.admin.display_html_small|safe}}{% endif %}
     </div>
     <br />
     {% endfor %}
@@ -58,7 +58,7 @@ More than <b>2,000,000 votes</b> have been cast using Helios.
 {% if user %}
 <!--<div class="row right">{{user.display_html_big|safe}}</div>-->
 {% if create_p %}
-<a class="small button" href="{% url "helios.views.election_new" %}">create election</a>
+<a class="small button" href="{% url "elections@new" %}">create election</a>
 <h5 class="subheader">Administration</h5>
 {% if elections_administered %}
 <ul>
@@ -70,7 +70,7 @@ More than <b>2,000,000 votes</b> have been cast using Helios.
 <em>none yet</em>
 {% endif %}
 <div class="row right">
-<a class="tiny button" href="{% url "helios.views.elections_administered" %}">see all</a>
+<a class="tiny button" href="{% url "elections@administered" %}">see all</a>
 </div>
 <div class="row"></div>
 {% endif %}
@@ -85,7 +85,7 @@ More than <b>2,000,000 votes</b> have been cast using Helios.
 {% else %}
 <em>none yet</em>
 {% endif %}
-<div class="row right"><a class="tiny button" href="{% url "helios.views.elections_voted" %}">see all</a></div>
+<div class="row right"><a class="tiny button" href="{% url "elections@voted" %}">see all</a></div>
 <div class="row"></div>
 {% else %}
 {% if settings.SHOW_LOGIN_OPTIONS %}
-- 
GitLab