Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### models.py ###
- class Question(SortableMixin):
- class Meta:
- ordering = ['order']
- CUSTOM = "custom"
- SELECT_ONE = "select-one"
- SELECT_MULTIPLE = "select-multiple"
- YEAR = "year"
- SSN = "SSN"
- DROPDOWN = "dropdown"
- QUESTION_TYPES = (
- (CUSTOM, "custom"),
- (SELECT_ONE, "select one"),
- (SELECT_MULTIPLE, "select multiple"),
- (YEAR, "year"),
- (SSN, "SSN"),
- (DROPDOWN, "Dropdown")
- )
- category = models.ForeignKey(QuestionCategory)
- grading_category = models.ForeignKey(GradingCategory, null=True, blank=True, related_name="questions")
- question_type = models.CharField(max_length=256, choices=QUESTION_TYPES)
- text = models.TextField(default="")
- form_field_identifier = models.CharField(max_length=256, blank=True, null=True)
- required = models.BooleanField(default=True)
- order = models.PositiveIntegerField(default=0, editable=False, db_index=True)
- def has_custom_choices(self):
- return self.get_custom_choices().exists()
- def get_custom_choices(self):
- return self.choices.filter(choice_type=Choice.CUSTOM)
- def __unicode__(self):
- return self.text
- ### forms.py ###
- # -*- coding: utf-8 -*-
- from django import forms
- from .models import Question, Choice, SDQProfile, GeneratedHash
- from helpers import is_id_valid
- from django.utils.translation import ugettext as _
- class LoginForm(forms.Form):
- username = forms.CharField(label=_(u"Käyttäjätunnus"), max_length=256)
- password = forms.CharField(label=_(u"Salasana"), max_length=256, widget=forms.PasswordInput())
- class ProfileSelectionForm(forms.Form):
- profiles = forms.ModelMultipleChoiceField(
- queryset=SDQProfile.objects.all(),
- widget=forms.CheckboxSelectMultiple
- )
- def __init__(self, *args, **kwargs):
- super(ProfileSelectionForm, self).__init__(*args, **kwargs)
- self.fields["profiles"].choices = map(
- lambda k: (k[0], k[1].strftime("%d.%m.%Y")),
- sorted(
- [(p.pk, p.created_at) for p in SDQProfile.objects.all()], key=lambda k: k[1]
- )
- )
- class SlugForm(forms.Form):
- slug = forms.CharField(label=_(u"Koodi"), max_length=10)
- class SDQForm(forms.Form):
- def __init__(self, *args, **kwargs):
- kwargs.setdefault('label_suffix', '')
- super(SDQForm, self).__init__(*args, **kwargs)
- for question in Question.objects.all():
- field_name = question.form_field_identifier
- required = question.required
- if question.question_type == Question.CUSTOM:
- field = forms.CharField(label=question.text, required=required)
- self.fields[field_name] = field
- elif question.question_type == Question.SELECT_ONE:
- choices = [(c.id, c.text) for c in question.choices.all() if c.choice_type]
- field = forms.ChoiceField(
- label=question.text,
- widget=forms.RadioSelect,
- choices=choices,
- required=required
- )
- self.fields[field_name] = field
- custom_choices = [c for c in question.choices.all() if c.choice_type == Choice.CUSTOM]
- for custom_choice in custom_choices:
- field = forms.CharField(label=custom_choice.text, required=False)
- self.fields[custom_choice.get_custom_identifier()] = field
- elif question.question_type == Question.SELECT_MULTIPLE:
- choices = [(c.id, c.text) for c in question.choices.all() if c.choice_type]
- field = forms.MultipleChoiceField(
- label=question.text,
- widget=forms.CheckboxSelectMultiple,
- choices=choices,
- required=required
- )
- self.fields[field_name] = field
- custom_choices = [c for c in question.choices.all() if c.choice_type == Choice.CUSTOM]
- for custom_choice in custom_choices:
- field = forms.CharField(label=custom_choice.text, required=False)
- self.fields[custom_choice.get_custom_identifier()] = field
- elif question.question_type == Question.YEAR:
- field = forms.DateField(
- label=question.text,
- widget=forms.TextInput(attrs={'class':'datepicker'}),
- input_formats=["%Y"],
- required=required
- )
- self.fields[field_name] = field
- elif question.question_type == Question.SSN:
- self.fields[field_name+"_start"] = forms.CharField(label=question.text, widget=forms.TextInput(attrs={'class':'ssn-field'}))
- self.fields[field_name+"_end"] = forms.CharField(label=question.text, widget=forms.TextInput(attrs={'class':'ssn-field'}))
- def clean(self):
- cleaned_data = super(SDQForm, self).clean()
- start = cleaned_data.get("hetu_start", "")
- end = cleaned_data.get("hetu_end", "")
- if not is_id_valid(start+"A"+end):
- raise forms.ValidationError(_(u"Virhe henkilötunnuksessa"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement