Advertisement
Guest User

Untitled

a guest
Mar 18th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.50 KB | None | 0 0
  1. ### models.py ###
  2.  
  3. class Question(SortableMixin):
  4.  
  5.     class Meta:
  6.         ordering = ['order']
  7.  
  8.     CUSTOM = "custom"
  9.     SELECT_ONE = "select-one"
  10.     SELECT_MULTIPLE = "select-multiple"
  11.     YEAR = "year"
  12.     SSN = "SSN"
  13.     DROPDOWN = "dropdown"
  14.  
  15.     QUESTION_TYPES = (
  16.         (CUSTOM, "custom"),
  17.         (SELECT_ONE, "select one"),
  18.         (SELECT_MULTIPLE, "select multiple"),
  19.         (YEAR, "year"),
  20.         (SSN, "SSN"),
  21.         (DROPDOWN, "Dropdown")
  22.     )
  23.  
  24.     category = models.ForeignKey(QuestionCategory)
  25.     grading_category = models.ForeignKey(GradingCategory, null=True, blank=True, related_name="questions")
  26.     question_type = models.CharField(max_length=256, choices=QUESTION_TYPES)
  27.     text = models.TextField(default="")
  28.     form_field_identifier = models.CharField(max_length=256, blank=True, null=True)
  29.     required = models.BooleanField(default=True)
  30.  
  31.     order = models.PositiveIntegerField(default=0, editable=False, db_index=True)
  32.    
  33.     def has_custom_choices(self):
  34.         return self.get_custom_choices().exists()
  35.  
  36.     def get_custom_choices(self):
  37.         return self.choices.filter(choice_type=Choice.CUSTOM)
  38.  
  39.     def __unicode__(self):
  40.         return self.text
  41.  
  42. ### forms.py ###
  43.  
  44. # -*- coding: utf-8 -*-
  45. from django import forms
  46. from .models import Question, Choice, SDQProfile, GeneratedHash
  47. from helpers import is_id_valid
  48. from django.utils.translation import ugettext as _
  49.  
  50. class LoginForm(forms.Form):
  51.     username = forms.CharField(label=_(u"Käyttäjätunnus"), max_length=256)
  52.     password = forms.CharField(label=_(u"Salasana"), max_length=256, widget=forms.PasswordInput())
  53.  
  54. class ProfileSelectionForm(forms.Form):
  55.     profiles = forms.ModelMultipleChoiceField(
  56.         queryset=SDQProfile.objects.all(),
  57.         widget=forms.CheckboxSelectMultiple
  58.     )
  59.  
  60.     def __init__(self, *args, **kwargs):
  61.         super(ProfileSelectionForm, self).__init__(*args, **kwargs)
  62.  
  63.         self.fields["profiles"].choices = map(
  64.             lambda k: (k[0], k[1].strftime("%d.%m.%Y")),
  65.                 sorted(
  66.                     [(p.pk, p.created_at) for p in SDQProfile.objects.all()], key=lambda k: k[1]
  67.                 )
  68.             )
  69.  
  70. class SlugForm(forms.Form):
  71.     slug = forms.CharField(label=_(u"Koodi"), max_length=10)
  72.  
  73. class SDQForm(forms.Form):
  74.     def __init__(self, *args, **kwargs):
  75.         kwargs.setdefault('label_suffix', '')
  76.         super(SDQForm, self).__init__(*args, **kwargs)
  77.  
  78.  
  79.         for question in Question.objects.all():
  80.             field_name = question.form_field_identifier
  81.             required = question.required
  82.  
  83.             if question.question_type == Question.CUSTOM:
  84.                 field = forms.CharField(label=question.text, required=required)
  85.                 self.fields[field_name] = field
  86.  
  87.             elif question.question_type == Question.SELECT_ONE:
  88.                 choices = [(c.id, c.text) for c in question.choices.all() if c.choice_type]
  89.                 field = forms.ChoiceField(
  90.                     label=question.text,
  91.                     widget=forms.RadioSelect,
  92.                     choices=choices,
  93.                     required=required
  94.                 )
  95.                 self.fields[field_name] = field
  96.  
  97.                 custom_choices = [c for c in question.choices.all() if c.choice_type == Choice.CUSTOM]
  98.  
  99.                 for custom_choice in custom_choices:
  100.                     field = forms.CharField(label=custom_choice.text, required=False)
  101.                     self.fields[custom_choice.get_custom_identifier()] = field
  102.  
  103.             elif question.question_type == Question.SELECT_MULTIPLE:
  104.                 choices = [(c.id, c.text) for c in question.choices.all() if c.choice_type]
  105.                 field = forms.MultipleChoiceField(
  106.                     label=question.text,
  107.                     widget=forms.CheckboxSelectMultiple,
  108.                     choices=choices,
  109.                     required=required
  110.                 )
  111.                 self.fields[field_name] = field    
  112.  
  113.                 custom_choices = [c for c in question.choices.all() if c.choice_type == Choice.CUSTOM]
  114.  
  115.                 for custom_choice in custom_choices:
  116.                     field = forms.CharField(label=custom_choice.text, required=False)
  117.                     self.fields[custom_choice.get_custom_identifier()] = field
  118.  
  119.             elif question.question_type == Question.YEAR:
  120.                 field = forms.DateField(
  121.                     label=question.text,
  122.                     widget=forms.TextInput(attrs={'class':'datepicker'}),
  123.                     input_formats=["%Y"],
  124.                     required=required
  125.                 )
  126.                 self.fields[field_name] = field
  127.  
  128.             elif question.question_type == Question.SSN:
  129.                 self.fields[field_name+"_start"] = forms.CharField(label=question.text, widget=forms.TextInput(attrs={'class':'ssn-field'}))
  130.                 self.fields[field_name+"_end"] = forms.CharField(label=question.text, widget=forms.TextInput(attrs={'class':'ssn-field'}))
  131.  
  132.     def clean(self):
  133.         cleaned_data = super(SDQForm, self).clean()
  134.         start = cleaned_data.get("hetu_start", "")
  135.         end = cleaned_data.get("hetu_end", "")
  136.  
  137.         if not is_id_valid(start+"A"+end):
  138.             raise forms.ValidationError(_(u"Virhe henkilötunnuksessa"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement