You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.1 KiB
37 lines
1.1 KiB
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() |