-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathforms.py
More file actions
191 lines (153 loc) · 8.19 KB
/
forms.py
File metadata and controls
191 lines (153 loc) · 8.19 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
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
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SelectField, SubmitField, BooleanField, IntegerField
from wtforms.validators import DataRequired, Email, Length, EqualTo, ValidationError, NumberRange, Optional
import re
class RegistrationForm(FlaskForm):
"""User registration form"""
username = StringField('Username', validators=[
DataRequired(),
Length(min=3, max=20, message='Username must be between 3 and 20 characters')
])
email = StringField('Email', validators=[
DataRequired(),
Email(message='Please enter a valid email address')
])
password = PasswordField('Password', validators=[
DataRequired(),
Length(min=6, message='Password must be at least 6 characters long')
])
confirm_password = PasswordField('Confirm Password', validators=[
DataRequired(),
EqualTo('password', message='Passwords must match')
])
medical_condition = SelectField('Medical Condition (Optional)',
choices=[
('', 'Select a condition (optional)'),
('diabetes', 'Diabetes'),
('hypertension', 'Hypertension'),
('heart_disease', 'Heart Disease'),
('celiac', 'Celiac Disease'),
('gluten_intolerance', 'Gluten Intolerance'),
('lactose_intolerance', 'Lactose Intolerance'),
('egg_allergy', 'Egg Allergy'),
('peanut_allergy', 'Peanut Allergy'),
('soy_allergy', 'Soy Allergy'),
('corn_allergy', 'Corn Allergy'),
('obesity', 'Obesity'),
('cholesterol', 'High Cholesterol')
])
submit = SubmitField('Register')
def validate_username(self, username):
"""Custom validation for username"""
# Must only contain letters, numbers, and underscores
if not re.match(r'^[a-zA-Z0-9_]+$', username.data):
raise ValidationError('Username can only contain letters, numbers, and underscores')
# Must start with a letter
if not re.match(r'^[a-zA-Z]', username.data):
raise ValidationError('Username must start with a letter')
# Must contain at least one letter (not purely numeric or underscores)
if not re.search(r'[a-zA-Z]', username.data):
raise ValidationError('Username must contain at least one letter')
def validate_password(self, password):
"""Custom validation for password strength"""
if len(password.data) < 6:
raise ValidationError('Password must be at least 6 characters long')
# Check for at least one letter and one number
if not re.search(r'[A-Za-z]', password.data):
raise ValidationError('Password must contain at least one letter')
if not re.search(r'\d', password.data):
raise ValidationError('Password must contain at least one number')
class LoginForm(FlaskForm):
"""User login form"""
username = StringField('Username or Email', validators=[
DataRequired(message='Please enter your username or email')
])
password = PasswordField('Password', validators=[
DataRequired(message='Please enter your password')
])
remember_me = BooleanField('Remember Me')
submit = SubmitField('Login')
class ProfileUpdateForm(FlaskForm):
"""Profile update form"""
email = StringField('Email', validators=[
DataRequired(),
Email(message='Please enter a valid email address')
])
medical_condition = SelectField('Medical Condition',
choices=[
('', 'Select a condition'),
('diabetes', 'Diabetes'),
('hypertension', 'Hypertension'),
('heart_disease', 'Heart Disease'),
('celiac', 'Celiac Disease'),
('gluten_intolerance', 'Gluten Intolerance'),
('lactose_intolerance', 'Lactose Intolerance'),
('egg_allergy', 'Egg Allergy'),
('peanut_allergy', 'Peanut Allergy'),
('soy_allergy', 'Soy Allergy'),
('corn_allergy', 'Corn Allergy'),
('obesity', 'Obesity'),
('cholesterol', 'High Cholesterol')
])
submit = SubmitField('Update Profile')
class ChangePasswordForm(FlaskForm):
"""Change password form"""
current_password = PasswordField('Current Password', validators=[
DataRequired(message='Please enter your current password')
])
new_password = PasswordField('New Password', validators=[
DataRequired(),
Length(min=6, message='Password must be at least 6 characters long')
])
confirm_new_password = PasswordField('Confirm New Password', validators=[
DataRequired(),
EqualTo('new_password', message='Passwords must match')
])
submit = SubmitField('Change Password')
def validate_new_password(self, new_password):
"""Custom validation for new password strength"""
if len(new_password.data) < 6:
raise ValidationError('Password must be at least 6 characters long')
# Check for at least one letter and one number
if not re.search(r'[A-Za-z]', new_password.data):
raise ValidationError('Password must contain at least one letter')
if not re.search(r'\d', new_password.data):
raise ValidationError('Password must contain at least one number')
class ProfileCompletionForm(FlaskForm):
"""Profile completion form for new users"""
age = IntegerField('Age', validators=[
DataRequired(message='Please enter your age'),
NumberRange(min=13, max=120, message='Age must be between 13 and 120')
])
weight = IntegerField('Weight (kg)', validators=[
DataRequired(message='Please enter your weight'),
NumberRange(min=20, max=300, message='Weight must be between 20 and 300 kg')
])
height = IntegerField('Height (cm)', validators=[
DataRequired(message='Please enter your height'),
NumberRange(min=50, max=250, message='Height must be between 50 and 250 cm')
])
gender = SelectField('Gender',
choices=[
('', 'Select your gender'),
('male', 'Male'),
('female', 'Female'),
('other', 'Other')
],
validators=[DataRequired(message='Please select your gender')])
diet_type = StringField('Diet Type (Optional)', validators=[Optional(), Length(max=50)])
allergies = StringField('Allergies (Optional)', validators=[Optional(), Length(max=200)])
calorie_target = IntegerField('Daily Calorie Target', validators=[
DataRequired(message='Please enter your calorie target'),
NumberRange(min=800, max=5000, message='Calorie target must be between 800 and 5000')
])
goal = SelectField('Fitness Goal',
choices=[
('', 'Select your goal'),
('lose_weight', '🔥 Lose Weight'),
('gain_muscle', '💪 Gain Muscle'),
('maintain_fitness', '⚖️ Maintain Fitness'),
('improve_health', '❤️ Improve Overall Health')
],
validators=[DataRequired(message='Please select a fitness goal')])
submit = SubmitField('Complete Profile')