def test_typedmultiplechoicefield_76(self):
     # If you want cleaning an empty value to return a different type,
     # tell the field
     f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")],
                                  coerce=int, required=False,
                                  empty_value=None)
     eq_(None, f.clean([]))
Beispiel #2
0
 def test_typedmultiplechoicefield_76(self):
     # If you want cleaning an empty value to return a different type,
     # tell the field
     f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")],
                                  coerce=int, required=False,
                                  empty_value=None)
     eq_(None, f.clean([]))
 def test_typedmultiplechoicefield_71(self):
     f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")],
                                  coerce=int)
     eq_([1], f.clean(['1']))
     self.assertRaisesErrorWithMessage(
         ValidationError,
         "[u'Select a valid choice. 2 is not one of the available choices."
         "']", f.clean, ['2'])
Beispiel #4
0
 def test_typedmultiplechoicefield_71(self):
     f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")],
                                  coerce=int)
     eq_([1], f.clean(['1']))
     self.assertRaisesErrorWithMessage(
         ValidationError,
         "[u'Select a valid choice. 2 is not one of the available choices."
         "']", f.clean, ['2'])
Beispiel #5
0
class WikiSearchForm(SearchForm):
    """Django form specifically for querying the knowledge base data. """
    language = forms.ChoiceField(required=False,
                                 label=_lazy(u'Language'),
                                 choices=SEARCH_LANGUAGES)

    category = TypedMultipleChoiceField(
        required=False,
        coerce=int,
        label=_lazy(u'Category'),
        choices=CATEGORIES,
        coerce_only=True,
    )

    topics = forms.MultipleChoiceField(required=False, label=_lazy(u'Topics'))

    product = forms.MultipleChoiceField(
        required=False,
        label=_lazy(u'Relevant to'),
    )

    def __init__(self, *args, **kwargs):
        super(SearchForm, self).__init__(*args, **kwargs)

        product_field = self.fields['product']
        product_field.choices = Product.objects.values_list('slug', 'title')

        topics_field = self.fields['topics']
        topics_field.choices = (Topic.objects.values_list('slug',
                                                          'title').distinct())
Beispiel #6
0
 def test_typedmultiplechoicefield_74(self):
     # Even more weirdness: if you have a valid choice but your coercion
     # function can't coerce, you'll still get a validation error.
     # Don't do this!
     f = TypedMultipleChoiceField(choices=[('A', 'A'), ('B', 'B')],
                                  coerce=int)
     self.assertRaisesErrorWithMessage(
         ValidationError,
         "[u'Select a valid choice. B is not one of the available choices."
         "']", f.clean, ['B'])
     # Required fields require values
     self.assertRaisesErrorWithMessage(
         ValidationError, "[u'This field is required.']", f.clean, [])
 def test_typedmultiplechoicefield_75(self):
     # Non-required fields aren't required
     f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")],
                                  coerce=int, required=False)
     eq_([], f.clean([]))
 def test_typedmultiplechoicefield_73(self):
     # This can also cause weirdness: bool(-1) == True
     f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")],
                                  coerce=bool)
     eq_([True], f.clean(['-1']))
 def test_typedmultiplechoicefield_72(self):
     # Different coercion, same validation.
     f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")],
                                  coerce=float)
     eq_([1.0], f.clean(['1']))
 def test_coerce_only(self):
     """No validation error raised in this case."""
     f = TypedMultipleChoiceField(choices=[(1, '+1')], coerce=int,
                                  coerce_only=True)
     eq_([], f.clean(['2']))
