diff options
author | luxagraf <sng@luxagraf.net> | 2010-10-23 19:46:20 -0400 |
---|---|---|
committer | luxagraf <sng@luxagraf.net> | 2010-10-23 19:46:20 -0400 |
commit | c59a2a69fb38b92b6c45bcf7431d2b1a3c5dce3c (patch) | |
tree | 2fc44ca867839d5e591e21467b6e4526f7a9f080 /lib/tagging/forms.py | |
parent | ed77da873e675f02f12cbab9be27f342f825444b (diff) |
added grappelli, filebrowser, chunks and tagging to lcal repo
Diffstat (limited to 'lib/tagging/forms.py')
-rw-r--r-- | lib/tagging/forms.py | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/lib/tagging/forms.py b/lib/tagging/forms.py new file mode 100644 index 0000000..a2d9fd9 --- /dev/null +++ b/lib/tagging/forms.py @@ -0,0 +1,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 |