strings.py 793 B

12345678910111213141516171819202122232425262728
  1. from django.template.defaultfilters import slugify as django_slugify
  2. from django.utils import crypto
  3. try:
  4. from unidecode import unidecode
  5. use_unidecode = True
  6. except ImportError:
  7. use_unidecode = False
  8. def slugify(string):
  9. if use_unidecode:
  10. string = unidecode(string)
  11. return django_slugify(string)
  12. def random_string(length):
  13. return crypto.get_random_string(length, "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM")
  14. def short_string(string, length=16):
  15. if len(string) <= length:
  16. return string;
  17. string = string[0:length]
  18. bits = string.split()
  19. if len(bits[-1]) > length:
  20. bits[-1] = bits[-1][0:length]
  21. if len(bits[-1]) < 3:
  22. bits.pop()
  23. return '%s...' % (' '.join(bits))