-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforms.py
93 lines (82 loc) · 2.44 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
from flask_wtf import FlaskForm
from wtforms import (StringField, PasswordField, IntegerField, TextAreaField)
from wtforms.fields.html5 import DateField
from wtforms.validators import (
DataRequired, Length, EqualTo, ValidationError, NumberRange)
from flask import request
import models
def name_exists(form, field):
try:
models.User.get(models.User.username**field.data)
raise ValidationError('Name already exists!')
except models.DoesNotExist:
return None
def title_exists(form, field):
try:
# edit only if the slug is the same or it doesnt exist yet
models.Entry.get(models.Entry.slug**models.create_slug(field.data))
if request.path.endswith('/edit'):
slug = request.path.replace('/entries/', '')
slug = slug.replace('/edit', '')
if slug == models.create_slug(field.data):
return None
raise ValidationError('Title already exists! Try a different one')
except models.DoesNotExist:
return None
class LoginForm(FlaskForm):
username = StringField(
'username',
validators=[
DataRequired()
]
)
password = PasswordField(
'password',
validators=[
DataRequired(),
Length(min=3)
]
)
class RegisterForm(FlaskForm):
username = StringField(
'username',
validators=[
DataRequired(),
name_exists
]
)
password = PasswordField(
'password',
validators=[
DataRequired(),
Length(min=3),
EqualTo('password2', 'Passwords must match!')
]
)
password2 = PasswordField(
'confirm password',
validators=[
DataRequired(),
Length(min=3)
]
)
class EntryForm(FlaskForm):
title = StringField(
'Title', validators=[DataRequired(), title_exists]
)
date = DateField(
'Date', validators=[DataRequired()]
)
time_spent = IntegerField(
'Time spent(in hours)', validators=[DataRequired(),
NumberRange(min=0, max=24)]
)
what_i_learned = TextAreaField(
'What i learned', validators=[DataRequired()]
)
resources_to_remember = TextAreaField(
'Resources to remember', validators=[DataRequired()]
)
tags = StringField(
'Tags (space separated list example: work hobby happy)'
)