blob: a2d9fd94076ebdde7f39cb5e550f03f6c56fdf6f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
"""
Tagging components for Django's form library.
"""
from django import forms
from django.utils.translation import ugettext as _
from tagging import settings
from tagging.models import Tag
from tagging.utils import parse_tag_input
class TagAdminForm(forms.ModelForm):
class Meta:
model = Tag
def clean_name(self):
value = self.cleaned_data['name']
tag_names = parse_tag_input(value)
if len(tag_names) > 1:
raise forms.ValidationError(_('Multiple tags were given.'))
elif len(tag_names[0]) > settings.MAX_TAG_LENGTH:
raise forms.ValidationError(
_('A tag may be no more than %s characters long.') %
settings.MAX_TAG_LENGTH)
return value
class TagField(forms.CharField):
"""
A ``CharField`` which validates that its input is a valid list of
tag names.
"""
def clean(self, value):
value = super(TagField, self).clean(value)
if value == u'':
return value
for tag_name in parse_tag_input(value):
if len(tag_name) > settings.MAX_TAG_LENGTH:
raise forms.ValidationError(
_('Each tag may be no more than %s characters long.') %
settings.MAX_TAG_LENGTH)
return value
|