aboutsummaryrefslogtreecommitdiff
path: root/tagging/validators.py
diff options
context:
space:
mode:
authorSVN-Git Migration <python-modules-team@lists.alioth.debian.org>2015-10-08 11:51:45 -0700
committerSVN-Git Migration <python-modules-team@lists.alioth.debian.org>2015-10-08 11:51:45 -0700
commit2228968f3d51a3d686adb2839bf43e018432f941 (patch)
tree9e95992c4b09bbea50336e91a709a775a20fd1e5 /tagging/validators.py
downloadpython-django-tagging-2228968f3d51a3d686adb2839bf43e018432f941.tar
python-django-tagging-2228968f3d51a3d686adb2839bf43e018432f941.tar.gz
Imported Upstream version 0.1+svn102upstream/0.1+svn102
Diffstat (limited to 'tagging/validators.py')
-rw-r--r--tagging/validators.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/tagging/validators.py b/tagging/validators.py
new file mode 100644
index 0000000..363fa6d
--- /dev/null
+++ b/tagging/validators.py
@@ -0,0 +1,33 @@
+import re
+
+from django.core.validators import ValidationError
+from django.utils.encoding import smart_unicode
+
+from tagging.utils import get_tag_name_list
+
+tag_re = re.compile(r'^[-\w]+$', re.U)
+tag_list_re = re.compile(r'^[-\w]+(?:(?:,\s|[,\s])[-\w]+)*$', re.U)
+
+def isTagList(field_data, all_data):
+ """
+ Validates that ``field_data`` is a valid list of tag names,
+ separated by a single comma, a single space or a comma followed
+ by a space.
+ """
+ if field_data is not None:
+ field_data = smart_unicode(field_data)
+ if not tag_list_re.search(field_data):
+ raise ValidationError(u'Tag names must contain only unicode alphanumeric characters, numbers, underscores or hyphens, with a comma, space or comma followed by space used to separate each tag name.')
+ tag_names = get_tag_name_list(field_data)
+ for tag_name in tag_names:
+ if len(tag_name) > 50:
+ raise ValidationError(u'Tag names must be no longer than 50 characters.')
+
+def isTag(field_data, all_data):
+ """
+ Validates that ``field_data`` is a valid tag name.
+ """
+ if field_data is not None:
+ field_data = smart_unicode(field_data)
+ if not tag_re.match(field_data):
+ raise ValidationError(u'Tag names must contain only unicode alphanumeric characters, numbers, underscores or hyphens.')