__init__.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """
  2. Smart slugify
  3. """
  4. import django.template.defaultfilters
  5. use_unidecode = True
  6. try:
  7. from unidecode import unidecode
  8. except ImportError:
  9. use_unidecode = False
  10. def slugify(string):
  11. if use_unidecode:
  12. string = unidecode(string)
  13. return django.template.defaultfilters.slugify(string)
  14. """
  15. Lazy translate that allows us to access original message
  16. """
  17. from django.utils import translation
  18. def ugettext_lazy(str):
  19. t = translation.ugettext_lazy(str)
  20. t.message = str
  21. return t
  22. def get_msgid(gettext):
  23. try:
  24. return gettext.message
  25. except AttributeError:
  26. return None
  27. """
  28. Random string
  29. """
  30. from django.utils import crypto
  31. def get_random_string(length):
  32. return crypto.get_random_string(length, "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM")
  33. """
  34. Date formats
  35. """
  36. from django.utils.formats import get_format
  37. formats = {
  38. 'DATE_FORMAT': '',
  39. 'DATETIME_FORMAT': '',
  40. 'TIME_FORMAT': '',
  41. 'YEAR_MONTH_FORMAT': '',
  42. 'MONTH_DAY_FORMAT': '',
  43. 'SHORT_DATE_FORMAT': '',
  44. 'SHORT_DATETIME_FORMAT': '',
  45. }
  46. for key in formats:
  47. formats[key] = get_format(key).replace('P', 'g:i a')
  48. """
  49. Build pagination list
  50. """
  51. import math
  52. def make_pagination(page, total, max):
  53. pagination = {'start': 0, 'stop': 0, 'prev': -1, 'next': -1}
  54. page = int(page)
  55. if page > 0:
  56. pagination['start'] = (page - 1) * max
  57. # Set page and total stat
  58. pagination['page'] = int(pagination['start'] / max) + 1
  59. pagination['total'] = int(math.ceil(total / float(max)))
  60. # Fix too large offset
  61. if pagination['start'] > total:
  62. pagination['start'] = 0
  63. # Allow prev/next?
  64. if total > max:
  65. if pagination['page'] > 1:
  66. pagination['prev'] = pagination['page'] - 1
  67. if pagination['page'] < pagination['total']:
  68. pagination['next'] = pagination['page'] + 1
  69. # Fix empty pagers
  70. if not pagination['total']:
  71. pagination['total'] = 1
  72. # Set stop offset
  73. pagination['stop'] = pagination['start'] + max
  74. return pagination