Ticket #2135: urlify.diff
File urlify.diff, 3.2 KB (added by , 18 years ago) |
---|
-
django/db/models/base.py
2 2 import django.db.models.manager 3 3 from django.core import validators 4 4 from django.core.exceptions import ObjectDoesNotExist 5 from django.db.models.fields import AutoField, ImageField, FieldDoesNotExist 5 from django.db.models.fields import AutoField, ImageField, FieldDoesNotExist, URLify 6 6 from django.db.models.fields.related import OneToOneRel, ManyToOneRel 7 7 from django.db.models.related import RelatedObject 8 8 from django.db.models.query import orderlist2sql, delete_objects … … 144 144 dispatcher.send(signal=signals.class_prepared, sender=cls) 145 145 146 146 _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) 147 155 148 156 def save(self): 149 157 dispatcher.send(signal=signals.pre_save, sender=self.__class__, instance=self) -
django/db/models/fields/__init__.py
7 7 from django.utils.functional import curry, lazy 8 8 from django.utils.text import capfirst 9 9 from django.utils.translation import gettext, gettext_lazy, ngettext 10 import datetime, os, time 10 import datetime, os, time, re 11 11 12 12 class NOT_PROVIDED: 13 13 pass … … 702 702 703 703 def get_manipulator_field_objs(self): 704 704 return [forms.TextField] 705 706 def 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 # remove all these words from the string before urlifying 716 removelist = ["a", "an", "as", "at", "before", "but", "by", "for", 717 "from", "is", "in", "into", "like", "of", "off", "on", 718 "onto", "per", "since", "than", "the", "this", "that", 719 "to", "up", "via", "with"] 720 ignore_words_pat = re.compile('|'.join([r'\b%s\b' % r for r in removelist]), re.I) 721 ignore_chars_pat = re.compile(r'[^-A-Z0-9\s]', re.I) 722 outside_space_pat = re.compile(r'^\s+|\s+$') 723 inside_space_pat = re.compile(r'[-\s]+') 724 725 s = ignore_words_pat.sub('', s) # remove unimportant words 726 s = ignore_chars_pat.sub('', s) # remove unneeded chars 727 s = outside_space_pat.sub('', s) # trim leading/trailing spaces 728 s = inside_space_pat.sub('-', s) # convert spaces to hyphens 729 s = s.lower() # convert to lowercase 730 return s[:num_chars] # trim to first num_chars chars 705 731 706 732 class SmallIntegerField(IntegerField): 707 733 def get_manipulator_field_objs(self):