Commits

Venelin Stoykov committed 87dbd97

Improve default behaviour when unidecode is installed

Use Django's slugify function but as argument pass a unidecoded string

Comments (0)

Files changed (2)

autoslug/settings.py

 
 """
 from django.conf import settings
+from django.core.urlresolvers import get_callable
 
 # use custom slugifying function if any
-slugify = getattr(settings, 'AUTOSLUG_SLUGIFY_FUNCTION', None)
-
-if not slugify:
-    try:
-        # i18n-friendly approach
-        from unidecode import unidecode
-        slugify = lambda s: unidecode(s).replace(' ', '-')
-    except ImportError:
-        try:
-            # Cyrillic transliteration (primarily Russian)
-            from pytils.translit import slugify
-        except ImportError:
-            # fall back to Django's default method
-            slugify = 'django.template.defaultfilters.slugify'
-
-# find callable by string
-if isinstance(slugify, str):
-    from django.core.urlresolvers import get_callable
-    slugify = get_callable(slugify)
+slugify_function_path = getattr(settings, 'AUTOSLUG_SLUGIFY_FUNCTION', 'autoslug.utils.slugify')
+slugify = get_callable(slugify_function_path)

autoslug/utils.py

 # django
 from django.core.exceptions import ImproperlyConfigured
 from django.db.models.fields import FieldDoesNotExist, DateField
+from django.template.defaultfilters import slugify as django_slugify
+
+try:
+    # i18n-friendly approach
+    from unidecode import unidecode
+except ImportError:
+    try:
+        # Cyrillic transliteration (primarily Russian)
+        from pytils.translit import slugify
+    except ImportError:
+        # fall back to Django's default method
+        slugify = django_slugify
+else:
+    # Use Django's default method over decoded string
+    def slugify(value):
+        return django_slugify(unidecode(value))
 
 
 def get_prepopulated_value(field, instance):