Commits

Jernej Kos committed de14104

Added error handling.

  • Participants
  • Parent commits d357f72

Comments (0)

Files changed (10)

File survey/models.py

     data = simplejson.loads(self.data)
     return self.question.interpret_answer(data)
 
-  def as_html(self):
+  def as_html(self, **form_args):
     data = simplejson.loads(self.data)
-    return self.question.as_html(initial = self.question.form_data(data))
+    return self.question.as_html(initial = self.question.form_data(data), **form_args)
 

File survey/views.py

 @login_required
 def enter_survey_instance(request, survey_id):
   survey = get_object_or_404(survey_models.Survey, pk = survey_id)
+  data, has_errors = None, False
   if request.method == 'POST':
     try:
       subject = survey.submit(request.POST, user = request.user)
       return render_to_response('survey/submit_done.html', dict(
         survey = survey, subject = subject), context_instance = RequestContext(request))
     except survey_models.ValidationError:
-      pass
+      data, has_errors = request.POST, True
+
+  questions = []
+  for question in survey.questions.all():
+    questions.append(question.as_html(data = data))
 
   return render_to_response('survey/enter.html', dict(
-    survey = survey), context_instance = RequestContext(request))
+    survey = survey, questions = questions, has_errors = has_errors
+  ), context_instance = RequestContext(request))
 
 @login_required
 def list_subjects(request, survey_id):
 def edit_survey_instance(request, survey_id, subject_id):
   survey = get_object_or_404(survey_models.Survey, pk = survey_id)
   subject = get_object_or_404(survey_models.Subject, pk = subject_id)
+  data, has_errors = None, False
   if request.method == 'POST':
     try:
       survey.submit(request.POST, subject)
       return render_to_response('survey/submit_done.html', dict(
         survey = survey, subject = subject), context_instance = RequestContext(request))
     except survey_models.ValidationError:
-      pass
+      data, has_errors = request.POST, True
 
   answers = []
   for question in survey.questions.all():
       continue
     
     answer = survey.subject_answers.get(question = question, subject = subject)
-    answers.append(answer)
+    answers.append(answer.as_html(data = data))
 
   return render_to_response('survey/edit.html', dict(
     survey = survey,
     subject = subject,
-    answers = answers), context_instance = RequestContext(request))
+    answers = answers,
+    has_errors = has_errors), context_instance = RequestContext(request))
 

File templates/survey/edit.html

 Survey: <b>{{ survey.title }}</b><br/>
 Subject#: <b>{{ subject.pk }}</b>
 
+{% if has_errors %}
+<div style="border: 1px solid black; background-color: #fd7a7a; padding: 2px;">
+<u>VNOS POPRAVKOV NI USPEL!</u> Pri validaciji podatkov je prišlo do napak, ki so izpisane spodaj pri vprašanju pri katerem je prišlo do napake. Napake odpravite in nato ponovno oddajte vprašalnik.
+</div>
+{% endif %}
+
 <form method="POST" action="{% url edit_survey survey.pk subject.pk %}">
 {% csrf_token %}
 {% for answer in answers %}
 <hr/>
-{{ answer.as_html }}
+{{ answer }}
 {% endfor %}
 
 <hr/>

File templates/survey/enter.html

 {% block content %}
 Survey: <b>{{ survey.title }}</b>
 
+{% if has_errors %}
+<div style="border: 1px solid black; background-color: #fd7a7a; padding: 2px;">
+<u>VNOS SUBJEKTA NI USPEL!</u> Pri validaciji podatkov je prišlo do napak, ki so izpisane spodaj pri vprašanju pri katerem je prišlo do napake. Napake odpravite in nato ponovno oddajte vprašalnik.
+</div>
+{% endif %}
+
 <form method="POST" action="{% url enter_survey survey.pk %}">
 {% csrf_token %}
-{% for question in survey.questions.all %}
+{% for question in questions %}
 <hr/>
-{{ question.as_html }}
+{{ question }}
 {% endfor %}
 
 <hr/>

File templates/survey/questions/base.html

+{% block title %}
+<b>{{ question.description }}</b>
+<br/>
+{% endblock %}
+
+{% block errors %}
+{% if form.errors %}
+<div style="border: 1px solid black; background-color: #fd7a7a; padding: 2px;">
+PRI VNOSU VREDNOSTI JE PRIŠLO DO NASLEDNJIH NAPAK:<br/>
+{% for error in form.errors.values %}
+  {{ error }}<br/>
+{% endfor %}
+</div>
+{% endif %}
+{% endblock %}
+
+{% block form %}
+{% endblock %}

File templates/survey/questions/column_choice.html

-<b>{{ question.description }}</b>
-<br/>
+{% extends "survey/questions/base.html" %}
+{% block form %}
 {{ form.as_p }}
+{% endblock %}

File templates/survey/questions/multiple_choice.html

-<b>{{ question.description }}</b>
-<br/>
+{% extends "survey/questions/base.html" %}
+{% block form %}
 {{ form.choice }}
 <br/>
 (za izbiro več vrednosti uporabite tipko <i>Ctrl</i>)
+{% endblock %}

File templates/survey/questions/numeric_entry.html

-<b>{{ question.description }}</b>
-<br/>
+{% extends "survey/questions/base.html" %}
+{% block form %}
 {{ form.value }}
+{% endblock %}

File templates/survey/questions/numeric_range.html

-<b>{{ question.description }}</b>
-<br/>
+{% extends "survey/questions/base.html" %}
+{% block form %}
 {{ form.choice }}
+{% endblock %}

File templates/survey/questions/simple_choice.html

-<b>{{ question.description }}</b>
-<br/>
+{% extends "survey/questions/base.html" %}
+{% block form %}
 {{ form.choice }}
+{% endblock %}