import unicodedata, re from django.utils.encoding import smart_text from django.core.exceptions import ObjectDoesNotExist def unique_slug(queryset, slug_field, slug): """ Ensures a slug is unique for the given queryset, appending an integer to its end until the slug is unique. """ i = 0 while True: if i > 0: if i > 1: slug = slug.rsplit("-", 1)[0] slug = "%s-%s" % (slug, i) try: queryset.get(**{slug_field: slug}) except ObjectDoesNotExist: break i += 1 return slug def slugify(s): """ Replacement for Django's slugify which allows unicode chars in slugs, for URLs in Chinese, Russian, etc. Adopted from https://github.com/mozilla/unicode-slugify/ """ chars = [] for char in smart_text(s): cat = unicodedata.category(char)[0] if cat in "LN" or char in "-_~": chars.append(char) elif cat == "Z": chars.append(" ") return re.sub("[-\s]+", "-", "".join(chars).strip()).lower()