strings.py 817 B

1234567891011121314151617181920212223242526272829
  1. from unidecode import unidecode
  2. from django.template.defaultfilters import slugify as django_slugify
  3. from django.utils import crypto
  4. def slugify(string):
  5. string = unicode(string)
  6. string = unidecode(string)
  7. return django_slugify(string.replace('_', ' '))
  8. def random_string(length):
  9. return crypto.get_random_string(length, "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM")
  10. def short_string(string, length=16):
  11. if len(string) <= length:
  12. return string;
  13. string = string[0:length - 3]
  14. bits = string.split()
  15. if len(bits[-1]) < 3:
  16. bits.pop()
  17. return '%s...' % (' '.join(bits))
  18. def html_escape(html):
  19. html = html.replace('&', '&amp;')
  20. html = html.replace('<', '&lt;')
  21. html = html.replace('>', '&gt;')
  22. return html.replace('"', '&quot;')