-
Notifications
You must be signed in to change notification settings - Fork 4
/
forms.py
324 lines (281 loc) · 8.41 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
from wtforms import *
from wtforms.validators import *
from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileRequired
from utils import _
from validation import *
from config import TOKEN_LIFETIME, BAN_DAYS
from captcha import CAPTCHA_REGEX
def ConfigField(name, description, *validators):
return StringField(
name,
validators = [Required(), SizeRange(1, 256), *validators],
description = description
)
def Required():
return InputRequired(_('This field is required.'))
def Username():
def validator(form, field):
if not REGEX_USERNAME.fullmatch(field.data):
raise ValidationError(_('Invalid username format.'))
return validator
def PasswordConfirm():
return EqualTo('password', message=_('Passwords are inconsistent.'))
def PositiveInteger():
return Regexp(REGEX_PINT, message=_('Positive integer required'))
class RegisterForm(FlaskForm):
mail = StringField(
_('Email Address'),
validators = [
Required(), Email(_('Invalid Email Address.')), SizeRange(3, 64)
],
description = _('Used to enable your account and reset your password.')
)
name = StringField(
_('Nickname'),
validators = [
Required(), SizeRange(3, 32), Username()
],
description = _('Nickname as well as identifier.')
)
password = PasswordField(
_('Password'),
validators = [
Required()
],
description = _("Your password. Won't be encrypted if not using HTTPS.")
)
confirm = PasswordField(
_('Password Confirmation'),
validators = [
PasswordConfirm()
],
description = _('Repeat your password to confirm.')
)
captcha = StringField(
_('Captcha'),
validators = [
Required(), Regexp(CAPTCHA_REGEX, message=_('Invalid captcha'))
],
description = _('Input the text on the image.')
)
class LoginForm(FlaskForm):
login_name = StringField(
_('Login name'),
validators = [
Required(), SizeRange(3, 64)
],
description = _('Login Name can be either nickname or email address.')
)
password = PasswordField(
_('Password'),
validators = [
Required()
],
description = _("Your password. Won't be encrypted if not using HTTPS.")
)
remember_me = BooleanField(
_('Remember me')
)
class GetTokenForm(FlaskForm):
mail = StringField(
_('Email Address'),
validators = [
Required(), Email(_('Invalid Email Address.'))
],
description = _('Email address of your account.')
)
class PasswordResetForm(FlaskForm):
new_mail = StringField(
_('[Optional] New Email'),
validators = [
Optional(), Email(_('Invalid Email Address.'))
],
description = _('New email address (optional).')
)
password = PasswordField(
_('New Password'),
validators = [
Required()
],
description = _("New password. Won't be encrypted if not using HTTPS.")
)
confirm = PasswordField(
_('Password Confirmation'),
validators = [
PasswordConfirm()
],
description = _('Repeat your password to confirm.')
)
token = StringField(
_('Token'),
validators = [
Regexp(REGEX_TOKEN, message=_('Invalid token format'))
],
description = (
_('The token sent to you by email. Valid in %d minutes.')
% TOKEN_LIFETIME
)
)
class ProfileForm(FlaskForm):
bio = TextAreaField(_('Bio'), description=_('A brief description of you.'))
location = StringField(
_('Location'),
validators = [Optional(), SizeRange(1, 64)],
description = _('Your geolocation.')
)
birth_year = IntegerField(
_('Birth Year'),
validators = [Optional(), NumberRange(min=1000)],
description = _('Your birth year, at least four digits.')
)
occupation = StringField(
_('Occupation'),
validators = [Optional(), SizeRange(1, 32)],
description = _('Your work.')
)
im_accounts = StringField(
_('IM Accounts'),
validators = [Optional(), SizeRange(1, 256)],
description = _('Your instant-messaging accounts.')
)
class BanForm(FlaskForm):
days = SelectField(
_('Duration'),
choices = [
(
str(days),
_('%d Day', '%d Days', days) % days
)
for days in BAN_DAYS
],
coerce = str
)
class LevelChangeForm(FlaskForm):
level = SelectField(
_('Privilege'),
choices = [
(0, _('None')), (1, _('Moderator')), (2, _('Administrator'))
],
coerce = int
)
class ConfigEditForm(FlaskForm):
site_name = ConfigField(
_('Site Name'),
_('Name of this site.')
)
site_url = ConfigField(
_('Site URL'),
_('URL of this site. Example: https://foobar.invalid')
)
mail_addr = ConfigField(
_('Email Address'),
_('Email address showed in mails sent by this site.')
)
count_topic = ConfigField(
_('Topics per page'),
_('The number of topics shown in one page in a topic list.'),
PositiveInteger()
)
count_post = ConfigField(
_('Posts per page'),
_('The number of posts shown in one page in a post list.'),
PositiveInteger()
)
count_item = ConfigField(
_('Items per page'),
_('The number of items shown in one page in a list'),
PositiveInteger()
)
class TagEditForm(FlaskForm):
slug = StringField(
_('Slug'),
validators = [
Required(),
SizeRange(1, 32),
Regexp(REGEX_SLUG, message=_('Invalid format.'))
],
description = _('Name in URL. Only Latin, number and "-" are allowed.')
)
name = StringField(
_('Name'),
validators = [Required(), SizeRange(1, 64)],
description = _('Name that is displayed.')
)
description = TextAreaField(
_('Description'),
validators = [Required(), SizeRange(1, 256)],
description = _('Detailed description.')
)
class ImageUploadForm(FlaskForm):
image = FileField(
_('Image'),
validators = [FileRequired()],
description = _('Image file to upload (PNG/JPG/GIF).')
)
class TopicAddForm(FlaskForm):
# size range of the title must be consistent with the front-end script
title = StringField(_('Title'), validators=[Required(), SizeRange(1, 64)])
content = TextAreaField(
_('Content'), validators=[Required(), SizeRange(1, 15000)]
)
tags = SelectMultipleField(_('Tags'), validators=[Optional()], coerce=str)
class PostAddForm(FlaskForm):
content = TextAreaField(
_('Content'), validators=[Required(), SizeRange(1, 15000)]
)
class TopicTagManageForm(FlaskForm):
tags = SelectMultipleField(_('Tags'), validators=[Optional()], coerce=str)
class FaceAddForm(FlaskForm):
name = StringField(
_('Name'),
validators = [
Required(),
SizeRange(1, 32)
],
description = _('Name of the face.')
)
hash_value = StringField(
_('Hash'),
validators = [
Required(),
Regexp(REGEX_SHA256_PART, message=_('Invalid format'))
],
description = _('Image hash value.')
)
class TiebaSyncForm(FlaskForm):
password = PasswordField(
_('Password'),
validators = [
Required()
],
description = _("Your password. Used to encrypt BDUSS.")
)
name = StringField(
_('Tieba ID'),
validators = [
Required(), SizeRange(1, 64)
],
description = _('Username of Tieba.')
)
bduss = StringField(
_('BDUSS'),
validators = [
Required(), SizeRange(1, 400)
],
description = _('BDUSS in cookie of Tieba.')
)
fakeip = StringField(
_('FakeIP'),
validators = [
Optional()
],
description = _('Fake IP used when creating posts in Tieba.')
)
ua = StringField(
_('UA'),
validators = [
Optional()
],
description = _('User Agent used when creating posts in Tieba.')
)