Commits

Jernej Kos committed 4f3500f

Added commenting and date questions.

  • Participants
  • Parent commits de14104

Comments (0)

Files changed (5)

File survey/admin.py

 
 admin.site.register(survey_models.NumericEntryQuestion)
 
+admin.site.register(survey_models.DateQuestion)
+
 admin.site.register(survey_models.SeparatorQuestion)
 
 admin.site.register(survey_models.Subject)

File survey/models.py

 from django.db import models, transaction
 from django.contrib.auth.models import User
 
+import datetime
 import polymorphic
 import random
 import simplejson
   survey = models.ForeignKey('Survey', related_name = 'questions')
   order = models.IntegerField()
   description = models.TextField()
+  allow_comment = models.BooleanField(default = False)
 
   def get_form_class(self, **attrs):
+    if self.allow_comment:
+      attrs['comment'] = forms.CharField(widget = forms.Textarea, required = False)
+    
     new_cls = type("_%s_Form" % self.__class__.__name__, (forms.Form,), attrs)
     return new_cls
 
     form = self.get_form(data = data, prefix = 'q%s' % self.pk)
     if not form.is_valid():
       raise ValidationError
-    return form.cleaned_data
-
-  def interpret_answer(self, data):
-    return data
+    return form.cleaned_data, dict(comment = form.cleaned_data.get('comment', '').strip())
 
   def form_data(self, data):
     return data
     return form(**kwargs)
 
   def submit(self, data):
-    data = super(SimpleChoiceQuestion, self).submit(data)
-    return data['choice'].pk if data['choice'] else None
-
-  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)
+    data, add_fields = super(SimpleChoiceQuestion, self).submit(data)
+    add_fields['choice'] = data['choice'].pk if data['choice'] else None
+    return add_fields
 
 class ChoiceColumn(models.Model):
   class Meta:
     return form(**kwargs)
 
   def submit(self, data):
-    raw_data = super(ColumnChoiceQuestion, self).submit(data)
+    raw_data, add_fields = super(ColumnChoiceQuestion, self).submit(data)
     data = {}
     for row in self.rows.all():
       datum = raw_data['r%s' % row.pk]
       data[row.pk] = datum.pk if datum else None
-    return data
-
-  def interpret_answer(self, data):
-    idata = {}
-    for rpk, cpk in data.items():
-      idata[rpk] = self.columns.get(pk = cpk).number if cpk else None
-    return idata
+    add_fields['rows'] = data
+    return add_fields
 
   def form_data(self, data):
-    fdata = {}
-    for rpk, cpk in data.items():
-      fdata['r%s' % rpk] = cpk
-    return fdata
+    data = super(ColumnChoiceQuestion, self).form_data(data)
+    for rpk, cpk in data['rows'].items():
+      data['r%s' % rpk] = cpk
+    
+    del data['rows']
+    return data
 
 class MultipleChoice(models.Model):
   class Meta:
     return form(**kwargs)
 
   def submit(self, data):
-    data = super(MultipleChoiceQuestion, self).submit(data)
-    return [x.pk for x in data['choice']]
-
-  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)
+    data, add_fields = super(MultipleChoiceQuestion, self).submit(data)
+    add_fields['choice'] = [x.pk for x in data['choice']]
+    return add_fields
 
 class NumericRangeQuestion(Question):
   min_answer = models.PositiveIntegerField()
     return form(**kwargs)
 
   def submit(self, data):
-    data = super(NumericRangeQuestion, self).submit(data)
-    return data['choice']
+    data, add_fields = super(NumericRangeQuestion, self).submit(data)
+    add_fields['choice'] = data['choice']
+    return add_fields
 
+class DateQuestion(Question):
+  def get_form(self, **kwargs):
+    form = self.get_form_class(
+      value = forms.DateField(required = False),
+      input_formats = ['%d %m %Y', '%d.%m.%Y', '%d. %m. %Y', '%d-%m-%Y', '%d/%m/%Y']
+    )
+    return form(**kwargs)
+  
+  def submit(self, data):
+    data, add_fields = super(DateQuestion, self).submit(data)
+    date = data['value']
+    add_fields['value'] = dict(
+      year = date.year,
+      month = date.month,
+      day = date.day
+    ) if date else None
+    return add_fields
+  
   def form_data(self, data):
-    return dict(choice = data)
+    return dict(
+      value = datetime.date(
+        data['value']['year'],
+        data['value']['month'],
+        data['value']['day']
+      ) if data['value'] else None
+    )
 
 class NumericEntryQuestion(Question):
   def get_form(self, **kwargs):
     return form(**kwargs)
 
   def submit(self, data):
-    data = super(NumericEntryQuestion, self).submit(data)
-    return int(data['value']) if data['value'] else None
-
-  def form_data(self, data):
-    return dict(value = data)
+    data, add_fields = super(NumericEntryQuestion, self).submit(data)
+    add_fields['value'] = int(data['value']) if data['value'] else None
+    return add_fields
 
 def generate_sid():
   return "".join([random.choice(string.ascii_letters + string.digits) for i in xrange(10)])
   question = models.ForeignKey(Question, related_name = 'subject_answers')
   data = models.TextField()
 
-  def interpret(self):
-    data = simplejson.loads(self.data)
-    return self.question.interpret_answer(data)
-
   def as_html(self, **form_args):
     data = simplejson.loads(self.data)
     return self.question.as_html(initial = self.question.form_data(data), **form_args)

File templates/survey/questions/base.html

 
 {% block form %}
 {% endblock %}
+
+{% block comment %}
+{% if form.comment %}
+<br/><br/>
+Komentar:<br/>
+{{ form.comment }}
+{% endif %}
+{% endblock %}

File templates/survey/questions/column_choice.html

 {% block form %}
 {{ form.as_p }}
 {% endblock %}
+
+{% block comment %}{% endblock %}

File templates/survey/questions/date.html

+{% extends "survey/questions/base.html" %}
+{% block form %}
+{{ form.value }}
+{% endblock %}