1 from __future__ import unicode_literals
3 from django.template.defaultfilters import slugify as django_slugify
4 from importlib import import_module
5 from unidecode import unidecode
8 # Timezone support with fallback.
10 from django.utils.timezone import now
12 from datetime import datetime
18 Translates unicode into closest possible ascii chars before
21 from future.builtins import str
22 return django_slugify(unidecode(str(s)))
25 def unique_slug(manager, slug_field, slug):
27 Ensure slug is unique for the given manager, appending a digit
34 slug = slug.rsplit("-", 1)[0]
35 slug = "%s-%s" % (slug, i)
36 if not manager.filter(**{slug_field: slug}):
42 def split_choices(choices_string):
44 Convert a comma separated choices string to a list.
46 return [x.strip() for x in choices_string.split(",") if x.strip()]
49 def html5_field(name, base):
51 Takes a Django form field class and returns a subclass of
52 it with the given name as its input type.
54 return type(str(""), (base,), {"input_type": name})
57 def import_attr(path):
59 Given a a Python dotted path to a variable in a module,
60 imports the module and returns the variable in it.
62 module_path, attr_name = path.rsplit(".", 1)
63 return getattr(import_module(module_path), attr_name)