-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.py
221 lines (209 loc) · 5.6 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
# -*- coding: utf-8 -*-
__author__ = 'QB'
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, SelectField, FileField, TextAreaField, IntegerField
from wtforms.validators import DataRequired, EqualTo, ValidationError
from models import User
from flask import session as form_session
# 登录表单
class LoginForm(FlaskForm):
account = StringField(
validators=[
DataRequired(u'账号不能为空')
],
description=u'账号',
render_kw={
'class': 'form-control',
'placeholder': '账号'
}
)
pwd = PasswordField(
validators=[
DataRequired(u'密码不能为空')
],
description=u'密码',
render_kw={
'class': 'form-control',
'placeholder': '密码'
}
)
submit = SubmitField(
u'登录',
render_kw={
'class': 'btn btn-success col-lg-12'
}
)
def validate_pwd(self, field):
pwd = field.data
user = User.query.filter_by(account=self.account.data).first()
if not user.check_pwd(pwd):
raise ValidationError(u'密码不正确')
# 注册表单
class RegisterForm(FlaskForm):
account = StringField(
validators=[
DataRequired(u'账号不能为空')
],
description=u'账号',
render_kw={
'class': 'form-control',
'placeholder': '账号'
}
)
pwd = PasswordField(
validators=[
DataRequired(u'密码不能为空')
],
description=u'密码',
render_kw={
'class': 'form-control',
'placeholder': '密码'
}
)
re_pwd = PasswordField(
validators=[
DataRequired(u'确认密码不能为空'),
EqualTo('pwd', message='两次密码不一致!')
],
description=u'确认密码',
render_kw={
'class': 'form-control',
'placeholder': '确认密码'
}
)
captcha = StringField(
validators=[
DataRequired(u'验证码不能为空')
],
description=u'验证码',
render_kw={
'class': 'form-control',
'placeholder': '验证码'
}
)
submit = SubmitField(
u'注册',
render_kw={
'class': 'btn btn-primary col-lg-12'
}
)
# 自定义字段验证规则:validate 下划线 字段名
def validate_account(self, field):
account = field.data
user = User.query.filter_by(account=account).count()
if user > 0:
raise ValidationError(u'账号已存在')
# 自定义验证码验证规则: validate 下划线 字段名
def validate_captcha(self, field):
captcha = field.data
if not form_session['captcha']:
raise ValidationError(u'非法操作')
if form_session['captcha'].lower() != captcha.lower():
raise ValidationError(u'验证码不正确')
# 发布文章表单
class ArticleAddForm(FlaskForm):
title = StringField(
validators=[
DataRequired(u'标题不能为空')
],
description=u'标题',
render_kw={
'class': 'form-control',
'placeholder': '请输入标题'
}
)
# 强制类型为整型
category = SelectField(
validators=[
DataRequired(u'分类不能为空')
],
description=u'分类',
choices=[(1, u'Python'), (2, u'MongoDB'), (3, u'Redis')],
default=3,
coerce=int,
render_kw={
'class': 'form-control'
}
)
logo = FileField(
validators=[
DataRequired(u'请上传封面')
],
description=u"封面",
render_kw={
"class": "form-control-file",
"style": "margin-left: 2px;"
}
)
content = TextAreaField(
validators=[
DataRequired(u'内容不能为空')
],
description=u'内容',
render_kw={
'style': 'height: 300px;',
'id': 'content'
}
)
submit = SubmitField(
u'发布文章',
render_kw={
'class': 'btn btn-primary'
}
)
# 编辑文章表单
class ArticleEditForm(FlaskForm):
id = IntegerField(
validators=[
DataRequired(u'编号不能为空')
]
)
title = StringField(
validators=[
DataRequired(u'标题不能为空')
],
description=u'标题',
render_kw={
'class': 'form-control',
'placeholder': '请输入标题'
}
)
# 强制类型为整型
category = SelectField(
validators=[
DataRequired(u'分类不能为空')
],
description=u'分类',
choices=[(1, u'Python'), (2, u'MongoDB'), (3, u'Redis')],
default=3,
coerce=int,
render_kw={
'class': 'form-control'
}
)
logo = FileField(
validators=[
DataRequired(u'请上传封面')
],
description=u"封面",
render_kw={
"class": "form-control-file",
"style": "margin-left: 2px;"
}
)
content = TextAreaField(
validators=[
DataRequired(u'内容不能为空')
],
description=u'内容',
render_kw={
'style': 'height: 300px',
'id': 'content'
}
)
submit = SubmitField(
u'编辑文章',
render_kw={
'class': 'btn btn-primary'
}
)