示例#1
0
class SoundDescriptionForm(forms.Form):
    name = forms.CharField(max_length=512,
                           min_length=5,
                           widget=forms.TextInput(attrs={
                               'size': 65,
                               'class': 'inputText'
                           }))
    is_explicit = forms.BooleanField(required=False)
    tags = TagField(
        widget=forms.Textarea(attrs={
            'cols': 80,
            'rows': 3
        }),
        help_text=
        "<br>Add at least 3 tags, separating them with spaces. Join multi-word tags with dashes. "
        "For example: <i>field-recording</i> is a popular tag."
        "<br>Only use letters a-z and numbers 0-9 with no accents or diacritics"
    )
    description = HtmlCleaningCharField(widget=forms.Textarea(attrs={
        'cols': 80,
        'rows': 10
    }))

    def __init__(self, *args, **kwargs):
        explicit_disable = False
        if 'explicit_disable' in kwargs:
            explicit_disable = kwargs.get('explicit_disable')
            del kwargs['explicit_disable']

        super(SoundDescriptionForm, self).__init__(*args, **kwargs)
        # Disable is_explicit field if is already marked
        self.initial['is_explicit'] = explicit_disable
        self.fields['is_explicit'].disabled = explicit_disable
示例#2
0
class SoundDescriptionForm(forms.Form):
    name = forms.CharField(max_length=512, min_length=5,
                           widget=forms.TextInput(attrs={'size': 65, 'class':'inputText'}))
    tags = TagField(widget=forms.Textarea(attrs={'cols': 80, 'rows': 3}),
                    help_text="<br>Add at least 3 tags, separating them with spaces. Join multi-word tags with dashes. "
                              "For example: field-recording is a popular tag.")
    description = HtmlCleaningCharField(widget=forms.Textarea(attrs={'cols': 80, 'rows': 10}))
示例#3
0
    def test_tag_field(self):
        f = TagField()
        # Split on spaces
        self.assertEqual({"One", "two2", "3three"}, f.clean("3three One two2"))

        # Split on commas
        self.assertEqual({"one", "two", "three"}, f.clean("three, one,two"))

        # Funny characters not allowed
        err_message = "Tags must contain only letters a-z, digits 0-9 and hyphen"
        with self.assertRaisesMessage(ValidationError, err_message):
            f.clean("One t%wo")

        # accents not allowed
        with self.assertRaisesMessage(ValidationError, err_message):
            f.clean("One twó")

        # hyphens allowed
        self.assertEqual({"tag", "tag-name", "another-name"},
                         f.clean("tag-name tag another-name"))

        # multiple hyphens cut down to one
        self.assertEqual({"tag", "tag-name", "another-name"},
                         f.clean("tag--name tag another----name"))

        # minimum number tags
        err_message = "You should add at least 3 different tags. Tags must be separated by spaces"
        with self.assertRaisesMessage(ValidationError, err_message):
            f.clean("One two")

        # maximum number tags
        err_message = "There can be maximum 30 tags, please select the most relevant ones!"
        with self.assertRaisesMessage(ValidationError, err_message):
            tags = " ".join(["tag%s" % i for i in range(35)])
            f.clean(tags)

        # remove common words
        self.assertEqual({"one", "two", "three"},
                         f.clean("three the of one to two one"))

        # duplicate tags removed
        self.assertEqual({"one", "two", "three"},
                         f.clean("three one two three one"))