1
|
|
|
# coding: utf-8 |
2
|
|
|
from django import forms |
3
|
|
|
from django.forms import TextInput, PasswordInput |
4
|
|
|
from django.utils.translation import ugettext_lazy as _ |
5
|
|
|
|
6
|
|
|
# trigger happy |
7
|
|
|
from django_th.models import User, UserService, ServicesActivated, TriggerService |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class TriggerServiceForm(forms.ModelForm): |
11
|
|
|
|
12
|
|
|
""" |
13
|
|
|
Form to edit the description |
14
|
|
|
""" |
15
|
|
|
class Meta: |
16
|
|
|
|
17
|
|
|
""" |
18
|
|
|
meta to add/override anything we need |
19
|
|
|
""" |
20
|
|
|
model = TriggerService |
21
|
|
|
exclude = ['provider', 'consumer', 'user', 'date_triggered', |
22
|
|
|
'date_created', 'status', 'result', 'date_result', |
23
|
|
|
'consumer_failed', 'provider_failed', 'counter_ok', |
24
|
|
|
'counter_ko'] |
25
|
|
|
widgets = { |
26
|
|
|
'description': TextInput(attrs={'class': 'form-control'}), |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
class UserServiceForm(forms.ModelForm): |
31
|
|
|
|
32
|
|
|
""" |
33
|
|
|
Form to deal with my own activated service |
34
|
|
|
""" |
35
|
|
|
def save(self, user=None, service_name=''): |
36
|
|
|
""" |
37
|
|
|
|
38
|
|
|
:param user: |
39
|
|
|
:param service_name: |
40
|
|
|
:return: |
41
|
|
|
""" |
42
|
|
|
self.myobject = super(UserServiceForm, self).save(commit=False) |
43
|
|
|
self.myobject.user = user |
44
|
|
|
self.myobject.name = ServicesActivated.objects.get(name=self.initial['name']) |
45
|
|
|
self.myobject.save() |
46
|
|
|
|
47
|
|
|
def clean(self): |
48
|
|
|
""" |
49
|
|
|
check the content of each field |
50
|
|
|
:return: |
51
|
|
|
""" |
52
|
|
|
cleaned_data = super(UserServiceForm, self).clean() |
53
|
|
|
sa = ServicesActivated.objects.get(name=self.initial['name']) |
54
|
|
|
# set the name of the service, related to ServicesActivated model |
55
|
|
|
cleaned_data['name'] = sa |
56
|
|
|
if sa.auth_required and sa.self_hosted: |
57
|
|
|
if cleaned_data.get('host') == '' or \ |
58
|
|
|
cleaned_data.get('username') == '' or \ |
59
|
|
|
cleaned_data.get('password') == '' or \ |
60
|
|
|
cleaned_data.get('client_id') == '' or \ |
61
|
|
|
cleaned_data.get('client_secret') == '': |
62
|
|
|
self.add_error('username', 'All the five fields are altogether mandatory') |
63
|
|
|
elif cleaned_data.get('host') is None: |
64
|
|
|
self.add_error('host', 'Check its protocol and its name') |
65
|
|
|
elif cleaned_data.get('host').endswith('/'): |
66
|
|
|
cleaned_data['host'] = cleaned_data['host'][:-1] |
67
|
|
|
|
68
|
|
|
class Meta: |
69
|
|
|
|
70
|
|
|
""" |
71
|
|
|
meta to add/override anything we need |
72
|
|
|
""" |
73
|
|
|
model = UserService |
74
|
|
|
exclude = ('name', 'user', 'counter_ok', 'counter_ko') |
75
|
|
|
widgets = { |
76
|
|
|
'host': TextInput(attrs={'class': 'form-control'}), |
77
|
|
|
'username': TextInput(attrs={'class': 'form-control'}), |
78
|
|
|
'password': PasswordInput(attrs={'class': 'form-control'}), |
79
|
|
|
'client_id': TextInput(attrs={'class': 'form-control'}), |
80
|
|
|
'client_secret': TextInput(attrs={'class': 'form-control'}), |
81
|
|
|
'token': TextInput(attrs={'class': 'form-control'}), |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
class LoginForm(forms.ModelForm): |
86
|
|
|
|
87
|
|
|
""" |
88
|
|
|
Form to manage the login page |
89
|
|
|
""" |
90
|
|
|
class Meta: |
91
|
|
|
model = User |
92
|
|
|
widgets = { |
93
|
|
|
'username': TextInput(attrs={'placeholder': _('Username')}), |
94
|
|
|
'password': PasswordInput(attrs={'placeholder': _('Password')}), |
95
|
|
|
} |
96
|
|
|
exclude = () |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
class MeForm(forms.ModelForm): |
100
|
|
|
|
101
|
|
|
""" |
102
|
|
|
form to edit its profile |
103
|
|
|
""" |
104
|
|
|
|
105
|
|
|
class Meta: |
106
|
|
|
|
107
|
|
|
model = User |
108
|
|
|
fields = ['email', 'first_name', 'last_name'] |
109
|
|
|
|