-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.py
158 lines (145 loc) · 4.35 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
from datetime import datetime
from flask_wtf import Form
from wtforms import StringField, SelectField, SelectMultipleField, DateTimeField, BooleanField
from wtforms.validators import DataRequired, URL, ValidationError, Optional, Length, re
from models import Genre, State
from enum import Enum
# phone validation
def isValidPhone(form, field):
if not re.search(r"^[0-9]{3}-[0-9]{3}-[0-9]{4}$", field.data):
raise ValidationError("Invalid phone number, format must be xxx-xxx-xxxx")
# states from db
def getStates():
# prevent error on flask db init
try:
db_states = State.query.with_entities(State.abbreviation, State.abbreviation).all()
except:
db_states = ''
return db_states
# genres from db
def getGenres():
# prevent error on flask db init
try:
db_genres = Genre.query.with_entities(Genre.name, Genre.name).all()
except:
db_genres = ''
return db_genres
# Enum + Custom Validator for genre to complete the TODO requirement
class E_Genre(Enum):
Alternative = 'Alternative'
Blues = 'Blues'
Classical = 'Classical'
Country = 'Country'
Electronic = 'Electronic'
Folk = 'Folk'
Funk = 'Funk'
Hip_Hop = 'Hip-Hop'
Heavy_Metal = 'Heavy Metal'
Instrumental = 'Instrumental'
Jazz = 'Jazz'
Musical_Theatre = 'Musical Theatre'
Pop = 'Pop'
Punk = 'Punk'
R_AND_B = 'R&B'
Reggae = 'Reggae'
Rock_n_Roll = 'Rock n Roll'
Soul = 'Soul'
Other = 'Other'
@classmethod
def choices(cls):
return [ (choice.value, choice.value) for choice in cls ]
def isValidGenre(form, field):
try:
valid_genres = set([ (choice.value) for choice in E_Genre ])
if not valid_genres.intersection(set(field.data)):
raise ValidationError("Invalid Genre!")
except Exception as e:
raise ValidationError(e.args)
# Forms
class ShowForm(Form):
artist_id = StringField(
'artist_id',
validators=[DataRequired()],
)
venue_id = StringField(
'venue_id',
validators=[DataRequired()],
)
start_time = DateTimeField(
'start_time',
validators=[DataRequired()],
default= datetime.today()
)
class VenueForm(Form):
name = StringField(
'name', validators=[DataRequired()]
)
city = StringField(
'city', validators=[DataRequired()]
)
state = SelectField(
'state', validators=[DataRequired()],
choices=getStates()
)
address = StringField(
'address', validators=[DataRequired()]
)
phone = StringField(
'phone', validators=[isValidPhone,Length(min=10, max=18), Optional()]
)
image_link = StringField(
'image_link', validators=[Length(max=500), Optional()]
)
genres = SelectMultipleField(
# TODO implement enum restriction
'genres', validators=[DataRequired(), isValidGenre],
choices=getGenres()
)
facebook_link = StringField(
'facebook_link', validators=[URL(), Optional()]
)
website_link = StringField(
'website_link', validators=[URL(), Optional()]
)
seeking_artist = BooleanField(
'seeking_artist', validators=[ Optional()]
)
seeking_description = StringField(
'seeking_description', validators=[ Optional()]
)
class ArtistForm(Form):
name = StringField(
'name', validators=[DataRequired()]
)
city = StringField(
'city', validators=[DataRequired()]
)
state = SelectField(
'state', validators=[DataRequired()],
choices=getStates()
)
phone = StringField(
# TODO implement validation logic for state
'phone', validators=[isValidPhone, Length(min=10, max=18), Optional()]
)
image_link = StringField(
'image_link', validators=[ Length(max=500), Optional()]
)
genres = SelectMultipleField(
# TODO implement enum restriction
'genres', validators=[DataRequired(), isValidGenre],
choices=getGenres()
)
facebook_link = StringField(
'facebook_link', validators=[URL(), Optional()]
)
website_link = StringField(
'website_link', validators=[URL(), Optional()]
)
seeking_venue = BooleanField(
'seeking_venue', validators=[ Optional()]
)
seeking_description = StringField(
'seeking_description', validators=[ Optional()]
)
# TODO IMPLEMENT NEW ARTIST FORM AND NEW SHOW FORM