-
-
Notifications
You must be signed in to change notification settings - Fork 266
/
forms.py
480 lines (382 loc) · 20.2 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# -*- coding: utf8 -*-
# This file is part of PYBOSSA.
#
# Copyright (C) 2015 Scifabric LTD.
#
# PYBOSSA is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PYBOSSA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PYBOSSA. If not, see <http://www.gnu.org/licenses/>.
from flask import current_app
from flask_wtf import Form
from flask_wtf.file import FileField, FileRequired
from wtforms import IntegerField, DecimalField, TextField, BooleanField, \
SelectField, validators, TextAreaField, PasswordField, FieldList
from wtforms.fields.html5 import EmailField, URLField
from wtforms.widgets import HiddenInput
from flask.ext.babel import lazy_gettext, gettext
from pybossa.core import project_repo, user_repo
from pybossa.sched import sched_variants
import validator as pb_validator
from pybossa.core import enable_strong_password
from flask import request
from werkzeug.utils import secure_filename
from pybossa.core import uploader
from pybossa.uploader import local
from flask import safe_join
from flask.ext.login import current_user
import os
EMAIL_MAX_LENGTH = 254
USER_NAME_MAX_LENGTH = 35
USER_FULLNAME_MAX_LENGTH = 35
### Forms for projects view
class ProjectForm(Form):
name = TextField(lazy_gettext('Name'),
[validators.Required(),
pb_validator.Unique(project_repo.get_by, 'name',
message=lazy_gettext("Name is already taken."))])
short_name = TextField(lazy_gettext('Short Name'),
[validators.Required(),
pb_validator.NotAllowedChars(),
pb_validator.Unique(project_repo.get_by, 'short_name',
message=lazy_gettext(
"Short Name is already taken.")),
pb_validator.ReservedName('project', current_app)])
long_description = TextAreaField(lazy_gettext('Long Description'),
[validators.Required()])
description = TextAreaField(lazy_gettext('Description'),
[validators.Length(max=255)])
class ProjectUpdateForm(ProjectForm):
id = IntegerField(label=None, widget=HiddenInput())
description = TextAreaField(lazy_gettext('Description'),
[validators.Required(
message=lazy_gettext(
"You must provide a description.")),
validators.Length(max=255)])
long_description = TextAreaField(lazy_gettext('Long Description'))
allow_anonymous_contributors = BooleanField(lazy_gettext('Allow Anonymous Contributors'))
category_id = SelectField(lazy_gettext('Category'), coerce=int)
protect = BooleanField(lazy_gettext('Protect with a password?'))
password = TextField(lazy_gettext('Password'))
webhook = TextField(lazy_gettext('Webhook'),
[pb_validator.Webhook()])
class TaskPresenterForm(Form):
id = IntegerField(label=None, widget=HiddenInput())
editor = TextAreaField('')
class TaskRedundancyForm(Form):
n_answers = IntegerField(lazy_gettext('Redundancy'),
[validators.Required(),
validators.NumberRange(
min=1, max=1000,
message=lazy_gettext('Number of answers should be a \
value between 1 and 1,000'))])
class TaskPriorityForm(Form):
task_ids = TextField(lazy_gettext('Task IDs'),
[validators.Required(),
pb_validator.CommaSeparatedIntegers()])
priority_0 = DecimalField(lazy_gettext('Priority'),
[validators.NumberRange(
min=0, max=1,
message=lazy_gettext('Priority should be a \
value between 0.0 and 1.0'))])
class TaskSchedulerForm(Form):
_translate_names = lambda variant: (variant[0], lazy_gettext(variant[1]))
_choices = map(_translate_names, sched_variants())
sched = SelectField(lazy_gettext('Task Scheduler'), choices=_choices)
@classmethod
def update_sched_options(cls, new_options):
_translate_names = lambda variant: (variant[0], lazy_gettext(variant[1]))
_choices = map(_translate_names, new_options)
cls.sched.kwargs['choices'] = _choices
class AnnouncementForm(Form):
id = IntegerField(label=None, widget=HiddenInput())
title = TextField(lazy_gettext('Title'),
[validators.Required(message=lazy_gettext(
"You must enter a title for the post."))])
body = TextAreaField(lazy_gettext('Body'),
[validators.Required(message=lazy_gettext(
"You must enter some text for the post."))])
class BlogpostForm(Form):
id = IntegerField(label=None, widget=HiddenInput())
title = TextField(lazy_gettext('Title'),
[validators.Required(message=lazy_gettext(
"You must enter a title for the post."))])
body = TextAreaField(lazy_gettext('Body'),
[validators.Required(message=lazy_gettext(
"You must enter some text for the post."))])
published = BooleanField(lazy_gettext('Publish'))
class PasswordForm(Form):
password = PasswordField(lazy_gettext('Password'),
[validators.Required(message=lazy_gettext(
"You must enter a password"))])
class BulkTaskCSVImportForm(Form):
form_name = TextField(label=None, widget=HiddenInput(), default='csv')
msg_required = lazy_gettext("You must provide a URL")
msg_url = lazy_gettext("Oops! That's not a valid URL. "
"You must provide a valid URL")
csv_url = TextField(lazy_gettext('URL'),
[validators.Required(message=msg_required),
validators.URL(message=msg_url)])
def get_import_data(self):
return {'type': 'csv', 'csv_url': self.csv_url.data}
class BulkTaskGDImportForm(Form):
form_name = TextField(label=None, widget=HiddenInput(), default='gdocs')
msg_required = lazy_gettext("You must provide a URL")
msg_url = lazy_gettext("Oops! That's not a valid URL. "
"You must provide a valid URL")
googledocs_url = TextField(lazy_gettext('URL'),
[validators.Required(message=msg_required),
validators.URL(message=msg_url)])
def get_import_data(self):
return {'type': 'gdocs', 'googledocs_url': self.googledocs_url.data}
class BulkTaskEpiCollectPlusImportForm(Form):
form_name = TextField(label=None, widget=HiddenInput(), default='epicollect')
msg_required = lazy_gettext("You must provide an EpiCollect Plus "
"project name")
msg_form_required = lazy_gettext("You must provide a Form name "
"for the project")
epicollect_project = TextField(lazy_gettext('Project Name'),
[validators.Required(message=msg_required)])
epicollect_form = TextField(lazy_gettext('Form name'),
[validators.Required(message=msg_required)])
def get_import_data(self):
return {'type': 'epicollect',
'epicollect_project': self.epicollect_project.data,
'epicollect_form': self.epicollect_form.data}
class BulkTaskFlickrImportForm(Form):
form_name = TextField(label=None, widget=HiddenInput(), default='flickr')
msg_required = lazy_gettext("You must provide a valid Flickr album ID")
album_id = TextField(lazy_gettext('Album ID'),
[validators.Required(message=msg_required)])
def get_import_data(self):
return {'type': 'flickr', 'album_id': self.album_id.data}
class BulkTaskDropboxImportForm(Form):
form_name = TextField(label=None, widget=HiddenInput(), default='dropbox')
files = FieldList(TextField(label=None, widget=HiddenInput()))
def get_import_data(self):
return {'type': 'dropbox', 'files': self.files.data}
class BulkTaskTwitterImportForm(Form):
form_name = TextField(label=None, widget=HiddenInput(), default='twitter')
msg_required = lazy_gettext("You must provide some source for the tweets")
source = TextField(lazy_gettext('Source'),
[validators.Required(message=msg_required)])
max_tweets = IntegerField(lazy_gettext('Number of tweets'))
user_credentials = TextField(label=None)
def get_import_data(self):
return {
'type': 'twitter',
'source': self.source.data,
'max_tweets': self.max_tweets.data,
'user_credentials': self.user_credentials.data,
}
class BulkTaskYoutubeImportForm(Form):
form_name = TextField(label=None, widget=HiddenInput(), default='youtube')
msg_required = lazy_gettext("You must provide a valid playlist")
playlist_url = URLField(lazy_gettext('Playlist'),
[validators.Required(message=msg_required)])
def get_import_data(self):
return {
'type': 'youtube',
'playlist_url': self.playlist_url.data
}
class BulkTaskS3ImportForm(Form):
form_name = TextField(label=None, widget=HiddenInput(), default='s3')
files = FieldList(TextField(label=None, widget=HiddenInput()))
msg_required = lazy_gettext("You must provide a valid bucket")
bucket = TextField(lazy_gettext('Bucket'),
[validators.Required(message=msg_required)])
def get_import_data(self):
return {
'type': 's3',
'files': self.files.data,
'bucket': self.bucket.data
}
class BulkTaskLocalCSVImportForm(Form):
form_name = TextField(label=None, widget=HiddenInput(), default='localCSV')
_allowed_extensions = set(['csv'])
def _allowed_file(self, filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in self._allowed_extensions
def _container(self):
return "user_%d" % current_user.id
def _upload_path(self):
container = self._container()
filepath = None
if isinstance(uploader, local.LocalUploader):
filepath = safe_join(uploader.upload_folder, container)
if not os.path.isdir(filepath):
os.makedirs(filepath)
return filepath
current_app.logger.error('Failed to generate upload path {0}'.format(filepath))
raise IOError('Local Upload folder is missing: {0}'.format(filepath))
def get_import_data(self):
if request.method == 'POST':
if 'file' not in request.files:
return {'type': 'localCSV', 'csv_filename': None}
csv_file = request.files['file']
if csv_file.filename == '':
return {'type': 'localCSV', 'csv_filename': None}
if csv_file and self._allowed_file(csv_file.filename):
filename = secure_filename(csv_file.filename)
filepath = self._upload_path()
tmpfile = safe_join(filepath, filename)
csv_file.save(tmpfile)
return {'type': 'localCSV', 'csv_filename': tmpfile}
return {'type': 'localCSV', 'csv_filename': None}
class GenericBulkTaskImportForm(object):
"""Callable class that will return, when called, the appropriate form
instance"""
_forms = { 'csv': BulkTaskCSVImportForm,
'gdocs': BulkTaskGDImportForm,
'epicollect': BulkTaskEpiCollectPlusImportForm,
'flickr': BulkTaskFlickrImportForm,
'dropbox': BulkTaskDropboxImportForm,
'twitter': BulkTaskTwitterImportForm,
's3': BulkTaskS3ImportForm,
'youtube': BulkTaskYoutubeImportForm,
'localCSV': BulkTaskLocalCSVImportForm }
def __call__(self, form_name, *form_args, **form_kwargs):
if form_name is None:
return None
return self._forms[form_name](*form_args, **form_kwargs)
### Forms for account view
class LoginForm(Form):
"""Login Form class for signin into PYBOSSA."""
email = TextField(lazy_gettext('E-mail'),
[validators.Required(
message=lazy_gettext("The e-mail is required"))])
password = PasswordField(lazy_gettext('Password'),
[validators.Required(
message=lazy_gettext(
"You must provide a password"))])
class RegisterForm(Form):
"""Register Form Class for creating an account in PYBOSSA."""
err_msg = lazy_gettext("Full name must be between 3 and %(fullname)s "
"characters long", fullname=USER_FULLNAME_MAX_LENGTH)
fullname = TextField(lazy_gettext('Full name'),
[validators.Length(min=3, max=USER_FULLNAME_MAX_LENGTH, message=err_msg)])
err_msg = lazy_gettext("User name must be between 3 and %(username_length)s "
"characters long", username_length=USER_NAME_MAX_LENGTH)
err_msg_2 = lazy_gettext("The user name is already taken")
name = TextField(lazy_gettext('User name'),
[validators.Length(min=3, max=USER_NAME_MAX_LENGTH, message=err_msg),
pb_validator.NotAllowedChars(),
pb_validator.Unique(user_repo.get_by, 'name', err_msg_2),
pb_validator.ReservedName('account', current_app)])
err_msg = lazy_gettext("Email must be between 3 and %(email_length)s "
"characters long", email_length=EMAIL_MAX_LENGTH)
err_msg_2 = lazy_gettext("Email is already taken")
email_addr = EmailField(lazy_gettext('Email Address'),
[validators.Length(min=3,
max=EMAIL_MAX_LENGTH,
message=err_msg),
validators.Email(),
pb_validator.Unique(user_repo.get_by, 'email_addr', err_msg_2)])
err_msg = lazy_gettext("Password cannot be empty")
err_msg_2 = lazy_gettext("Passwords must match")
if enable_strong_password:
password = PasswordField(
lazy_gettext('New Password'),
[validators.Required(err_msg),
validators.EqualTo('confirm', err_msg_2),
pb_validator.CheckPasswordStrength()])
else:
password = PasswordField(
lazy_gettext('New Password'),
[validators.Required(err_msg),
validators.EqualTo('confirm', err_msg_2)])
confirm = PasswordField(lazy_gettext('Repeat Password'))
consent = BooleanField(false_values=("False", "false", '', '0', 0))
class UpdateProfileForm(Form):
"""Form Class for updating PYBOSSA's user Profile."""
id = IntegerField(label=None, widget=HiddenInput())
err_msg = lazy_gettext("Full name must be between 3 and %(fullname)s "
"characters long" , fullname=USER_FULLNAME_MAX_LENGTH)
fullname = TextField(lazy_gettext('Full name'),
[validators.Length(min=3, max=USER_FULLNAME_MAX_LENGTH, message=err_msg)])
err_msg = lazy_gettext("User name must be between 3 and %(username_length)s "
"characters long", username_length=USER_NAME_MAX_LENGTH)
err_msg_2 = lazy_gettext("The user name is already taken")
name = TextField(lazy_gettext('Username'),
[validators.Length(min=3, max=USER_NAME_MAX_LENGTH, message=err_msg),
pb_validator.NotAllowedChars(),
pb_validator.Unique(user_repo.get_by, 'name', err_msg_2),
pb_validator.ReservedName('account', current_app)])
err_msg = lazy_gettext("Email must be between 3 and %(email_length)s "
"characters long", email_length=EMAIL_MAX_LENGTH)
err_msg_2 = lazy_gettext("Email is already taken")
email_addr = EmailField(lazy_gettext('Email Address'),
[validators.Length(min=3,
max=EMAIL_MAX_LENGTH,
message=err_msg),
validators.Email(),
pb_validator.Unique(user_repo.get_by, 'email_addr', err_msg_2)])
subscribed = BooleanField(lazy_gettext('Get email notifications'))
locale = SelectField(lazy_gettext('Language'))
ckan_api = TextField(lazy_gettext('CKAN API Key'))
privacy_mode = BooleanField(lazy_gettext('Privacy Mode'))
def set_locales(self, locales):
"""Fill the locale.choices."""
choices = []
for locale in locales:
choices.append(locale)
self.locale.choices = choices
class ChangePasswordForm(Form):
"""Form for changing user's password."""
current_password = PasswordField(lazy_gettext('Current password'))
err_msg = lazy_gettext("Password cannot be empty")
err_msg_2 = lazy_gettext("Passwords must match")
new_password = PasswordField(lazy_gettext('New password'),
[validators.Required(err_msg),
validators.EqualTo('confirm', err_msg_2)])
confirm = PasswordField(lazy_gettext('Repeat password'))
class ResetPasswordForm(Form):
"""Class for resetting user's password."""
err_msg = lazy_gettext("Password cannot be empty")
err_msg_2 = lazy_gettext("Passwords must match")
new_password = PasswordField(lazy_gettext('New Password'),
[validators.Required(err_msg),
validators.EqualTo('confirm', err_msg_2)])
confirm = PasswordField(lazy_gettext('Repeat Password'))
class ForgotPasswordForm(Form):
"""Form Class for forgotten password."""
err_msg = lazy_gettext("Email must be between 3 and %(email_length)s "
"characters long", email_length=EMAIL_MAX_LENGTH)
email_addr = EmailField(lazy_gettext('Email Address'),
[validators.Length(min=3,
max=EMAIL_MAX_LENGTH,
message=err_msg),
validators.Email()])
class OTPForm(Form):
otp = TextField(lazy_gettext('One Time Password'),
[validators.Required(message=lazy_gettext(
'You must provide a valid OTP code'))])
### Forms for admin view
class SearchForm(Form):
user = TextField(lazy_gettext('User'))
class CategoryForm(Form):
id = IntegerField(label=None, widget=HiddenInput())
name = TextField(lazy_gettext('Name'),
[validators.Required(),
pb_validator.Unique(project_repo.get_category_by, 'name',
message="Name is already taken.")])
description = TextField(lazy_gettext('Description'),
[validators.Required()])
### Common forms
class AvatarUploadForm(Form):
id = IntegerField(label=None, widget=HiddenInput())
avatar = FileField(lazy_gettext('Avatar'), validators=[FileRequired()])
x1 = IntegerField(label=None, widget=HiddenInput(), default=0)
y1 = IntegerField(label=None, widget=HiddenInput(), default=0)
x2 = IntegerField(label=None, widget=HiddenInput(), default=0)
y2 = IntegerField(label=None, widget=HiddenInput(), default=0)
class TransferOwnershipForm(Form):
email_addr = EmailField(lazy_gettext('Email of the new owner'))