/
forms.py
77 lines (59 loc) · 2.37 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""Notification-related forms."""
from django import forms
from django.core.validators import URLValidator
from django.forms.fields import CharField
from django.utils.translation import gettext_lazy as _
from reviewboard.admin.form_widgets import RelatedRepositoryWidget
from reviewboard.notifications.models import WebHookTarget
from reviewboard.scmtools.models import Repository
from reviewboard.site.mixins import LocalSiteAwareModelFormMixin
class WebHookTargetForm(LocalSiteAwareModelFormMixin, forms.ModelForm):
"""A form for creating and updating WebHookTargets."""
url = CharField(
label=_('URL'),
validators=[URLValidator()],
widget=forms.widgets.URLInput(attrs={'size': 100})
)
repositories = forms.ModelMultipleChoiceField(
label=_('Repositories'),
required=False,
queryset=Repository.objects.filter(visible=True).order_by('name'),
widget=RelatedRepositoryWidget())
def clean_extra_data(self):
"""Ensure that extra_data is a valid value.
Returns:
unicode:
Either a non-zero length string of JSON-encoded extra data or None.
"""
return self.cleaned_data['extra_data'] or None
def clean_events(self):
events = self.cleaned_data['events']
if '*' in events:
events = ['*']
return events
def clean(self):
"""Validate the state of the entire form.
Returns:
The cleaned form data.
"""
super(WebHookTargetForm, self).clean()
custom_content = self.cleaned_data.get('custom_content', '')
self.cleaned_data['use_custom_content'] = len(custom_content) > 0
apply_to = self.cleaned_data.get('apply_to')
if (apply_to != WebHookTarget.APPLY_TO_SELECTED_REPOS or
'repositories' not in self.cleaned_data):
self.cleaned_data['repositories'] = Repository.objects.none()
return self.cleaned_data
class Meta:
model = WebHookTarget
widgets = {
'apply_to': forms.widgets.RadioSelect(),
}
error_messages = {
'repositories': {
'invalid_choice': _('A repository with ID %(value)s was not '
'found.'),
'invalid_pk_value': _('"%(pk)s" is an invalid repository ID.'),
},
}
fields = '__all__'