Ticket #2135: urlify.4.diff

File urlify.4.diff, 6.2 KB (added by greg[at]abbas.org, 18 years ago)

fix strip & doctest

  • django/db/models/base.py

     
    22import django.db.models.manager
    33from django.core import validators
    44from django.core.exceptions import ObjectDoesNotExist
    5 from django.db.models.fields import AutoField, ImageField, FieldDoesNotExist
     5from django.db.models.fields import AutoField, ImageField, FieldDoesNotExist, URLify
    66from django.db.models.fields.related import OneToOneRel, ManyToOneRel
    77from django.db.models.related import RelatedObject
    88from django.db.models.query import orderlist2sql, delete_objects
     
    144144        dispatcher.send(signal=signals.class_prepared, sender=cls)
    145145
    146146    _prepare = classmethod(_prepare)
     147   
     148    def prepopulate(self):
     149        for f in self._meta.fields:
     150            if not f.prepopulate_from: continue
     151            if getattr(self, f.attname): continue
     152            value = [getattr(self, p) for p in f.prepopulate_from]
     153            value = URLify(' '.join(value), f.maxlength)
     154            setattr(self, f.attname, value)
    147155
    148156    def save(self):
    149157        dispatcher.send(signal=signals.pre_save, sender=self.__class__, instance=self)
  • django/db/models/fields/__init__.py

     
    77from django.utils.functional import curry, lazy
    88from django.utils.text import capfirst
    99from django.utils.translation import gettext, gettext_lazy, ngettext
    10 import datetime, os, time
     10import datetime, os, time, re
    1111
    1212class NOT_PROVIDED:
    1313    pass
     
    702702
    703703    def get_manipulator_field_objs(self):
    704704        return [forms.TextField]
     705   
     706def URLify(s, num_chars):
     707    '''
     708    Changes, e.g., "Petty theft" to "petty_theft".
     709    This function is the Python equivalent of the javascript function
     710    of the same name in django/contrib/admin/media/js/urlify.js.
     711    It can get invoked for any field that has a prepopulate_from
     712    attribute defined, although it only really makes sense for
     713    SlugFields.
     714   
     715    NOTE: this implementation corresponds to the Python implementation
     716          of the same algorithm in django/contrib/admin/media/js/urlify.js
     717    '''
     718    # remove all these words from the string before urlifying
     719    removelist = ["a", "an", "as", "at", "before", "but", "by", "for",
     720                  "from", "is", "in", "into", "like", "of", "off", "on",
     721                  "onto", "per", "since", "than", "the", "this", "that",
     722                  "to", "up", "via", "with"]
     723    ignore_words = '|'.join([r for r in removelist])
     724    ignore_words_pat = re.compile(r'\b(%s)\b' % ignore_words, re.I)
     725    ignore_chars_pat = re.compile(r'[^-a-z0-9\s]')
     726    inside_space_pat = re.compile(r'[-\s]+')
     727   
     728    s = s.lower()                    # convert to lowercase
     729    s = ignore_words_pat.sub('', s)  # remove unimportant words
     730    s = ignore_chars_pat.sub('', s)  # remove unneeded chars
     731    s = s.strip()                    # trim leading/trailing spaces
     732    s = inside_space_pat.sub('-', s) # convert remaining spaces to hyphens
     733    if num_chars is not None:
     734        s = s[:num_chars]            # trim to first num_chars chars
     735    return s
    705736
    706737class SmallIntegerField(IntegerField):
    707738    def get_manipulator_field_objs(self):
  • django/contrib/admin/media/js/urlify.js

     
    11function URLify(s, num_chars) {
    22    // changes, e.g., "Petty theft" to "petty_theft"
    33    // remove all these words from the string before urlifying
     4    // NOTE: this implementation corresponds to the Python implementation
     5    //       of the same algorithm in django/db/models/fields/__init__.py
    46    removelist = ["a", "an", "as", "at", "before", "but", "by", "for", "from",
    57                  "is", "in", "into", "like", "of", "off", "on", "onto", "per",
    68                  "since", "than", "the", "this", "that", "to", "up", "via",
  • django/template/defaultfilters.py

     
    33from django.template import resolve_variable, Library
    44from django.conf import settings
    55from django.utils.translation import gettext
     6from django.db.models.fields import URLify
    67import re
    78import random as random_module
    89
     
    6364    """
    6465    return list(str(value))
    6566
    66 def slugify(value):
    67     "Converts to lowercase, removes non-alpha chars and converts spaces to hyphens"
    68     value = re.sub('[^\w\s-]', '', value).strip().lower()
    69     return re.sub('[-\s]+', '-', value)
     67def slugify(value, maxlen=None):
     68    """
     69    Converts to lowercase, removes non-alpha chars and converts spaces
     70    to hyphens
     71    """
     72    return URLify(value, maxlen)
    7073
    7174def stringformat(value, arg):
    7275    """
  • tests/othertests/defaultfilters.py

     
    4040['1', '2', '3', '4']
    4141
    4242>>> slugify(' Jack & Jill like numbers 1,2,3 and 4 and silly characters ?%.$!/')
    43 'jack-jill-like-numbers-123-and-4-and-silly-characters'
     43'jack-jill-numbers-123-and-4-and-silly-characters'
    4444
     45>>> slugify('the test of testers  ')
     46'test-testers'
     47
    4548>>> stringformat(1, '03d')
    4649'001'
    4750
  • docs/templates.txt

     
    993993slugify
    994994~~~~~~~
    995995
    996 Converts to lowercase, removes non-word characters (alphanumerics and
    997 underscores) and converts spaces to hyphens. Also strips leading and trailing
    998 whitespace.
     996Converts to lowercase, removes unimportant words, removes non-word
     997characters (everything but letters and digits), and converts spaces to
     998hyphens. Also strips leading and trailing whitespace.
    999999
    10001000stringformat
    10011001~~~~~~~~~~~~
Back to Top