-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforms.py
More file actions
190 lines (170 loc) · 6.64 KB
/
forms.py
File metadata and controls
190 lines (170 loc) · 6.64 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
190
from flask_wtf import FlaskForm
from wtforms import StringField, TextAreaField, PasswordField, SelectField, IntegerField
from wtforms.validators import DataRequired, Length, Email, EqualTo, ValidationError
from wtforms.widgets import TextArea
from typing import Optional
import re
class CKTextAreaWidget(TextArea):
"""Custom textarea widget for CKEditor"""
def __call__(self, field, **kwargs):
kwargs.setdefault('class_', 'ckeditor')
return super(CKTextAreaWidget, self).__call__(field, **kwargs)
class CKTextAreaField(TextAreaField):
"""Custom textarea field for CKEditor"""
widget = CKTextAreaWidget()
class CustomValidator:
"""Custom validation methods"""
@staticmethod
def validate_username(form, field):
"""Validate username format"""
username = field.data
if not re.match("^[a-zA-Z0-9_]+$", username):
raise ValidationError('Username can only contain letters, numbers, and underscores.')
@staticmethod
def validate_password_strength(form, field):
"""Validate password strength"""
password = field.data
if len(password) < 8:
raise ValidationError('Password must be at least 8 characters long.')
if not re.search(r"[A-Z]", password):
raise ValidationError('Password must contain at least one uppercase letter.')
if not re.search(r"[a-z]", password):
raise ValidationError('Password must contain at least one lowercase letter.')
if not re.search(r"\d", password):
raise ValidationError('Password must contain at least one number.')
class RegisterForm(FlaskForm):
"""User registration form"""
name = StringField(
"Full Name",
validators=[
DataRequired(message="Full name is required"),
Length(min=2, max=100, message="Name must be between 2 and 100 characters")
],
render_kw={"placeholder": "Enter your full name", "class": "form-control"}
)
username = StringField(
"Username",
validators=[
DataRequired(message="Username is required"),
Length(min=3, max=20, message="Username must be between 3 and 20 characters"),
CustomValidator.validate_username
],
render_kw={"placeholder": "Choose a username", "class": "form-control"}
)
email = StringField(
"Email",
validators=[
DataRequired(message="Email is required"),
Email(message="Please enter a valid email address"),
Length(max=120, message="Email must be less than 120 characters")
],
render_kw={"placeholder": "Enter your email", "class": "form-control"}
)
password = PasswordField(
"Password",
validators=[
DataRequired(message="Password is required"),
CustomValidator.validate_password_strength
],
render_kw={"placeholder": "Create a strong password", "class": "form-control"}
)
confirm_password = PasswordField(
"Confirm Password",
validators=[
DataRequired(message="Please confirm your password"),
EqualTo('password', message='Passwords must match')
],
render_kw={"placeholder": "Confirm your password", "class": "form-control"}
)
class LoginForm(FlaskForm):
"""User login form"""
username = StringField(
"Username",
validators=[
DataRequired(message="Username is required"),
Length(min=3, max=20, message="Username must be between 3 and 20 characters")
],
render_kw={"placeholder": "Enter your username", "class": "form-control"}
)
password = PasswordField(
"Password",
validators=[
DataRequired(message="Password is required")
],
render_kw={"placeholder": "Enter your password", "class": "form-control"}
)
class ArticleForm(FlaskForm):
"""Article creation/editing form"""
title = StringField(
"Title",
validators=[
DataRequired(message="Title is required"),
Length(min=5, max=200, message="Title must be between 5 and 200 characters")
],
render_kw={"placeholder": "Enter article title", "class": "form-control"}
)
content = CKTextAreaField(
"Content",
validators=[
DataRequired(message="Content is required"),
Length(min=50, message="Content must be at least 50 characters long")
],
render_kw={"placeholder": "Write your article content here", "rows": "10"}
)
keywords = StringField(
"Keywords",
validators=[
DataRequired(message="Keywords are required"),
Length(min=3, max=200, message="Keywords must be between 3 and 200 characters")
],
render_kw={"placeholder": "Enter keywords separated by commas", "class": "form-control"}
)
class ContactForm(FlaskForm):
"""Contact form"""
name = StringField(
"First Name",
validators=[
DataRequired(message="First name is required"),
Length(min=2, max=50, message="Name must be between 2 and 50 characters")
],
render_kw={"placeholder": "Enter your first name", "class": "form-control"}
)
surname = StringField(
"Last Name",
validators=[
DataRequired(message="Last name is required"),
Length(min=2, max=50, message="Last name must be between 2 and 50 characters")
],
render_kw={"placeholder": "Enter your last name", "class": "form-control"}
)
email = StringField(
"Email",
validators=[
DataRequired(message="Email is required"),
Email(message="Please enter a valid email address"),
Length(max=120, message="Email must be less than 120 characters")
],
render_kw={"placeholder": "Enter your email", "class": "form-control"}
)
message = TextAreaField(
"Message",
validators=[
DataRequired(message="Message is required"),
Length(min=10, max=1000, message="Message must be between 10 and 1000 characters")
],
render_kw={"placeholder": "Write your message here", "class": "form-control", "rows": "5"}
)
class PredictionForm(FlaskForm):
"""Form for machine learning predictions"""
days = SelectField(
"Prediction Days",
choices=[
(7, "7 Days"),
(14, "14 Days"),
(30, "30 Days")
],
coerce=int,
default=7,
validators=[DataRequired(message="Please select prediction period")],
render_kw={"class": "form-control"}
)