Skip to content

Commit

Permalink
Change slugs to ascii only (#5)
Browse files Browse the repository at this point in the history
Some tools like the AWS CLI don't do well with unicode file names
even though AWS S3 does not have a problem with them. It is saver
to use ASCII characters only. Which is the default for Django's
builtin slugify method anyways.
  • Loading branch information
codingjoe authored Nov 1, 2018
1 parent 45084a7 commit a39e9b6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
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

0 comments on commit a39e9b6

Please sign in to comment.