Andy Mikhailenko avatar Andy Mikhailenko committed c017ba7

BUGFIX: slug length was only controlled with unique=True.

Comments (0)

Files changed (3)

autoslug/fields.py

 
         assert slug, 'slug is defined before trying to ensure uniqueness'
 
+        slug = self._crop_slug(slug)
+
         # ensure the slug is unique (if required)
         if self.unique or self.unique_with:
             slug = self._generate_unique_slug(instance, slug)
         model = instance.__class__
         field_name = self.name
         index = 1
-        if self.max_length < len(slug):
-            slug = slug[:self.max_length]
+        slug = self._crop_slug(slug)
         orig_slug = slug
         # keep changing the slug until it is unique
         while True:
             # re-generate the slug
             data = dict(slug=orig_slug, sep=self.index_sep, index=index)
             slug = '%(slug)s%(sep)s%(index)d' % data
+
+    def _crop_slug(self, slug):
+        if self.max_length < len(slug):
+            return slug[:self.max_length]
+        return slug

autoslug/tests.py

     >>> a.save()
     >>> len(a.slug)    # original slug is cropped by field length
     50
-    >>> b = ModelWithLongName(name=long_name)
+    """
+    name = CharField(max_length=200)
+    slug = AutoSlugField(populate_from='name')
+
+
+class ModelWithLongNameUnique(Model):
+    """
+    >>> long_name = 'x' * 250
+    >>> a = ModelWithLongNameUnique(name=long_name)
+    >>> a.save()
+    >>> len(a.slug)    # original slug is cropped by field length
+    50
+    >>> b = ModelWithLongNameUnique(name=long_name)
     >>> b.save()
     >>> b.slug[-3:]    # uniqueness is forced
     'x-2'
 
 setup(
     name     = 'django-autoslug',
-    version  = '1.3.4',
+    version  = '1.3.5',
     packages = ['autoslug'],
 
     requires = ['python (>= 2.4)', 'django (>= 1.0)'],
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.