strings.py 903 B

123456789101112131415161718192021222324252627282930313233
  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 - 3]
  19. bits = string.split()
  20. if len(bits[-1]) < 3:
  21. bits.pop()
  22. return '%s...' % (' '.join(bits))
  23. def html_escape(html):
  24. html = html.replace('&', '&amp;')
  25. html = html.replace('<', '&lt;')
  26. html = html.replace('>', '&gt;')
  27. return html.replace('"', '&quot;')