Ticket #317: slug.patch

File slug.patch, 2.5 KB (added by sune.kirkeby@…, 19 years ago)

Patch to change URLify.js, and add a separate validator for slugs.

  • django/conf/admin_media/js/urlify.js

     
    88                  "with"];
    99    r = new RegExp('\\b(' + removelist.join('|') + ')\\b', 'gi');
    1010    s = s.replace(r, '');
    11     s = s.replace(/[^\w\s]/g, '');   // remove unneeded chars
     11    s = s.replace(/[^\w\s-]/g, '');   // remove unneeded chars
    1212    s = s.replace(/^\s+|\s+$/g, ''); // trim leading/trailing spaces
    13     s = s.replace(/\s+/g, '_');      // convert spaces to underscores
     13    s = s.replace(/\s+/g, '-');      // convert spaces to underscores
    1414    s = s.toLowerCase();             // convert to lowercase
    1515    return s.substring(0, num_chars);// trim to first num_chars chars
    16 }
    17  No newline at end of file
     16}
  • django/core/meta/fields.py

     
    485485class SlugField(Field):
    486486    def __init__(self, *args, **kwargs):
    487487        kwargs['maxlength'] = 50
    488         kwargs.setdefault('validator_list', []).append(validators.isAlphaNumeric)
     488        kwargs.setdefault('validator_list', []).append(validators.isSlug)
    489489        # Set db_index=True unless it's been set manually.
    490490        if not kwargs.has_key('db_index'):
    491491            kwargs['db_index'] = True
  • django/core/validators.py

     
    1414_timere = r'(?:[01]?[0-9]|2[0-3]):[0-5][0-9](?::[0-5][0-9])?'
    1515alnum_re = re.compile(r'^\w+$')
    1616alnumurl_re = re.compile(r'^[\w/]+$')
     17slug_re = re.compile(r'^[\w.-]+$')
    1718ansi_date_re = re.compile('^%s$' % _datere)
    1819ansi_time_re = re.compile('^%s$' % _timere)
    1920ansi_datetime_re = re.compile('^%s %s$' % (_datere, _timere))
     
    5859    if not alnumurl_re.search(field_data):
    5960        raise ValidationError, "This value must contain only letters, numbers, underscores and slashes."
    6061
     62def isSlug(field_data, all_data):
     63    if not slug_re.search(field_data):
     64        raise ValidationError, "This value must contain only letters, numbers, underscores and dashes."
     65
    6166def isLowerCase(field_data, all_data):
    6267    if field_data.lower() != field_data:
    6368        raise ValidationError, "Uppercase letters are not allowed here."
Back to Top