From 539e53ccf1b9da4acd095a4aeca4ab6ccc441543 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Thu, 20 Oct 2005 04:20:52 +0000 Subject: [PATCH] Fixed #317 -- SlugField now accepts hyphens. Thanks, Sune git-svn-id: http://code.djangoproject.com/svn/django/trunk@968 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/admin/media/js/urlify.js | 9 ++++----- django/core/meta/fields.py | 2 +- django/core/validators.py | 7 ++++++- docs/model-api.txt | 4 ++-- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/django/contrib/admin/media/js/urlify.js b/django/contrib/admin/media/js/urlify.js index 412130ad6f..1e0f04b9ab 100644 --- a/django/contrib/admin/media/js/urlify.js +++ b/django/contrib/admin/media/js/urlify.js @@ -1,14 +1,13 @@ function URLify(s, num_chars) { // changes, e.g., "Petty theft" to "petty_theft" - // remove all these words from the string before urlifying - removelist = ["a", "an", "as", "at", "before", "but", "by", "for", "from", - "is", "in", "into", "like", "of", "off", "on", "onto", "per", - "since", "than", "the", "this", "that", "to", "up", "via", + removelist = ["a", "an", "as", "at", "before", "but", "by", "for", "from", + "is", "in", "into", "like", "of", "off", "on", "onto", "per", + "since", "than", "the", "this", "that", "to", "up", "via", "with"]; r = new RegExp('\\b(' + removelist.join('|') + ')\\b', 'gi'); s = s.replace(r, ''); - s = s.replace(/[^\w\s]/g, ''); // remove unneeded chars + s = s.replace(/[^\w\s-]/g, ''); // remove unneeded chars s = s.replace(/^\s+|\s+$/g, ''); // trim leading/trailing spaces s = s.replace(/\s+/g, '_'); // convert spaces to underscores s = s.toLowerCase(); // convert to lowercase diff --git a/django/core/meta/fields.py b/django/core/meta/fields.py index 0740010579..5d82e61dcd 100644 --- a/django/core/meta/fields.py +++ b/django/core/meta/fields.py @@ -500,7 +500,7 @@ class PositiveSmallIntegerField(IntegerField): class SlugField(Field): def __init__(self, *args, **kwargs): kwargs['maxlength'] = 50 - kwargs.setdefault('validator_list', []).append(validators.isAlphaNumeric) + kwargs.setdefault('validator_list', []).append(validators.isSlug) # Set db_index=True unless it's been set manually. if not kwargs.has_key('db_index'): kwargs['db_index'] = True diff --git a/django/core/validators.py b/django/core/validators.py index 697c2efe81..09a733f348 100644 --- a/django/core/validators.py +++ b/django/core/validators.py @@ -21,6 +21,7 @@ email_re = re.compile(r'^[-\w.+]+@\w[\w.-]+$') integer_re = re.compile(r'^-?\d+$') ip4_re = re.compile(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$') phone_re = re.compile(r'^[A-PR-Y0-9]{3}-[A-PR-Y0-9]{3}-[A-PR-Y0-9]{4}$', re.IGNORECASE) +slug_re = re.compile(r'^[-\w]+$') url_re = re.compile(r'^http://\S+$') from django.conf.settings import JING_PATH @@ -57,7 +58,11 @@ def isAlphaNumeric(field_data, all_data): def isAlphaNumericURL(field_data, all_data): if not alnumurl_re.search(field_data): - raise ValidationError, "This value must contain only letters, numbers, underscores and slashes." + raise ValidationError, "This value must contain only letters, numbers, underscores or slashes." + +def isSlug(field_data, all_data): + if not slug_re.search(field_data): + raise ValidationError, "This value must contain only letters, numbers, underscores or hyphens." def isLowerCase(field_data, all_data): if field_data.lower() != field_data: diff --git a/docs/model-api.txt b/docs/model-api.txt index ef203074a3..f355f9fd5e 100644 --- a/docs/model-api.txt +++ b/docs/model-api.txt @@ -369,8 +369,8 @@ Here are all available field types: ``SlugField`` "Slug" is a newspaper term. A slug is a short label for something, - containing only letters, numbers and underscores. They're generally used in - URLs. + containing only letters, numbers, underscores or hyphens. They're generally + used in URLs. Implies ``maxlength=50`` and ``db_index=True``.