strings.py 836 B

12345678910111213141516171819202122232425262728293031
  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. short = []
  18. length = length - 3
  19. string = string[0:length]
  20. bits = string.split()
  21. if len(bits[-1]) > length:
  22. bits[-1] = bits[-1][0:length]
  23. if len(bits[-1]) < 3:
  24. bits.pop()
  25. return '%s...' % (' '.join(bits))