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.

utils.py 1.1KB

123456789101112131415161718192021222324252627282930313233
  1. from unidecode import unidecode
  2. from django.conf import settings
  3. def slugify(value):
  4. """
  5. Slugify a string (even if it contains non-ASCII chars)
  6. """
  7. # Re-map some strings to avoid important characters being stripped. Eg
  8. # remap 'c++' to 'cpp' otherwise it will become 'c'.
  9. if hasattr(settings, 'OSCAR_SLUG_MAP'):
  10. for k, v in settings.OSCAR_SLUG_MAP.items():
  11. value = value.replace(k, v)
  12. # Allow an alternative slugify function to be specified
  13. if hasattr(settings, 'OSCAR_SLUG_FUNCTION'):
  14. slugifier = settings.OSCAR_SLUG_FUNCTION
  15. else:
  16. from django.template import defaultfilters
  17. slugifier = defaultfilters.slugify
  18. # Use unidecode to convert non-ASCII strings to ASCII equivalents where
  19. # possible.
  20. value = slugifier(
  21. unidecode(unicode(value)))
  22. # Remove stopwords
  23. if hasattr(settings, 'OSCAR_SLUG_BLACKLIST'):
  24. for word in settings.OSCAR_SLUG_BLACKLIST:
  25. value = value.replace(word + '-', '')
  26. value = value.replace('-' + word, '')
  27. return value