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.

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