Beispiel #11
0
class SearchForm(forms.Form):
    """Django form for handling display and validation"""

    def __init__(self, *args, **kwargs):
        super(SearchForm, self).__init__(*args, **kwargs)

        product_field = self.fields['product']
        product_field.choices = Product.objects.values_list('slug', 'title')

        topics_field = self.fields['topics']
        topics_field.choices = Topic.objects.values_list(
            'slug', 'title').distinct()

    def clean(self):
        """Clean up data and set defaults"""
        c = self.cleaned_data

        if ('a' not in c or not c['a']) and c['q'] == '':
            raise ValidationError('Basic search requires a query string.')

        # Validate created and updated dates
        date_fields = (('created', 'created_date'),
                       ('updated', 'updated_date'))
        for field_option, field_date in date_fields:
            if c[field_date] != '':
                try:
                    created_timestamp = time.mktime(
                        time.strptime(c[field_date], '%m/%d/%Y'))
                    c[field_date] = int(created_timestamp)
                except (ValueError, OverflowError):
                    c[field_option] = None
            else:
                c[field_option] = None

        # Empty value defaults to int
        c['num_votes'] = c.get('num_votes') or 0
        return c

    # Common fields
    q = forms.CharField(required=False)

    w = forms.TypedChoiceField(required=False, coerce=int,
                               widget=forms.HiddenInput,
                               empty_value=constants.WHERE_BASIC,
                               choices=((constants.WHERE_SUPPORT, None),
                                        (constants.WHERE_WIKI, None),
                                        (constants.WHERE_BASIC, None),
                                        (constants.WHERE_DISCUSSION, None)))

    a = forms.IntegerField(required=False, widget=forms.HiddenInput)

    # KB fields
    topics = forms.MultipleChoiceField(
        required=False,
        widget=forms.CheckboxSelectMultiple(),
        label=_lazy('Topics'))

    language = forms.ChoiceField(required=False, label=_lazy('Language'),
                                 choices=SEARCH_LANGUAGES)

    category = TypedMultipleChoiceField(
        required=False, coerce=int, widget=forms.CheckboxSelectMultiple,
        label=_lazy('Category'), choices=CATEGORIES, coerce_only=True)

    product = forms.MultipleChoiceField(
        required=False,
        label=_lazy('Relevant to'),
        widget=forms.CheckboxSelectMultiple())

    include_archived = forms.BooleanField(
        required=False, label=_lazy('Include obsolete articles?'))

    sortby_documents = forms.TypedChoiceField(
        required=False,
        empty_value=constants.SORTBY_DOCUMENTS_CHOICES[0][0],
        label=_lazy('Sort results by'),
        choices=constants.SORTBY_DOCUMENTS_CHOICES)

    # Support questions and discussion forums fields
    created = forms.TypedChoiceField(
        required=False, coerce=int, empty_value=0,
        label=_lazy('Created'), choices=constants.DATE_LIST)

    created_date = forms.CharField(required=False)

    updated = forms.TypedChoiceField(
        required=False, coerce=int, empty_value=0,
        label=_lazy('Last updated'), choices=constants.DATE_LIST)
    updated_date = forms.CharField(required=False)

    user_widget = forms.TextInput(attrs={'placeholder': _lazy('username'),
                                         'class': 'auto-fill'})
    # Discussion forums fields
    author = forms.CharField(required=False, widget=user_widget)

    sortby = forms.TypedChoiceField(
        required=False, coerce=int, empty_value=0,
        label=_lazy('Sort results by'), choices=constants.SORTBY_FORUMS)

    thread_type = TypedMultipleChoiceField(
        required=False, coerce=int, widget=forms.CheckboxSelectMultiple,
        label=_lazy('Thread type'), choices=constants.DISCUSSION_STATUS_LIST,
        coerce_only=True)

    forum = TypedMultipleChoiceField(
        required=False,
        coerce=int,
        label=_lazy('Search in forum'),
        choices=[],  # Note: set choices with set_allowed_forums
        coerce_only=True)

    # Support questions fields
    asked_by = forms.CharField(required=False, widget=user_widget)
    answered_by = forms.CharField(required=False, widget=user_widget)

    sortby_questions = forms.TypedChoiceField(
        required=False, coerce=int, empty_value=0,
        label=_lazy('Sort results by'), choices=constants.SORTBY_QUESTIONS)

    is_locked = forms.TypedChoiceField(
        required=False, coerce=int, empty_value=0, widget=forms.RadioSelect,
        label=_lazy('Locked'), choices=constants.TERNARY_LIST)

    is_archived = forms.TypedChoiceField(
        required=False, coerce=int, empty_value=0, widget=forms.RadioSelect,
        label=_lazy('Archived'), choices=constants.TERNARY_LIST)

    is_solved = forms.TypedChoiceField(
        required=False, coerce=int, empty_value=0, widget=forms.RadioSelect,
        label=_lazy('Solved'), choices=constants.TERNARY_LIST)

    has_answers = forms.TypedChoiceField(
        required=False, coerce=int, empty_value=0, widget=forms.RadioSelect,
        label=_lazy('Has answers'), choices=constants.TERNARY_LIST)

    has_helpful = forms.TypedChoiceField(
        required=False, coerce=int, empty_value=0, widget=forms.RadioSelect,
        label=_lazy('Has helpful answers'), choices=constants.TERNARY_LIST)

    num_voted = forms.TypedChoiceField(
        required=False, coerce=int, empty_value=0,
        label=_lazy('Votes'), choices=constants.NUMBER_LIST)
    num_votes = forms.IntegerField(required=False)

    tag_widget = forms.TextInput(attrs={'placeholder': _lazy('tag1, tag2'),
                                        'class': 'auto-fill'})
    q_tags = forms.CharField(label=_lazy('Tags'), required=False,
                             widget=tag_widget)

    def set_allowed_forums(self, user):
        """Sets the 'forum' field choices to forums the user can see."""
        forums = [(f.id, f.name)
                  for f in DiscussionForum.authorized_forums_for_user(user)]
        self.fields['forum'].choices = forums
