From a5f397559932ed966a3f342820dcfb8dd20d97ca Mon Sep 17 00:00:00 2001
From: Ben Adida <ben@adida.net>
Date: Wed, 27 Apr 2016 03:44:42 +0000
Subject: [PATCH] friendlier feedback when adding/editing a question with a bad
 URL

---
 helios/models.py                         |  3 ++-
 helios/templates/election_questions.html |  8 ++++++--
 helios/views.py                          | 12 +++++++-----
 3 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/helios/models.py b/helios/models.py
index bece7ca..2bc5742 100644
--- a/helios/models.py
+++ b/helios/models.py
@@ -257,9 +257,10 @@ class Election(HeliosModel):
           
         # abort saving if bad URL
         if not (answer_url[:7] == "http://" or answer_url[:8]== "https://"):
-          return
+          return False
     
     self.questions = questions
+    return True
 
   def add_voters_file(self, uploaded_file):
     """
diff --git a/helios/templates/election_questions.html b/helios/templates/election_questions.html
index d2262c4..4f83646 100644
--- a/helios/templates/election_questions.html
+++ b/helios/templates/election_questions.html
@@ -26,7 +26,11 @@
   
   function save_questions(callback) {
       $.post('./save_questions', {'questions_json' : $.toJSON(QUESTIONS), 'csrf_token': CSRF_TOKEN}, function(result) {
-          callback();          
+          if (result == "FAILURE") {
+            alert("The questions could not be saved. Please check that the URLs you entered are either http:// or https://");
+          } else {
+            callback();
+          }
       });      
   }
   
@@ -106,7 +110,7 @@
   function add_answer(el) {
     el = $(el);
     var num_answers = el.find('input.answer').length + 1;
-    el.find('tbody').append('<tr><th>&nbsp;&nbsp;&nbsp;</th><th>Answer #' + num_answers + '</th><td><input type="text" class="answer" name="answer_' + num_answers + '" size="70" /><br /><nobr>Link (optional): <input type="text" name="answer_url_' + num_answers + '" width="50%" /></nobr></td></tr>');
+    el.find('tbody').append('<tr><th>&nbsp;&nbsp;&nbsp;</th><th>Answer #' + num_answers + '</th><td><input type="text" class="answer" name="answer_' + num_answers + '" size="70" /><br /><nobr>Link (optional, <tt>http</tt> or <tt>https</tt> only): <input type="text" name="answer_url_' + num_answers + '" width="50%" /></nobr></td></tr>');
   }
 
   function add_answers(el, num) {
diff --git a/helios/views.py b/helios/views.py
index 17969db..f7975ee 100644
--- a/helios/views.py
+++ b/helios/views.py
@@ -941,11 +941,13 @@ def one_election_save_questions(request, election):
   check_csrf(request)
   
   questions = utils.from_json(request.POST['questions_json'])
-  election.save_questions_safely(questions)
-  election.save()
-
-  # always a machine API
-  return SUCCESS
+  questions_saved = election.save_questions_safely(questions)
+  
+  if questions_saved:
+    election.save()
+    return SUCCESS
+  else:
+    return FAILURE
 
 @transaction.atomic
 @election_admin(frozen=False)
-- 
GitLab