Skip to content

Change slugs to ascii only #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions dynamic_filenames.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
import uuid
from string import Formatter

try: # use unicode-slugify library if installed
from slugify import slugify
except ImportError:
from django.utils.text import slugify

def slugify(value):
try: # use unicode-slugify library if installed
from slugify import slugify as _slugify
return _slugify(value, only_ascii=True)
except ImportError:
from django.utils.text import slugify as _slugify
return _slugify(value, allow_unicode=False)


class SlugFormatter(Formatter):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_dynamic_filenames.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ def test_call__name_override(self):

def test_call__slug(self):
assert FilePattern(filename_pattern='{instance.title:slug}{ext}')(
instance=DefaultModel(title='best model'), filename='some_file.txt'
) == 'best-model.txt'
instance=DefaultModel(title='best model with ünicode'), filename='some_file.txt'
) == 'best-model-with-unicode.txt'

def test_call__slug_precision(self):
assert FilePattern(filename_pattern='{instance.title:.4slug}{ext}')(
Expand Down