Beispiel #12
0
 def test_typedmultiplechoicefield_75(self):
     # Non-required fields aren't required
     f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")],
                                  coerce=int,
                                  required=False)
     eq_([], f.clean([]))
Beispiel #13
0
 def test_typedmultiplechoicefield_73(self):
     # This can also cause weirdness: bool(-1) == True
     f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")],
                                  coerce=bool)
     eq_([True], f.clean(['-1']))
Beispiel #14
0
 def test_typedmultiplechoicefield_72(self):
     # Different coercion, same validation.
     f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")],
                                  coerce=float)
     eq_([1.0], f.clean(['1']))
Beispiel #15
0
 def test_coerce_only(self):
     """No validation error raised in this case."""
     f = TypedMultipleChoiceField(choices=[(1, '+1')],
                                  coerce=int,
                                  coerce_only=True)
     eq_([], f.clean(['2']))
Beispiel #16
0
class AdvancedSearchForm(BaseSearchForm):
    """Django form for handling display and validation"""

    # Common fields
    # TODO: get rid of this.
    a = forms.IntegerField(required=False, widget=forms.HiddenInput)

    # KB fields
    topics = forms.MultipleChoiceField(required=False,
                                       widget=forms.CheckboxSelectMultiple(),
                                       label=_lazy("Topics"))

    language = forms.ChoiceField(required=False,
                                 label=_lazy("Language"),
                                 choices=SEARCH_LANGUAGES)

    category = TypedMultipleChoiceField(
        required=False,
        coerce=int,
        widget=forms.CheckboxSelectMultiple,
        label=_lazy("Category"),
        choices=CATEGORIES,
        coerce_only=True,
    )

    product = forms.MultipleChoiceField(required=False,
                                        label=_lazy("Relevant to"),
                                        widget=forms.CheckboxSelectMultiple())

    include_archived = forms.BooleanField(
        required=False, label=_lazy("Include obsolete articles?"))

    sortby_documents = forms.TypedChoiceField(
        required=False,
        empty_value=constants.SORTBY_DOCUMENTS_CHOICES[0][0],
        label=_lazy("Sort results by"),
        choices=constants.SORTBY_DOCUMENTS_CHOICES,
    )

    # Support questions and discussion forums fields
    created = forms.TypedChoiceField(
        required=False,
        coerce=int,
        empty_value=0,
        label=_lazy("Created"),
        choices=constants.DATE_LIST,
    )

    created_date = forms.CharField(required=False)

    updated = forms.TypedChoiceField(
        required=False,
        coerce=int,
        empty_value=0,
        label=_lazy("Last updated"),
        choices=constants.DATE_LIST,
    )
    updated_date = forms.CharField(required=False)

    user_widget = forms.TextInput(attrs={
        "placeholder": _lazy("username"),
        "class": "auto-fill"
    })
    # Discussion forums fields
    author = forms.CharField(required=False, widget=user_widget)

    sortby = forms.TypedChoiceField(
        required=False,
        coerce=int,
        empty_value=0,
        label=_lazy("Sort results by"),
        choices=constants.SORTBY_FORUMS,
    )

    thread_type = TypedMultipleChoiceField(
        required=False,
        coerce=int,
        widget=forms.CheckboxSelectMultiple,
        label=_lazy("Thread type"),
        choices=constants.DISCUSSION_STATUS_LIST,
        coerce_only=True,
    )

    forum = TypedMultipleChoiceField(
        required=False,
        coerce=int,
        label=_lazy("Search in forum"),
        choices=[],  # Note: set choices with set_allowed_forums
        coerce_only=True,
    )

    # Support questions fields
    asked_by = forms.CharField(required=False, widget=user_widget)
    answered_by = forms.CharField(required=False, widget=user_widget)

    sortby_questions = forms.TypedChoiceField(
        required=False,
        coerce=int,
        empty_value=0,
        label=_lazy("Sort results by"),
        choices=constants.SORTBY_QUESTIONS,
    )

    is_locked = forms.TypedChoiceField(
        required=False,
        coerce=int,
        empty_value=0,
        widget=forms.RadioSelect,
        label=_lazy("Locked"),
        choices=constants.TERNARY_LIST,
    )

    is_archived = forms.TypedChoiceField(
        required=False,
        coerce=int,
        empty_value=0,
        widget=forms.RadioSelect,
        label=_lazy("Archived"),
        choices=constants.TERNARY_LIST,
    )

    is_solved = forms.TypedChoiceField(
        required=False,
        coerce=int,
        empty_value=0,
        widget=forms.RadioSelect,
        label=_lazy("Solved"),
        choices=constants.TERNARY_LIST,
    )

    has_answers = forms.TypedChoiceField(
        required=False,
        coerce=int,
        empty_value=0,
        widget=forms.RadioSelect,
        label=_lazy("Has answers"),
        choices=constants.TERNARY_LIST,
    )

    has_helpful = forms.TypedChoiceField(
        required=False,
        coerce=int,
        empty_value=0,
        widget=forms.RadioSelect,
        label=_lazy("Has helpful answers"),
        choices=constants.TERNARY_LIST,
    )

    num_voted = forms.TypedChoiceField(
        required=False,
        coerce=int,
        empty_value=0,
        label=_lazy("Votes"),
        choices=constants.NUMBER_LIST,
    )
    num_votes = forms.IntegerField(required=False)

    tag_widget = forms.TextInput(attrs={
        "placeholder": _lazy("tag1, tag2"),
        "class": "auto-fill"
    })
    q_tags = forms.CharField(label=_lazy("Tags"),
                             required=False,
                             widget=tag_widget)

    def __init__(self, *args, **kwargs):
        super(AdvancedSearchForm, self).__init__(*args, **kwargs)

        product_field = self.fields["product"]
        product_field.choices = Product.objects.values_list("slug", "title")

        topics_field = self.fields["topics"]
        topics_field.choices = Topic.objects.values_list("slug",
                                                         "title").distinct()

    def clean(self):
        """Clean up data and set defaults"""
        c = self.cleaned_data

        # Validate created and updated dates
        date_fields = (("created", "created_date"), ("updated",
                                                     "updated_date"))
        for field_option, field_date in date_fields:
            if c[field_date] != "":
                try:
                    created_timestamp = time.mktime(
                        time.strptime(c[field_date], "%m/%d/%Y"))
                    c[field_date] = int(created_timestamp)
                except (ValueError, OverflowError):
                    c[field_option] = None
            else:
                c[field_option] = None

        # Empty value defaults to int
        c["num_votes"] = c.get("num_votes") or 0
        return c

    def clean_category(self):
        category = self.cleaned_data["category"]
        if not category:
            category = settings.SEARCH_DEFAULT_CATEGORIES
        return category

    def set_allowed_forums(self, user):
        """Sets the 'forum' field choices to forums the user can see."""
        forums = [(f.id, f.name)
                  for f in DiscussionForum.authorized_forums_for_user(user)]
        self.fields["forum"].choices = forums