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.

34 lines
944 B

  1. from croniter import croniter
  2. from django.core.exceptions import ValidationError
  3. from six.moves.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. try:
  17. croniter(value)
  18. except:
  19. raise ValidationError(message=self.message)
  20. class TimezoneValidator(object):
  21. message = "Not a valid time zone."
  22. def __call__(self, value):
  23. if value not in all_timezones:
  24. raise ValidationError(message=self.message)