コード例 #1
0
ファイル: test_utils.py プロジェクト: jibaku/tanuki
 def test_get_choices(self):
     """Test get_choices function."""
     self.assertEqual(
         get_choices("1,2"),
         (('1', '1'), ('2', '2'))
     )
     self.assertEqual(
         get_choices("a,b"),
         (('a', 'a'), ('b', 'b'))
     )
     self.assertEqual(
         get_choices("1;2", separator=";"),
         (('1', '1'), ('2', '2'))
     )
コード例 #2
0
ファイル: forms.py プロジェクト: jibaku/tanuki
    def __init__(self, *args, **kwargs):
        """A survey object need to be passed as the survey kwargs."""
        empty_tuple = ('', '-------------')
        survey = kwargs.pop('survey')
        self.survey = survey
        self.user = kwargs.pop('user')
        self.separator = self.survey.separator
        self.callback_code = kwargs.pop('callback_code', None)
        try:
            self.step = int(kwargs.pop('step'))
        except KeyError:
            self.step = None

        super(ResponseForm, self).__init__(*args, **kwargs)
        random_uuid = uuid.uuid4().hex
        self.uuid = random_uuid
        self.fields['tanuki_callback_code'] = forms.CharField(
            widget=forms.HiddenInput, required=False
        )
        self.fields['tanuki_callback_code'].initial = self.callback_code
        self.steps_count = survey.questions().count()
        # add a field for each survey question, corresponding to the question
        # type as appropriate.
        data = kwargs.get('data')
        for index, q in enumerate(survey.questions()):
            if (self.survey.display_by_question and
               index != self.step and self.step is not None):
                continue
            else:
                field_name = "question_%d" % q.pk
                if q.question_type == Question.TEXT:
                    self.fields[field_name] = forms.CharField(
                        label=q.text,
                        widget=forms.Textarea
                    )
                elif q.question_type == Question.SHORT_TEXT:
                    self.fields[field_name] = forms.CharField(
                        label=q.text,
                        widget=forms.TextInput
                    )
                elif q.question_type == Question.RADIO:
                    question_choices = get_choices(q.choices,
                                                   separator=self.separator)
                    self.fields[field_name] = forms.ChoiceField(
                        label=q.text,
                        widget=forms.RadioSelect(
                            renderer=HorizontalRadioRenderer
                        ),
                        choices=question_choices
                    )
                elif q.question_type == Question.SELECT:
                    question_choices = get_choices(q.choices,
                                                   separator=self.separator)
                    # add an empty option at the top so that the user has to
                    # explicitly select one of the options
                    question_choices = tuple([empty_tuple]) + question_choices
                    self.fields[field_name] = forms.ChoiceField(
                        label=q.text,
                        widget=forms.Select,
                        choices=question_choices
                    )
                elif q.question_type == Question.SELECT_IMAGE:
                    question_choices = get_choices(q.choices,
                                                   separator=self.separator)
                    # add an empty option at the top so that the user has to
                    # explicitly select one of the options
                    question_choices = tuple([empty_tuple]) + question_choices
                    self.fields[field_name] = forms.ChoiceField(
                        label=q.text,
                        widget=ImageSelectWidget,
                        choices=question_choices
                    )
                elif q.question_type == Question.SELECT_MULTIPLE:
                    question_choices = get_choices(q.choices,
                                                   separator=self.separator)
                    self.fields[field_name] = forms.MultipleChoiceField(
                        label=q.text,
                        widget=forms.CheckboxSelectMultiple,
                        choices=question_choices
                    )
                elif q.question_type == Question.INTEGER:
                    self.fields[field_name] = forms.IntegerField(label=q.text)
                # if the field is required, give it a corresponding css class.
                if q.required:
                    self.fields[field_name].required = True
                    self.fields[field_name].widget.attrs["class"] = "required"
                    self.fields[field_name].widget.attrs["required"] = True
                else:
                    self.fields[field_name].required = False
                # add the category as a css class, and add it as a data
                # attribute as well (this is used in the template to allow
                # sorting the questions by category)
                if q.category:
                    cat_name = q.category.name
                    classes = self.fields[field_name].widget.attrs.get("class")
                    category_class = " cat_%s" % q.category.name
                    if classes:
                        new_classes = classes + (category_class)
                    else:
                        new_classes = (category_class)
                    self.fields[field_name].widget.attrs["class"] = new_classes
                    self.fields[field_name].widget.attrs["category"] = cat_name

                classes = self.fields[field_name].widget.attrs.get("class")
                if q.question_type == Question.SELECT:
                    new_classes = classes + (" cs-select cs-skin-boxes")
                elif q.question_type == Question.RADIO:
                    new_classes = classes + (
                        " fs-radio-group fs-radio-custom clearfix")
                # elif q.question_type == Question.SELECT_MULTIPLE:
                #    new_classes = classes + (" ")
                self.fields[field_name].widget.attrs["class"] = new_classes

                # initialize the form field with values from a POST request, if
                # any.
                if data:
                    self.fields[field_name].initial = data.get(field_name)
コード例 #3
0
ファイル: models.py プロジェクト: jibaku/tanuki
 def get_choices(self):
     return get_choices(self.choices, separator=self.survey.separator)