@@ -38,8 +38,8 @@ class VersionSlugField(models.CharField):
38
38
Implementation inspired by ``django_extensions.db.fields.AutoSlugField``.
39
39
"""
40
40
41
- allowed_punctuation = '-._'
42
- allowed_chars = string . lowercase + string . digits + allowed_punctuation
41
+ invalid_chars_re = re . compile ( '[^-._a-z0-9]' )
42
+ leading_punctuation_re = re . compile ( '^[-._]+' )
43
43
placeholder = '-'
44
44
fallback_slug = 'unknown'
45
45
test_pattern = re .compile ('^{pattern}$' .format (pattern = VERSION_SLUG_REGEX ))
@@ -63,22 +63,10 @@ def get_queryset(self, model_cls, slug_field):
63
63
def slugify (self , content ):
64
64
if not content :
65
65
return ''
66
- slugified = ''
67
- content = content .lower ()
68
- for char in content :
69
- if char not in self .allowed_chars :
70
- slugified += self .placeholder
71
- else :
72
- slugified += char
73
-
74
- # Do not start and end in punctuation.
75
- slug_length = len (slugified )
76
- diff = 1
77
- while diff > 0 :
78
- for char in self .allowed_punctuation :
79
- slugified = slugified .strip (char )
80
- diff = slug_length - len (slugified )
81
- slug_length = len (slugified )
66
+
67
+ slugified = content .lower ()
68
+ slugified = self .invalid_chars_re .sub (self .placeholder , slugified )
69
+ slugified = self .leading_punctuation_re .sub ('' , slugified )
82
70
83
71
if not slugified :
84
72
return self .fallback_slug
@@ -152,6 +140,7 @@ def create_slug(self, model_instance):
152
140
slug = slug + end
153
141
kwargs [self .attname ] = slug
154
142
next += 1
143
+
155
144
assert self .test_pattern .match (slug ), (
156
145
'Invalid generated slug: {slug}' .format (slug = slug ))
157
146
return slug
0 commit comments