This repository has been archived by the owner on Jun 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
forms.py
61 lines (49 loc) · 2.37 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
from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileAllowed, FileRequired
from wtforms import StringField, PasswordField
from wtforms.validators import DataRequired, Email, EqualTo, ValidationError
from models import UserAccount, Document
from flask_login import current_user
class RegistrationForm(FlaskForm):
display_name = StringField("Display Name", validators=[DataRequired()])
username = StringField("Username", validators=[DataRequired()])
email = StringField("E-mail", validators=[DataRequired(), Email()])
password = PasswordField("Password", validators=[DataRequired()])
confirm_password = PasswordField(
"Confirm Password", validators=[DataRequired(), EqualTo("password")]
)
def validate_display_name(self, display_name):
user = UserAccount.query.filter_by(
display_name=display_name.data
).first()
if user is not None:
raise ValidationError("Please use a different display name.")
def validate_username(self, username):
user = UserAccount.query.filter_by(username=username.data).first()
if user is not None:
raise ValidationError("Please use a different username.")
def validate_email(self, email):
user = UserAccount.query.filter_by(email=email.data).first()
if user is not None:
raise ValidationError("Please use a different email address.")
class LoginForm(FlaskForm):
username = StringField("Username", validators=[DataRequired()])
password = PasswordField("Password", validators=[DataRequired()])
class DocumentUploadForm(FlaskForm):
title = StringField("Title", validators=[DataRequired()])
document = FileField(
validators=[
FileRequired(),
FileAllowed(['md', 'docx', 'rtf', 'txt', 'odt'],
'.md, .docx, .rtf, .txt, and .odt documents only')])
def validate_title(self, title):
doc = Document.query.filter_by(
author_id=current_user.id, title=title.data).first()
if doc is not None:
raise ValidationError("You have already published a work with this name.")
class DocumentReuploadForm(FlaskForm):
document = FileField(
validators=[
FileRequired(),
FileAllowed(['md', 'docx', 'rtf', 'txt', 'odt'],
'.md, .docx, .rtf, .txt, and .odt documents only')])