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.

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