utils.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from django.core.validators import URLValidator
  2. from django.core.exceptions import ValidationError
  3. from django.utils.html import mark_safe
  4. from wagtailcrx.settings import crx_settings
  5. def get_protected_media_link(request, path, render_link=False):
  6. if render_link:
  7. return mark_safe(
  8. "<a href='{0}{1}'>{0}{1}</a>".format(
  9. request.build_absolute_uri('/')[:-1],
  10. path
  11. )
  12. )
  13. return "{0}{1}".format(request.build_absolute_uri('/')[:-1], path)
  14. def uri_validator(possible_uri):
  15. validate = URLValidator()
  16. try:
  17. validate(possible_uri)
  18. return True
  19. except ValidationError:
  20. return False
  21. def attempt_protected_media_value_conversion(request, value):
  22. try:
  23. if value.startswith(crx_settings.CRX_PROTECTED_MEDIA_URL):
  24. new_value = get_protected_media_link(request, value)
  25. return new_value
  26. except AttributeError:
  27. pass
  28. return value
  29. def fix_ical_datetime_format(dt_str):
  30. """
  31. ICAL generation gives timezones in the format of 2018-06-30T14:00:00-04:00.
  32. The Timezone offset -04:00 has a character not recognized by the timezone offset
  33. code (%z). The being the colon in -04:00. We need it to instead be -0400
  34. """
  35. if dt_str and ":" == dt_str[-3:-2]:
  36. dt_str = dt_str[:-3] + dt_str[-2:]
  37. return dt_str
  38. return dt_str