Commits

Andy Mikhailenko committed 0332968

Allow for custom slugify functions

  • Participants
  • Parent commits 99082c3

Comments (0)

Files changed (2)

File autoslugfield/fields.py

 # Original snippet: http://djangosnippets.org/snippets/728/
 # Reworked by Andy Mikhailenko <neithere at gmail dot com> in Sep 2008
 
-from pytils.translit import slugify
+# django
+from django.db.models.fields import SlugField
 
-from django.db.models.fields import SlugField
+# app
+from autoslugfield.settings import slugify
 
 class AutoSlugField(SlugField):
     """ A slug field which can automatically do the following two tasks on save:
     def _get_unique_slug(self, model_instance, slug):
         model = model_instance.__class__
         field_name = self.name
-        index = 0
+        index = 1
         orig_slug = slug
         while True:
             try:

File autoslugfield/settings.py

+# -*- coding: utf-8 -*-
+
+from django.conf import settings
+
+""" the AUTOSLUGFIELD_SLUGIFY_FUNCTION setting allows to define
+a custom slugifying function. Value can be a string or a callable.
+Default value is 'django.template.defaultfilters.slugify'.
+"""
+
+slugify = getattr(settings, 'AUTOSLUGFIELD_SLUGIFY_FUNCTION', # use custom slugifying function...
+    'django.template.defaultfilters.slugify' # ...or fall back to Django's default one
+)
+
+if isinstance(slugify, str):
+    from django.core.urlresolvers import get_callable
+    slugify = get_callable(slugify)