This repository was archived by the owner on Dec 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforms.py
More file actions
116 lines (96 loc) · 4.6 KB
/
Copy pathforms.py
File metadata and controls
116 lines (96 loc) · 4.6 KB
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
"""
Define the forms used in the views.
Forms are defined using FlaskForm class of Flask-WTF module.
They have several fields defined, and a CSRF token hidden field that is created
automatically.
"""
from flask_wtf import FlaskForm
from wtforms.fields import TextField, BooleanField, PasswordField, RadioField, SubmitField, TextField, IntegerField, TextAreaField
from wtforms.validators import Optional, Required, Email, EqualTo, Length
from wtforms.widgets.html5 import NumberInput
from flask_uploads import UploadSet, IMAGES
from flask_wtf.file import FileField, FileAllowed, FileRequired
import app
class RequiredIf(Optional, Required):
""" A validator which makes a field required if
another field is set and has a truthy value
Adapted from : http://stackoverflow.com/a/8464478
"""
def __init__(self, other_field_name, *args, **kwargs):
self.other_field_name = other_field_name
super().__init__(*args, **kwargs)
def __call__(self, form, field):
# Why use the __call__ special method : http://stackoverflow.com/a/5826283
other_field = form._fields.get(self.other_field_name)
if other_field is None:
raise Exception('no field named "%s" in form' % self.other_field_name)
if len(other_field.data) != 0:
Required()(form, field)
else :
Optional()(form, field)
class UploadForm(FlaskForm):
"""
Form for image upload.
"""
sample = FileField('Sample image file', validators=[
FileRequired(),
FileAllowed( app.samples_set, 'Not supported type : jpeg only.')
])
smear_type = RadioField('Blood smear type', choices=[
('thick', 'Thick'),
('thin', 'Thin')
], validators=[Required()] )
comment = TextAreaField('Comment', render_kw={"placeholder": "URL, legend, etc...", "rows": 3, "cols": 70})
license = RadioField('License', choices=[
('CC0', 'CC0 / Public Domain: Freeing content globally without restrictions'),
('BY', 'CC-BY: Attribution')
], validators=[Required()], default='BY' )
provider = TextAreaField('Provider', render_kw={"rows": 3, "cols": 70}, validators=[Required()])
magnification =IntegerField ('Microscope magnification factor', widget=NumberInput(), render_kw={"placeholder": "e.g. 100"}, validators=[Optional()], default=None )
patient_ref = TextField('Patient reference', validators=[Length(max=50)])
patient_year_of_birth = IntegerField ('Patient year of birth', widget=NumberInput(), validators=[RequiredIf('patient_ref')], default=None )
patient_gender =RadioField('Patient gender', validators=[RequiredIf('patient_ref')], default=None, choices=[
('M', 'Male'),
('F', 'Female')
])
patient_city = TextField('Patient city', validators=[Length(max=50), RequiredIf('patient_ref')], default=None)
patient_country = TextField('Patient country', validators=[Length(max=50), RequiredIf('patient_ref')], default=None)
submit = SubmitField('Upload new blood smear', render_kw={"class": "btn btn-info btn-lg", "id": "submit-button"})
class LoginForm(FlaskForm):
"""
Form for user login.
"""
username = TextField('Username', validators=[Required(), Length(min=4, max=80)])
password = PasswordField('Password', validators=[Required()])
idle_ttl = RadioField('Idle Session Timeout', default='tmp', choices=[
('tmp', '20 minutes'),
('day', '8 hours (a normal business day)'),
('week', '8 days (Monday to Monday)'),
])
submit = SubmitField('Login')
class ProfileForm(FlaskForm):
"""
For for the user to change infos of his own account.
"""
email = TextField('Email Adress', validators=[Optional(), Email() ])
password = PasswordField('New Password', validators=[
Optional(),
Length(min=8, max=80),
EqualTo('confirm', message='Passwords must match')
])
confirm = PasswordField('Repeat Password')
submit = SubmitField('Update Profile')
class RegisterForm(FlaskForm):
"""
For for new user registration.
"""
username = TextField('Username', validators=[Required(), Length(min=8, max=80)])
email = TextField('Email Address', validators = [Required(), Email()])
password = PasswordField('New Password', validators=[
Required(),
Length(min=8, max=80),
EqualTo('confirm', message='Passwords must match')
])
confirm = PasswordField('Repeat Password')
accept_tos = BooleanField('I accept the TOS', validators = [Required()])
submit = SubmitField('Register')