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.

43 lines
1.2 KiB

  1. from datetime import datetime
  2. from cronsim import CronSim
  3. from django.core.exceptions import ValidationError
  4. from urllib.parse import urlparse
  5. from pytz import all_timezones
  6. class WebhookValidator(object):
  7. message = "Enter a valid URL."
  8. def __call__(self, value):
  9. parsed = urlparse(value)
  10. if parsed.scheme not in ("http", "https"):
  11. raise ValidationError(message=self.message)
  12. if parsed.hostname in ("127.0.0.1", "localhost"):
  13. raise ValidationError(message=self.message)
  14. class CronExpressionValidator(object):
  15. message = "Not a valid cron expression."
  16. def __call__(self, value):
  17. # Expect 5 components-
  18. if len(value.split()) != 5:
  19. raise ValidationError(message=self.message)
  20. try:
  21. # Does cronsim accept the schedule?
  22. it = CronSim(value, datetime(2000, 1, 1))
  23. # Can it calculate the next datetime?
  24. next(it)
  25. except:
  26. raise ValidationError(message=self.message)
  27. class TimezoneValidator(object):
  28. message = "Not a valid time zone."
  29. def __call__(self, value):
  30. if value not in all_timezones:
  31. raise ValidationError(message=self.message)