From 7463bab9fc6c35f98f67440f279d84013f16330f Mon Sep 17 00:00:00 2001
From: RunasSudo <runassudo@yingtongli.me>
Date: Tue, 5 Jul 2016 14:35:44 +0930
Subject: [PATCH] Implement voting extension

---
 helios/election_urls.py               |  1 +
 helios/forms.py                       |  3 +++
 helios/templates/election_extend.html | 17 +++++++++++++++++
 helios/templates/election_view.html   |  3 +++
 helios/views.py                       | 17 +++++++++++++++++
 5 files changed, 41 insertions(+)
 create mode 100644 helios/templates/election_extend.html

diff --git a/helios/election_urls.py b/helios/election_urls.py
index e6654d1..c7cd8fc 100644
--- a/helios/election_urls.py
+++ b/helios/election_urls.py
@@ -18,6 +18,7 @@ urlpatterns = patterns('',
     # edit election params
     (r'^/edit$', one_election_edit),
     (r'^/schedule$', one_election_schedule),
+    (r'^/extend$', one_election_extend),
     (r'^/archive$', one_election_archive),
 
     # badge
diff --git a/helios/forms.py b/helios/forms.py
index 8851d66..2ef7c80 100644
--- a/helios/forms.py
+++ b/helios/forms.py
@@ -29,6 +29,9 @@ class ElectionForm(forms.Form):
   voting_ends_at = SplitDateTimeField(help_text = 'UTC date and time when voting ends',
                                    widget=SplitSelectDateTimeWidget, required=False)
 
+class ElectionTimeExtensionForm(forms.Form):
+  voting_extended_until = SplitDateTimeField(help_text = 'UTC date and time voting extended to',
+                                   widget=SplitSelectDateTimeWidget, required=False)
   
 class EmailVotersForm(forms.Form):
   subject = forms.CharField(max_length=80)
diff --git a/helios/templates/election_extend.html b/helios/templates/election_extend.html
new file mode 100644
index 0000000..0db2c44
--- /dev/null
+++ b/helios/templates/election_extend.html
@@ -0,0 +1,17 @@
+{% extends TEMPLATE_BASE %}
+
+{% block content %}
+
+  <h2 class="title">{{election.name}} &mdash; Extend Voting <span style="font-size:0.7em;">[<a href="{% url "helios.views.one_election_view" election.uuid %}">cancel</a>]</span></h2>
+  
+  <form class="prettyform" action="" method="POST" id="edit_election_form">
+    <input type="hidden" name="csrf_token" value="{{csrf_token}}" />
+    <table class="pretty">
+     {{election_form.as_table}}
+     </table>
+  <div>
+  <label for="">&nbsp;</label><input type="submit" value="Extend Voting" id="update_button" />
+  </div>
+  </form>
+
+{% endblock %}
diff --git a/helios/templates/election_view.html b/helios/templates/election_view.html
index 472a837..fba739c 100644
--- a/helios/templates/election_view.html
+++ b/helios/templates/election_view.html
@@ -247,6 +247,9 @@ Anyone can vote in this election.
 
 {% endif %}
 
+{% if admin_p and election.voting_ends_at and not election.tallying_started_at %}
+<br /><a href="{% url "helios.views.one_election_extend" election.uuid %}">extend voting</a><br />
+{% endif %}
 
 <div style="background: lightyellow; padding:5px; padding-left: 10px; margin-top: 15px; border: 1px solid #aaa; width: 720px;" class="round">
 <a href="#" onclick="$('#auditbody').slideToggle(250);">Audit Info</a>
diff --git a/helios/views.py b/helios/views.py
index 08c3e6c..39a5f26 100644
--- a/helios/views.py
+++ b/helios/views.py
@@ -254,6 +254,23 @@ def one_election_edit(request, election):
 def one_election_schedule(request, election):
   return HttpResponse("foo")
 
+@election_admin()
+def one_election_extend(request, election):
+  if request.method == "GET":
+    election_form = forms.ElectionTimeExtensionForm({'voting_extended_until': election.voting_extended_until})
+  else:
+    check_csrf(request)
+    election_form = forms.ElectionTimeExtensionForm(request.POST)
+
+    if election_form.is_valid():
+      clean_data = election_form.cleaned_data
+      election.voting_extended_until = clean_data['voting_extended_until']
+      election.save()
+        
+      return HttpResponseRedirect(settings.SECURE_URL_HOST + reverse(one_election_view, args=[election.uuid]))
+  
+  return render_template(request, "election_extend", {'election_form' : election_form, 'election' : election})
+
 @election_view()
 @return_json
 def one_election(request, election):
-- 
GitLab