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