utils.py 1.4 KB

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