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.

93 lines
2.8 KiB

10 years ago
10 years ago
10 years ago
  1. import json
  2. from datetime import timedelta as td
  3. from django import forms
  4. from django.core.validators import RegexValidator
  5. from hc.front.validators import (CronExpressionValidator, TimezoneValidator,
  6. WebhookValidator)
  7. class NameTagsForm(forms.Form):
  8. name = forms.CharField(max_length=100, required=False)
  9. tags = forms.CharField(max_length=500, required=False)
  10. def clean_tags(self):
  11. result = []
  12. for part in self.cleaned_data["tags"].split(" "):
  13. part = part.strip()
  14. if part != "":
  15. result.append(part)
  16. return " ".join(result)
  17. class TimeoutForm(forms.Form):
  18. timeout = forms.IntegerField(min_value=60, max_value=2592000)
  19. grace = forms.IntegerField(min_value=60, max_value=2592000)
  20. def clean_timeout(self):
  21. return td(seconds=self.cleaned_data["timeout"])
  22. def clean_grace(self):
  23. return td(seconds=self.cleaned_data["grace"])
  24. class CronForm(forms.Form):
  25. schedule = forms.CharField(max_length=100,
  26. validators=[CronExpressionValidator()])
  27. tz = forms.CharField(max_length=36, validators=[TimezoneValidator()])
  28. grace = forms.IntegerField(min_value=1, max_value=43200)
  29. class AddOpsGenieForm(forms.Form):
  30. error_css_class = "has-error"
  31. value = forms.CharField(max_length=40)
  32. class AddEmailForm(forms.Form):
  33. error_css_class = "has-error"
  34. value = forms.EmailField(max_length=100)
  35. class AddUrlForm(forms.Form):
  36. error_css_class = "has-error"
  37. value = forms.URLField(max_length=1000, validators=[WebhookValidator()])
  38. class AddWebhookForm(forms.Form):
  39. error_css_class = "has-error"
  40. url_down = forms.URLField(max_length=1000, required=False,
  41. validators=[WebhookValidator()])
  42. url_up = forms.URLField(max_length=1000, required=False,
  43. validators=[WebhookValidator()])
  44. post_data = forms.CharField(max_length=1000, required=False)
  45. def __init__(self, *args, **kwargs):
  46. super(AddWebhookForm, self).__init__(*args, **kwargs)
  47. self.headers = {}
  48. if "header_key[]" in self.data and "header_value[]" in self.data:
  49. keys = self.data.getlist("header_key[]")
  50. values = self.data.getlist("header_value[]")
  51. for key, value in zip(keys, values):
  52. if key:
  53. self.headers[key] = value
  54. def get_value(self):
  55. val = dict(self.cleaned_data)
  56. val["headers"] = self.headers
  57. return json.dumps(val, sort_keys=True)
  58. phone_validator = RegexValidator(regex='^\+\d{5,15}$',
  59. message="Invalid phone number format.")
  60. class AddSmsForm(forms.Form):
  61. error_css_class = "has-error"
  62. value = forms.CharField(max_length=16, validators=[phone_validator])