This repository has been archived by the owner on Mar 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.py
110 lines (84 loc) · 4.32 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
# YTCTF Platform
# Copyright © 2018-2019 Evgeniy Filimonov <[email protected]>
# See full NOTICE at http://github.com/YummyTacos/YTCTF
from flask import g
from flask_wtf import FlaskForm
from wtforms import (SubmitField, StringField, PasswordField, ValidationError, TextAreaField,
SelectMultipleField, BooleanField, FileField)
from wtforms.fields.html5 import EmailField, URLField, IntegerField
from wtforms.validators import (DataRequired, Email, EqualTo, Regexp, Optional, URL, InputRequired,
Length)
from re import compile as re_compile
from app import app
from models import User
from utils import find_user
class SubmitForm(FlaskForm):
submit = SubmitField()
class UserDataForm(SubmitForm):
email = EmailField(validators=[DataRequired(), Email()])
login = StringField(validators=[DataRequired()])
first_name = StringField(validators=[DataRequired(), Length(max=32,
message='Слишком длинное имя')])
last_name = StringField(validators=[Optional(), Length(max=32,
message='Слишком длинная фамилия')])
group = IntegerField(validators=[Optional()])
@staticmethod
def validate_group(cls, field):
if field.data is None:
return
if not (71 <= field.data <= 76 or 81 <= field.data <= 86):
raise ValidationError('Неверно введена группа')
class UserForm(SubmitForm):
login = StringField(validators=[DataRequired()])
password = PasswordField(validators=[DataRequired()])
class LoginForm(UserForm):
@staticmethod
def validate_login(cls, field):
if find_user(field.data) is None:
raise ValidationError('Неверное имя пользователя')
def validate_password(self, field):
user = find_user(self.login.data)
if user is None:
return # Another validator will fail
if not user.verify_password(field.data):
raise ValidationError('Неверный пароль')
class RegisterForm(UserForm, UserDataForm):
password2 = PasswordField(validators=[EqualTo('password')])
@staticmethod
def validate_login(cls, field):
if find_user(field.data) is not None:
raise ValidationError('Такой пользователь уже зарегистрирован!')
@staticmethod
def validate_email(cls, field):
if User.query.filter(User.email == field.data).one_or_none() is not None:
raise ValidationError('Такой e-mail уже зарегистрирован!')
class FlagForm(SubmitForm):
flag = StringField('Флаг', validators=[DataRequired()])
def __init__(self, flag):
super().__init__()
self._flag_re = re_compile(app.config.get('FLAG_REGEXP', r'^\w*ctf.+'))
self._right_flag = flag
def validate_flag(self, field):
field.data = field.data.strip()
if self._flag_re.fullmatch(field.data) is None:
raise ValidationError('Неверный формат флага.')
if field.data != self._right_flag:
raise ValidationError('Неверный флаг. Учти, что регистр флага имеет значение.')
class ChangePasswordForm(SubmitForm):
old_password = PasswordField(validators=[DataRequired()])
new_password = PasswordField(validators=[DataRequired()])
new_password2 = PasswordField(validators=[DataRequired(), EqualTo('new_password')])
@staticmethod
def validate_old_password(cls, field):
if not g.user.verify_password(field.data):
raise ValidationError('Неверный пароль')
class TaskForm(SubmitForm):
name = StringField(validators=[DataRequired()])
category = StringField(validators=[DataRequired()])
description = TextAreaField(validators=[DataRequired()])
link = URLField(validators=[Optional(), URL()])
files = FileField('Файлы не выбраны', validators=[Optional()])
points = IntegerField(validators=[InputRequired()])
flag = StringField(validators=[DataRequired(), Regexp(app.config.get('FLAG_REGEXP'))])
author = SelectMultipleField(validators=[DataRequired()], coerce=int)
is_hidden = BooleanField('Скрытый таск')