from django import forms from evap.student.tools import question_id from evap.evaluation.tools import LIKERT_NAMES, GRADE_NAMES, POSITIVE_YES_NO_NAMES, NEGATIVE_YES_NO_NAMES LIKERT_CHOICES = [(str(k), v) for k, v in LIKERT_NAMES.items()] GRADE_CHOICES = [(str(k), v) for k, v in GRADE_NAMES.items()] POSITIVE_YES_NO_CHOICES = [(str(k), v) for k, v in POSITIVE_YES_NO_NAMES.items()] NEGATIVE_YES_NO_CHOICES = [(str(k), v) for k, v in NEGATIVE_YES_NO_NAMES.items()] class QuestionsForm(forms.Form): """Dynamic form class that adds one field per question. See http://jacobian.org/writing/dynamic-form-generation/""" def __init__(self, *args, contribution, questionnaire, **kwargs): super().__init__(*args, **kwargs) self.questionnaire = questionnaire for question in self.questionnaire.question_set.all(): # generic arguments for all kinds of fields field_args = dict(label=question.text) if question.is_text_question: field = forms.CharField(required=False, widget=forms.Textarea(), **field_args) elif question.is_likert_question: field = forms.TypedChoiceField(widget=forms.RadioSelect(), choices=LIKERT_CHOICES, coerce=int,
from django import forms from django.utils.translation import ugettext_lazy as _ from django.utils.encoding import force_unicode from django.utils.safestring import mark_safe from evap.student.tools import make_form_identifier from evap.evaluation.models import UserProfile from evap.evaluation.tools import GRADE_NAMES GRADE_CHOICES = [(unicode(k), v) for k, v in GRADE_NAMES.items()] class RadioFieldTableRenderer(forms.widgets.RadioFieldRenderer): def render(self): """Outputs a <ul> for this set of radio fields.""" return mark_safe(u'\n'.join([u'<div>%s</div>' % force_unicode(w) for w in self])) class QuestionsForm(forms.Form): """Dynamic form class that adds one field per question. Pass the arguments `contribution` and `questionnaire` to the constructor. See http://jacobian.org/writing/dynamic-form-generation/""" def __init__(self, *args, **kwargs): self.contribution = kwargs.pop('contribution') self.questionnaire = kwargs.pop('questionnaire')
from django import forms from django.utils.translation import ugettext_lazy as _ from django.utils.encoding import force_unicode from django.utils.safestring import mark_safe from evap.student.tools import make_form_identifier from evap.evaluation.models import UserProfile from evap.evaluation.tools import GRADE_NAMES GRADE_CHOICES = [(unicode(k), v) for k, v in GRADE_NAMES.items()] class RadioFieldTableRenderer(forms.widgets.RadioFieldRenderer): def render(self): """Outputs a <ul> for this set of radio fields.""" return mark_safe(u'\n'.join( [u'<div>%s</div>' % force_unicode(w) for w in self])) class QuestionsForm(forms.Form): """Dynamic form class that adds one field per question. Pass the arguments `contribution` and `questionnaire` to the constructor. See http://jacobian.org/writing/dynamic-form-generation/""" def __init__(self, *args, **kwargs): self.contribution = kwargs.pop('contribution') self.questionnaire = kwargs.pop('questionnaire') super(QuestionsForm, self).__init__(*args, **kwargs) for question in self.questionnaire.question_set.all():
from django import forms from evap.student.tools import make_form_identifier from evap.evaluation.tools import LIKERT_NAMES, GRADE_NAMES LIKERT_CHOICES = [(str(k), v) for k, v in LIKERT_NAMES.items()] GRADE_CHOICES = [(str(k), v) for k, v in GRADE_NAMES.items()] class QuestionsForm(forms.Form): """Dynamic form class that adds one field per question. Pass the arguments `contribution` and `questionnaire` to the constructor. See http://jacobian.org/writing/dynamic-form-generation/""" def __init__(self, *args, **kwargs): self.contribution = kwargs.pop('contribution') self.questionnaire = kwargs.pop('questionnaire') super().__init__(*args, **kwargs) for question in self.questionnaire.question_set.all(): # generic arguments for all kinds of fields field_args = dict(label=question.text) if question.is_text_question: field = forms.CharField(required=False, widget=forms.Textarea(), **field_args) elif question.is_likert_question: field = forms.TypedChoiceField(widget=forms.RadioSelect(), choices=LIKERT_CHOICES,