Skip to content

Commit

Permalink
Allow the setting of a SLUG_TRANSLITERATOR to convert non-ASCII chara…
Browse files Browse the repository at this point in the history
…cters to ASCII characters.
  • Loading branch information
coordt committed Feb 15, 2012
1 parent 65ef584 commit 96a3f61
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
4 changes: 2 additions & 2 deletions categories/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from mptt.managers import TreeManager

from .editor.tree_editor import TreeEditor
from .settings import ALLOW_SLUG_CHANGE
from .settings import ALLOW_SLUG_CHANGE, SLUG_TRANSLITERATOR

class CategoryManager(models.Manager):
"""
Expand Down Expand Up @@ -51,7 +51,7 @@ def save(self, *args, **kwargs):
decendants remain active.
"""
if not self.slug:
self.slug = slugify(self.name)[:50]
self.slug = slugify(SLUG_TRANSLITERATOR(self.name))[:50]

super(CategoryBase, self).save(*args, **kwargs)

Expand Down
6 changes: 4 additions & 2 deletions categories/management/commands/import_categories.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from django.core.management.base import BaseCommand, CommandError
from categories.models import Category
from django.template.defaultfilters import slugify
from django.db import transaction

from categories.models import Category
from categories.settings import SLUG_TRANSLITERATOR

class Command(BaseCommand):
"""Import category trees from a file."""

Expand Down Expand Up @@ -30,7 +32,7 @@ def make_category(self, string, parent=None, order=1):
"""
cat = Category(
name=string.strip(),
slug=slugify(string.strip())[:49],
slug=slugify(SLUG_TRANSLITERATOR(string.strip()))[:49],
#parent=parent,
order=order
)
Expand Down
15 changes: 15 additions & 0 deletions categories/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,25 @@
'THUMBNAIL_UPLOAD_PATH': 'uploads/categories/thumbnails',
'THUMBNAIL_STORAGE': settings.DEFAULT_FILE_STORAGE,
'JAVASCRIPT_URL': getattr(settings, 'STATIC_URL', settings.MEDIA_URL) + 'js/',
'SLUG_TRANSLITERATOR': '',
}

DEFAULT_SETTINGS.update(getattr(settings, 'CATEGORIES_SETTINGS', {}))

if DEFAULT_SETTINGS['SLUG_TRANSLITERATOR']:
if callable(DEFAULT_SETTINGS['SLUG_TRANSLITERATOR']):
pass
elif isinstance(DEFAULT_SETTINGS['SLUG_TRANSLITERATOR'], basestring):
from django.utils.importlib import import_module
bits = DEFAULT_SETTINGS['SLUG_TRANSLITERATOR'].split(".")
module = import_module(".".join(bits[:-1]))
DEFAULT_SETTINGS['SLUG_TRANSLITERATOR'] = getattr(module, bits[-1])
else:
from django.core.exceptions import ImproperlyConfigured
raise ImproperlyConfigured("SLUG_TRANSLITERATOR must be a callable or a string.")
else:
DEFAULT_SETTINGS['SLUG_TRANSLITERATOR'] = lambda x: x

ERR_MSG = "settings.%s is deprecated; use settings.CATEGORIES_SETTINGS instead."

if hasattr(settings, 'CATEGORIES_ALLOW_SLUG_CHANGE'):
Expand Down

0 comments on commit 96a3f61

Please sign in to comment.