Commits

Stefan Zimmermann  committed 3a5e0d1

some improvements in voting index generator (formal stuff + field internal num vote constraints check + decimal comma support)

  • Participants
  • Parent commits 9626ff6

Comments (0)

Files changed (1)

File app/views.py

   def _Form(self, question, *args):
     """Generate the form instance for given poll question.
     """
-    qtype = question.qtype
-    if qtype != 2: responses = question.pollresponses(self._poll)
+    class Form(forms.Form):
+      __qtype = question.qtype
 
-    class Form(forms.Form):
-      if qtype == 2: # input field for numerical questions,
-        num = forms.DecimalField()
+      if __qtype == 2: # input field for numerical questions,
+        __numresponse = question.numresponse
+
+        num = forms.DecimalField(
+          min_value = __numresponse.bottom,
+          max_value = __numresponse.top,
+          localize = True)
 
       else: #... choice fields for choice questions
-        choice = (forms.ChoiceField, forms.MultipleChoiceField)[qtype](
-          choices = [(r.id, r.title) for r in responses], widget = (
-            (forms.RadioSelect, forms.CheckboxSelectMultiple)[qtype]()))
+        __responses = question.pollresponses(self._poll)
+
+        choice = (forms.ChoiceField, forms.MultipleChoiceField)[__qtype](
+          choices = [(r.id, r.title)
+                     for r in question.pollresponses(self._poll)],
+
+          widget = ((forms.RadioSelect, forms.CheckboxSelectMultiple)
+                    [__qtype]()))
 
     return Form(*args)
 
     form = self._Form(question, request.POST)
     if not form.is_valid(): return form #, question, qn
 
-    qtype = question.qtype
-    if qtype == 2:
-      numresponse = question.numresponse
-
-      value = Decimal(request.POST['num'])
-      if value < numresponse.bottom or value > numresponse.top:
-        return form #, question, qn
-
     # save that voter has answered the question
     self._voter.question = question
     self._voter.save()
 
     voterefs = dict(voting = self._voting, voter = self._voter)
-    if qtype == 2: NumVote(
-      response = numresponse, value = request.POST['num'], **voterefs
+    if question.qtype == 2: NumVote(
+      response = question.numresponse,
+      value = request.POST['num'].replace(',', '.'),
+      **voterefs
       ).save()
 
     else:
       for id in request.POST.getlist('choice'):
         Vote(response = Response.objects.get(id = id), **voterefs).save()
 
-    ## # get next poll question and generate new form
-    ## qn += 1
-    ## question = self._question(qn)
-    ## return self._Form(question), question, qn
-
   def _response(self, request, templatefile, context = None):
     """Generate the HttpResponse from template and context.
     """