strings.py 823 B

1234567891011121314151617181920212223242526272829
  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. string = unicode(string)
  10. if use_unidecode:
  11. string = unidecode(string)
  12. return django_slugify(string)
  13. def random_string(length):
  14. return crypto.get_random_string(length, "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM")
  15. def short_string(string, length=16):
  16. if len(string) <= length:
  17. return string;
  18. string = string[0:length]
  19. bits = string.split()
  20. if len(bits[-1]) > length:
  21. bits[-1] = bits[-1][0:length]
  22. if len(bits[-1]) < 3:
  23. bits.pop()
  24. return '%s...' % (' '.join(bits))