Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from __future__ import absolute_import # for import below
  2. import logging
  3. from django.utils.timezone import get_current_timezone, is_naive, make_aware
  4. from unidecode import unidecode
  5. from django.conf import settings
  6. from django.template.defaultfilters import date as date_filter
  7. def slugify(value):
  8. """
  9. Slugify a string (even if it contains non-ASCII chars)
  10. """
  11. # Re-map some strings to avoid important characters being stripped. Eg
  12. # remap 'c++' to 'cpp' otherwise it will become 'c'.
  13. if hasattr(settings, 'OSCAR_SLUG_MAP'):
  14. for k, v in settings.OSCAR_SLUG_MAP.items():
  15. value = value.replace(k, v)
  16. # Allow an alternative slugify function to be specified
  17. if hasattr(settings, 'OSCAR_SLUG_FUNCTION'):
  18. slugifier = settings.OSCAR_SLUG_FUNCTION
  19. else:
  20. from django.template import defaultfilters
  21. slugifier = defaultfilters.slugify
  22. # Use unidecode to convert non-ASCII strings to ASCII equivalents where
  23. # possible.
  24. value = slugifier(
  25. unidecode(unicode(value)))
  26. # Remove stopwords
  27. if hasattr(settings, 'OSCAR_SLUG_BLACKLIST'):
  28. for word in settings.OSCAR_SLUG_BLACKLIST:
  29. value = value.replace(word + '-', '')
  30. value = value.replace('-' + word, '')
  31. return value
  32. def compose(*functions):
  33. """
  34. Compose functions
  35. This is useful for combining decorators.
  36. """
  37. def _composed(*args):
  38. for fn in functions:
  39. try:
  40. args = fn(*args)
  41. except TypeError:
  42. # args must be scalar so we don't try to expand it
  43. args = fn(args)
  44. return args
  45. return _composed
  46. def format_datetime(dt, format=None):
  47. """
  48. Takes an instance of datetime, converts it to the current timezone and
  49. formats it as a string. Use this instead of
  50. django.core.templatefilters.date, which expects localtime.
  51. :param format: Common will be settings.DATETIME_FORMAT or
  52. settings.DATE_FORMAT, or the resp. shorthands
  53. ('DATETIME_FORMAT', 'DATE_FORMAT')
  54. """
  55. if is_naive(dt):
  56. localtime = make_aware(dt, get_current_timezone())
  57. logging.warning(
  58. "oscar.core.utils.format_datetime received native datetime")
  59. else:
  60. localtime = dt.astimezone(get_current_timezone())
  61. return date_filter(localtime, format)