Commits

Jernej Kos committed 743657a

Implemented answer modification/review.

Comments (0)

Files changed (6)

   def interpret_answer(self, data):
     return data
 
-  def as_html(self):
-    form = self.get_form(prefix = 'q%s' % self.pk)
+  def form_data(self, data):
+    return data
+
+  def as_html(self, **form_args):
+    form = self.get_form(prefix = 'q%s' % self.pk, **form_args)
     tpl = template.loader.get_template('survey/questions/%s.html' % \
       self._meta.verbose_name.replace(' ', '_').replace('_question', ''))
     return tpl.render(template.Context({ "form" : form, "question" : self }))
   def interpret_answer(self, data):
     return self.choices.get(pk = data).number if data else None
 
+  def form_data(self, data):
+    return dict(choice = data)
+
 class ChoiceColumn(models.Model):
   class Meta:
     ordering = ['question', 'number']
       idata[rpk] = self.columns.get(pk = cpk).number if cpk else None
     return idata
 
+  def form_data(self, data):
+    fdata = {}
+    for rpk, cpk in data.items():
+      fdata['r%s' % rpk] = cpk
+    return fdata
+
 class MultipleChoice(models.Model):
   class Meta:
     ordering = ['question', 'number']
   def interpret_answer(self, data):
     return [self.choices.get(pk = x).number for x in data]
 
+  def form_data(self, data):
+    return dict(choice = data)
+
 class NumericRangeQuestion(Question):
   min_answer = models.PositiveIntegerField()
   max_answer = models.PositiveIntegerField()
     data = super(NumericRangeQuestion, self).submit(data)
     return data['choice']
 
+  def form_data(self, data):
+    return dict(choice = data)
+
 class NumericEntryQuestion(Question):
   def get_form(self, **kwargs):
     form = self.get_form_class(
     data = super(NumericEntryQuestion, self).submit(data)
     return int(data['value']) if data['value'] else None
 
+  def form_data(self, data):
+    return dict(value = data)
+
 def generate_sid():
   return "".join([random.choice(string.ascii_letters + string.digits) for i in xrange(10)])
 
 class Survey(models.Model):
+  class Meta:
+    ordering = ['sid']
+
   sid = models.CharField(primary_key = True, max_length = 15, editable = False, default = generate_sid)
   title = models.CharField(max_length = 200)
   questions = models.ManyToManyField(Question)
 
     super(Survey, self).save(*args, **kwargs)
 
-  def submit(self, data):
-    subject = Subject()
-    subject.save()
+  def submit(self, data, subject = None):
+    if subject is None:
+      subject = Subject()
+      subject.save()
 
     for question in self.questions.all():
-      answer = Answer(subject = subject, survey = self, question = question)
+      try:
+        answer = Answer.objects.get(subject = subject, survey = self, question = question)
+      except Answer.DoesNotExist:
+        answer = Answer(subject = subject, survey = self, question = question)
+
       answer.data = simplejson.dumps(question.submit(data))
       answer.save()
 
+    return subject
+
+  def get_subjects(self):
+    return Subject.objects.filter(answers__survey = self).distinct()
+
   def __unicode__(self):
     return self.title
 
   return "".join([random.choice(string.uppercase + string.digits) for i in xrange(4)])
 
 class Subject(models.Model):
+  class Meta:
+    ordering = ['sid']
+
   sid = models.CharField(primary_key = True, max_length = 15, editable = False, default = generate_subject_sid)
   created_at = models.DateTimeField(auto_now_add = True)
 
   data = models.TextField()
 
   def interpret(self):
-    return self.question.interpret_answer(self.data)
+    data = simplejson.loads(self.data)
+    return self.question.interpret_answer(data)
 
+  def as_html(self):
+    data = simplejson.loads(self.data)
+    return self.question.as_html(initial = self.question.form_data(data))
+
 def enter_survey_instance(request, survey_id):
   survey = get_object_or_404(survey_models.Survey, pk = survey_id)
   if request.method == 'POST':
-    survey.submit(request.POST)
+    subject = survey.submit(request.POST)
     return render_to_response('survey/submit_done.html', dict(
-      survey = survey), context_instance = RequestContext(request))
+      survey = survey, subject = subject), context_instance = RequestContext(request))
 
   return render_to_response('survey/enter.html', dict(
     survey = survey), context_instance = RequestContext(request))
 
 def list_subjects(request, survey_id):
-  pass
+  survey = get_object_or_404(survey_models.Survey, pk = survey_id)
+  return render_to_response('survey/list.html', dict(
+    survey = survey), context_instance = RequestContext(request))
 
-def show_answers(request, survey_id, subject_id):
-  pass
+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)
+  if request.method == 'POST':
+    survey.submit(request.POST, subject)
+    return render_to_response('survey/submit_done.html', dict(
+      survey = survey, subject = subject), context_instance = RequestContext(request))
+
+  answers = []
+  for question in survey.questions.all():
+    answer = survey.subject_answers.get(question = question, subject = subject)
+    answers.append(answer)
+
+  return render_to_response('survey/edit.html', dict(
+    survey = survey,
+    subject = subject,
+    answers = answers), context_instance = RequestContext(request))
 
 def list_surveys(request):
   pass

templates/survey/edit.html

+
+Survey: <b>{{ survey.title }}</b><br/>
+Subject#: <b>{{ subject.pk }}</b>
+
+<form method="POST" action="{% url edit_survey survey.pk subject.pk %}">
+{% csrf_token %}
+{% for answer in answers %}
+<hr/>
+{{ answer.as_html }}
+{% endfor %}
+
+<hr/>
+<input type="submit" value="Submit" />
+</form>
+

templates/survey/list.html

+Survey: <b>{{ survey.title }}</b><br/>
+<br/>
+Subjects:
+<hr/>
+{% for subject in survey.get_subjects %}
+<a href="{% url edit_survey survey.pk subject.pk %}">{{ subject.pk }}</a> ({{ subject.created_at }})<br/>
+{% endfor %}
+<hr/>
+<a href="{% url enter_survey survey.pk %}">Enter new subject</a>

templates/survey/submit_done.html

-Answers recorded successfully.
+Survey: <b>{{ survey.title }}</b><br/>
+Subject#: <b>{{ subject.pk }}</b><br/>
+<br/>
+Answers recorded successfully.<br/>
+<br/>
+<a href="{% url list_subjects survey.pk %}">List of subjects</a>
 admin.autodiscover()
 
 urlpatterns = patterns('',
-  url(r'^survey/(?P<survey_id>.+)$', 'survey.views.enter_survey_instance', name = 'enter_survey'),
+  url(r'^survey/(?P<survey_id>\w+)$', 'survey.views.enter_survey_instance', name = 'enter_survey'),
+  url(r'^survey/(?P<survey_id>\w+)/(?P<subject_id>\w+)$', 'survey.views.edit_survey_instance', name = 'edit_survey'),
+  url(r'^survey/(?P<survey_id>\w+)/.subjects$', 'survey.views.list_subjects', name = 'list_subjects'),
 
   url(r'^admin/', include(admin.site.urls)),
 )