From 3c61f7a6c4cd0b9d3cd4dba47ded372bdcfce35b Mon Sep 17 00:00:00 2001 From: Ben Adida <ben@adida.net> Date: Tue, 27 May 2014 19:49:22 -0700 Subject: [PATCH] added extra error handling --- helios/templates/voters_upload_confirm.html | 11 ++++++++--- helios/views.py | 15 +++++++++++---- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/helios/templates/voters_upload_confirm.html b/helios/templates/voters_upload_confirm.html index 4f5d2ad..20caaae 100644 --- a/helios/templates/voters_upload_confirm.html +++ b/helios/templates/voters_upload_confirm.html @@ -14,11 +14,16 @@ You have uploaded a file of voters. The first few rows of this file are: {% endfor %} </table> -{% if email_problem %} +{% if problems %} <p style="font-size: 1.5em;"> -<b>HOLD ON</b>: those don't look like correct email addresses. Are you sure you uploaded a file with email address as second field?<br /> +HOLD ON:<br /> +{% for problem in problems %} +- {{problem}}<br /> +{% endfor %} +</p> +<br /> -<a href="{% url helios.views.voters_upload_cancel election.uuid %}">no, let me upload a different file</a> +<a href="{% url helios.views.voters_upload_cancel election.uuid %}">never mind, upload a different file</a> </p> {% else %} diff --git a/helios/views.py b/helios/views.py index f6ed5cb..021e448 100644 --- a/helios/views.py +++ b/helios/views.py @@ -1229,14 +1229,21 @@ def voters_upload(request, election): voter_file_obj = election.add_voters_file(voters_file) request.session['voter_file_id'] = voter_file_obj.id - + + problems = [] + # import the first few lines to check - voters = [v for v in voter_file_obj.itervoters()][:5] + try: + voters = [v for v in voter_file_obj.itervoters()][:5] + except: + voters = [] + problems.append("your CSV file could not be processed. Please check that it is a proper CSV file.") # check if voter emails look like emails - email_problem = False in [validate_email(v['email']) for v in voters] + if False in [validate_email(v['email']) for v in voters]: + problems.append("those don't look like correct email addresses. Are you sure you uploaded a file with email address as second field?") - return render_template(request, 'voters_upload_confirm', {'election': election, 'voters': voters, 'email_problem': email_problem}) + return render_template(request, 'voters_upload_confirm', {'election': election, 'voters': voters, 'problems': problems}) else: return HttpResponseRedirect("%s?%s" % (settings.SECURE_URL_HOST + reverse(voters_upload, args=[election.uuid]), urllib.urlencode({'e':'no voter file specified, try again'}))) -- GitLab