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.

38 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. try:
  20. croniter(value)
  21. except:
  22. raise ValidationError(message=self.message)
  23. class TimezoneValidator(object):
  24. message = "Not a valid time zone."
  25. def __call__(self, value):
  26. if value not in all_timezones:
  27. raise ValidationError(message=self.message)