You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.0 KiB

  1. from croniter import croniter
  2. from django.core.exceptions import ValidationError
  3. from urllib.parse import urlparse
  4. from pytz import all_timezones
  5. class WebhookValidator(object):
  6. message = "Enter a valid URL."
  7. def __call__(self, value):
  8. parsed = urlparse(value)
  9. if parsed.scheme not in ("http", "https"):
  10. raise ValidationError(message=self.message)
  11. if parsed.hostname in ("127.0.0.1", "localhost"):
  12. raise ValidationError(message=self.message)
  13. class CronExpressionValidator(object):
  14. message = "Not a valid cron expression."
  15. def __call__(self, value):
  16. # Expect 5 components-
  17. if len(value.split()) != 5:
  18. raise ValidationError(message=self.message)
  19. if not croniter.is_valid(value):
  20. raise ValidationError(message=self.message)
  21. class TimezoneValidator(object):
  22. message = "Not a valid time zone."
  23. def __call__(self, value):
  24. if value not in all_timezones:
  25. raise ValidationError(message=self.message)