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.

109 lines
3.3 KiB

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