neithere / django-autoslug

AutoSlugField for Django. Supports (but not requires) pytils for Cyrillic transliteration. Any suggestions about adding other translit libs are welcome. Documentation: http://packages.python.org/django-autoslug/

Changed (Δ1.5 KB):

raw changeset »

AUTHORS (3 lines added, 1 lines removed)

autoslug/__init__.py (1 lines added, 1 lines removed)

autoslug/fields.py (13 lines added, 1 lines removed)

autoslug/models.py (null-size change)

autoslug/tests.py (31 lines added, 0 lines removed)

run_tests.py (14 lines added, 0 lines removed)

Up to file-list AUTHORS:

@@ -6,5 +6,7 @@ And here is a probably incomplete list o
6
6
who have submitted patches, reported bugs, added translations and
7
7
generally made django-autoslug better:
8
8
9
* Blake Imsland,
9
* Steve Steiner
10
* Blake Imsland
11
* Ollie Rutherfurd
10
12
* Your Name Here  ;)

Up to file-list autoslug/__init__.py:

12
12
__author__  = 'Andy Mikhailenko'
13
13
__license__ = 'GNU Lesser General Public License (GPL), Version 3'
14
14
__url__     = 'http://bitbucket.org/neithere/django-autoslug/'
15
__version__ = '1.1.2'
15
__version__ = '1.1.3'

Up to file-list autoslug/fields.py:

@@ -18,6 +18,9 @@ from django.db.models.fields import Fiel
18
18
# app
19
19
from autoslug.settings import slugify
20
20
21
22
SLUG_INDEX_SEPARATOR = '-'    # the "-" in "foo-2"
23
21
24
class AutoSlugField(SlugField):
22
25
    """
23
26
    AutoSlugField is an extended SlugField able to automatically resolve name
@@ -222,7 +225,10 @@ class AutoSlugField(SlugField):
222
225
        model = instance.__class__
223
226
        field_name = self.name
224
227
        index = 1
228
        if self.max_length < len(slug):
229
            slug = slug[:self.max_length]
225
230
        orig_slug = slug
231
        sep = SLUG_INDEX_SEPARATOR
226
232
        # keep changing the slug until it is unique
227
233
        while True:
228
234
            try:
@@ -233,7 +239,13 @@ class AutoSlugField(SlugField):
233
239
                    raise model.DoesNotExist
234
240
                # the slug is not unique; change once more
235
241
                index += 1
236
                slug = '%s-%d' % (orig_slug, index)
242
                # ensure the resulting string is not too long
243
                tail_length = len(sep) + len(str(index))
244
                combined_length = len(orig_slug) + tail_length
245
                if self.max_length < combined_length:
246
                    orig_slug = orig_slug[:self.max_length - tail_length]
247
                # re-generate the slug
248
                slug = '%s%s%d' % (orig_slug, sep, index)
237
249
            except model.DoesNotExist:
238
250
                # slug is unique, no model uses it
239
251
                return slug

Up to file-list autoslug/tests.py:

1
# -*- coding: utf-8 -*-
2
#
3
#  Copyright (c) 2008—2009 Andy Mikhailenko
4
#
5
#  This file is part of django-autoslug.
6
#
7
#  django-autoslug is free software under terms of the GNU Lesser
8
#  General Public License version 3 (LGPLv3) as published by the Free
9
#  Software Foundation. See the file README for copying conditions.
10
#
11
12
13
from django.db.models import Model, CharField
14
from autoslug.fields import AutoSlugField
15
16
17
class Foo(Model):
18
    name = CharField(max_length=200)
19
    slug = AutoSlugField(populate_from='name', unique=True)
20
21
__doc__ = """
22
>>> long_name = 'x' * 250
23
>>> foo = Foo(name=long_name)
24
>>> foo.save()
25
>>> len(foo.slug)
26
50
27
>>> bar = Foo(name=long_name)
28
>>> bar.save()
29
>>> [len(x.slug) for x in Foo.objects.all()]
30
[50, 50]
31
"""

Up to file-list run_tests.py:

1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
4
from django.conf import settings
5
from django.core.management import call_command
6
7
8
settings.configure(
9
    INSTALLED_APPS=('autoslug',),
10
    DATABASE_ENGINE='sqlite3'
11
)
12
13
if __name__ == "__main__":
14
    call_command('test', 'autoslug')