forked from ELDAELRA/ELRI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforms.py
386 lines (320 loc) · 15.7 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
from django import forms
from django.conf import settings
from django.contrib.admin import widgets
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
from django.core.validators import RegexValidator
from metashare.accounts.django_password_validators import validate_password
from metashare.accounts.models import UserProfile, EditorGroupApplication, \
OrganizationApplication, Organization, OrganizationManagers, EditorGroup, \
EditorGroupManagers, AccessPointEdeliveryApplication
from metashare.accounts.validators import validate_wsdl_url
from metashare.settings import LOG_HANDLER
import logging
## Setup logging support.
LOGGER = logging.getLogger(__name__)
LOGGER.addHandler(LOG_HANDLER)
class ModelForm(forms.ModelForm):
"""
Base form for META-SHARE model forms -- disables the colon after a label,
and formats error messages as expected by the templates.
"""
def __init__(self, *args, **kwargs):
super(ModelForm, self).__init__(*args, **kwargs)
# Avoid the default ':' suffix
self.label_suffix = ''
required_css_class = 'required'
error_css_class = 'error'
def as_table(self):
"Returns this form rendered as HTML <tr>s -- excluding the <table></table>."
return self._html_output(
normal_row=u'<tr%(html_class_attr)s><th>%(label)s%(errors)s</th><td>%(field)s%(help_text)s</td></tr>',
error_row=u'<tr><td colspan="2">%s</td></tr>',
row_ender=u'</td></tr>',
help_text_html=u'<br /><span class="helptext">%s</span>',
errors_on_separate_row=False)
class Form(forms.Form):
"""
Base form for META-SHARE forms -- disables the colon after a label,
and formats error messages as expected by the templates.
"""
def __init__(self, *args, **kwargs):
super(Form, self).__init__(*args, **kwargs)
# Avoid the default ':' suffix
self.label_suffix = ''
required_css_class = 'required'
error_css_class = 'error'
def as_table(self):
"Returns this form rendered as HTML <tr>s -- excluding the <table></table>."
return self._html_output(
normal_row=u'<tr%(html_class_attr)s><th>%(label)s%(errors)s</th><td>%(field)s%(help_text)s</td></tr>',
error_row=u'<tr><td colspan="2">%s</td></tr>',
row_ender=u'</td></tr>',
help_text_html=u'<br /><span class="helptext">%s</span>',
errors_on_separate_row=False)
class RegistrationRequestForm(Form):
"""
Form used to create user account requests from new users.
"""
alphanumeric = RegexValidator(r'^[0-9a-zA-Z@.+\-_]*$', _(u'This value may contain only letters, numbers and @/./+/-/_ characters.'))
shortname = forms.CharField(max_length=User._meta.get_field('username').max_length,validators=[alphanumeric],
label=mark_safe(u"%s<span style='color:red' aria-label='%s'>*</span>" % ( _("Desired account name"), _("Required Field"))))
first_name = forms.CharField(User._meta.get_field('first_name').max_length,
label=mark_safe(u"%s<span style='color:red' aria-label='%s'>*</span>" % (_("First name"), _("Required Field"))))
last_name = forms.CharField(User._meta.get_field('last_name').max_length,
label=mark_safe(u"%s<span style='color:red' aria-label='%s'>*</span>" % (_("Last name"), _("Required Field"))))
email = forms.EmailField(label=mark_safe(u"%s<span style='color:red' aria-label='%s'>*</span>" % (_("E-mail"), _("Required Field"))))
# For National Relay Stations, the country is limited to the Member State in which the NRS is deployed
#country = forms.ChoiceField(UserProfile._meta.get_field('country').choices,
#UserProfile._meta.get_field('country').max_length,
#label=mark_safe(u"%s<span style='color:red'>*</span>" % _("Country")))
organization = forms.CharField(UserProfile._meta.get_field('affiliation').max_length,
label=mark_safe(u"%s<span style='color:red' aria-label='%s'>*</span>" % (_("Organization name"), _("Required Field"))))
organization_address = forms.CharField(
UserProfile._meta.get_field('affiliation_address').max_length,
label=mark_safe(_("Organization address")), required=False)
organization_phone_number = forms.CharField(
UserProfile._meta.get_field('affiliation_phone_number').max_length,
label=mark_safe( _("Organization phone number")),required=False)
position = forms.CharField(UserProfile._meta.get_field('position').max_length,
label=mark_safe(_("Position in the organization")),required=False)
#Removing user phone number for now: user email and organisation phone number should be sufficient
#phone_number = forms.CharField(UserProfile._meta.get_field('phone_number').max_length,
#label=mark_safe(u"%s<span style='color:grey'>*</span>" % _("Phone number")))
password = forms.CharField(User._meta.get_field('password').max_length,
label=mark_safe(u"%s<span style='color:red' aria-label='%s'>*</span>" % (_("Password"), _("Required Field"))),
widget=forms.PasswordInput())
confirm_password = forms.CharField(
User._meta.get_field('password').max_length,
label=mark_safe(u"%s<span style='color:red' aria-label='%s'>*</span>" % (_("Password confirmation"), _("Required Field"))), widget=forms.PasswordInput())
#Commenting from now, as it might be more functional to handle group assignment for logged in users
## In ELRI we need users to be able to join more than one group
#contributor_group = forms.MultipleChoiceField(
#label=mark_safe(u"%s<span style='color:red'>*</span>" % _("Contributor group"))
#)
accepted_tos = forms.BooleanField()
def __init__(self, *args, **kwargs):
group_choices = kwargs.pop("group_choices")
super(RegistrationRequestForm, self).__init__(*args, **kwargs)
#self.fields["contributor_group"].choices = group_choices
def clean_shortname(self):
"""
Make sure that the user name is still available.
"""
_user_name = self.cleaned_data['shortname']
try:
User.objects.get(username=_user_name)
except:
pass
else:
raise ValidationError(_('User account name already exists, ' \
'please choose another one.'))
return _user_name
def clean_email(self):
"""
Make sure that there is no account yet registered with this email.
"""
_email = self.cleaned_data['email']
try:
User.objects.get(email=_email)
except User.DoesNotExist:
pass
except User.MultipleObjectsReturned:
raise ValidationError(_('There is already an account registered ' \
'with this e-mail address.'))
else:
raise ValidationError(_('There is already an account registered ' \
'with this e-mail address.'))
return _email
def clean_confirm_password(self):
"""
Make sure that the password confirmation is the same as password.
"""
pswrd = self.cleaned_data.get('password', None)
pswrd_conf = self.cleaned_data.get('confirm_password',None)
if pswrd != pswrd_conf:
raise ValidationError(_('The two password fields did not match.'))
#if 'shortname' in self.cleaned_data.keys() and 'email' in self.cleaned_data.keys() and 'password' in : #check password iif there is a valid username, to avoid
validate_password(pswrd, user=User(
# this in-memory object is just for password validation
username=self.cleaned_data.get('shortname',None),
email=self.cleaned_data.get('email',None),
password=self.cleaned_data.get('password',None),
first_name=self.cleaned_data.get('first_name',None),
last_name=self.cleaned_data.get('last_name',None),
))
validate_password(pswrd, user=UserProfile(
# this in-memory object is just for password validation
user_id=1, # dummy foreign key
affiliation=self.cleaned_data.get('organization',None),
affiliation_address=self.cleaned_data.get('organization_address',None),
affiliation_phone_number=self.cleaned_data.get('organization_phone_number',None),
))
return pswrd
# cfedermann: possible extensions for future improvements.
# - add validation for shortname for forbidden characters
class EdeliveryApplicationForm(Form):
# class Meta:
# """
# Meta class connecting to UserProfile object model.
# """
# model = AccessPointEdeliveryApplication
# exclude = ('user', 'status', 'created', 'rejection_reason')
endpoint = forms.URLField(validators=[validate_wsdl_url], required=True)
gateway_party_name = forms.CharField(max_length=100, required=True)
gateway_party_id = forms.CharField(max_length=100, required=True)
public_key = forms.FileField(required=True)
class ContactForm(Form):
"""
Form used to contact the superusers of the META-SHARE node.
"""
subject = forms.CharField(min_length=6, max_length=80,label=_(u'Subject'),
error_messages={'min_length': _('Please provide a meaningful and '
'sufficiently indicative subject.')})
message = forms.CharField(min_length=30, max_length=2500,label=_(u'Message'),
widget=forms.Textarea, error_messages={'min_length': _('Your message '
'appears to be rather short. Please make sure to phrase your '
'request as precise as possible. This will help us to process it '
'as quick as possible.')})
class ResetRequestForm(Form):
"""
Form used to reset an existing user account.
"""
username = forms.CharField(max_length=30, label=_(u'Username'))
email = forms.EmailField(label=_(u'Email'))
def clean(self):
cleaned_data = self.cleaned_data
username = cleaned_data.get("username")
email = cleaned_data.get("email")
if username and email:
# Only do something if both fields are valid so far.
user = User.objects.filter(username=username, email=email)
if not user:
raise forms.ValidationError(_('Not a valid username-email combination.'))
return cleaned_data
class UserProfileForm(ModelForm):
"""
Form used to update the user account profile information.
"""
class Meta:
"""
Meta class connecting to UserProfile object model.
"""
model = UserProfile
exclude = ('user', 'modified', 'uuid', 'default_editor_groups', 'country')
class EditorGroupApplicationForm(ModelForm):
"""
Form used to apply to new editor groups membership.
"""
class Meta:
"""
Meta class connecting to EditorGroupApplication object model.
"""
model = EditorGroupApplication
exclude = ('user', 'created')
def __init__(self, editor_group_qs, *args, **kwargs):
"""
Initializes the `EditorGroupApplicationForm` with the editor groups
of the given query set.
"""
super(EditorGroupApplicationForm, self).__init__(*args, **kwargs)
# If there is a list of editor groups, then modify the ModelChoiceField
self.fields['editor_group'].queryset = editor_group_qs
class UpdateDefaultEditorGroupForm(ModelForm):
"""
Form used to update default editor groups.
"""
default_editor_groups = forms.ModelMultipleChoiceField([],
widget=widgets.FilteredSelectMultiple(
_("default editor groups"),
is_stacked=False),
required=False)
class Media:
css = {
# required by the FilteredSelectMultiple widget
'all': ['{}admin/css/widgets.css'.format(settings.STATIC_URL)],
}
# required by the FilteredSelectMultiple widget
js = ['/{}admin/jsi18n/'.format(settings.DJANGO_BASE)]
class Meta:
"""
Meta class connecting to UserProfile object model.
"""
model = UserProfile
exclude = ('user', 'modified', 'uuid', 'birthdate', 'affiliation', \
'position', 'homepage')
def __init__(self, available_editor_group, chosen_editor_group, *args, **kwargs):
"""
Initializes the `UpdateDefaultEditorGroupForm` with the editor groups
of the given query set.
"""
super(UpdateDefaultEditorGroupForm, self).__init__(*args, **kwargs)
# If there is a list of editor groups, then modify the ModelChoiceField
self.fields['default_editor_groups'].queryset = available_editor_group
self.fields['default_editor_groups'].initial = chosen_editor_group
class OrganizationApplicationForm(ModelForm):
"""
Form used to apply to new organizations membership.
"""
class Meta:
"""
Meta class connecting to OrganizationApplication object model.
"""
model = OrganizationApplication
exclude = ('user', 'created')
def __init__(self, organization_qs, *args, **kwargs):
"""
Initializes the `OrganizationApplicationForm` with the organizations
of the given query set.
"""
super(OrganizationApplicationForm, self).__init__(*args, **kwargs)
# If there is a list of organizations, then modify the ModelChoiceField
self.fields['organization'].queryset = organization_qs
class EditorGroupForm(ModelForm):
"""
Form used to render the add/change admin views for `EditorGroup` model
instances.
"""
class Meta:
model = EditorGroup
exclude = ()
widgets = {
'permissions': forms.widgets.MultipleHiddenInput
}
class EditorGroupManagersForm(ModelForm):
"""
Form used to render the add/change admin views for `EditorGroupManagers`
model instances.
"""
class Meta:
model = EditorGroupManagers
exclude = ()
widgets = {
'permissions': forms.widgets.MultipleHiddenInput
}
class OrganizationForm(ModelForm):
"""
Form used to render the add/change admin views for `Organization` model
instances.
"""
class Meta:
model = Organization
exclude = ()
widgets = {
'permissions': widgets.FilteredSelectMultiple(
Organization._meta.get_field('permissions').verbose_name, False)
}
class OrganizationManagersForm(ModelForm):
"""
Form used to render the add/change admin views for `OrganizationManagers`
model instances.
"""
class Meta:
model = OrganizationManagers
exclude = ()
widgets = {
'permissions': widgets.FilteredSelectMultiple(OrganizationManagers \
._meta.get_field('permissions').verbose_name, False)
}