-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.py
53 lines (37 loc) · 2.16 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
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, TextAreaField, SelectField
from wtforms.validators import DataRequired, Email, Length, Optional, URL
from models import Allergy, DietaryPreference
class AddUserForm(FlaskForm):
"""Form for adding users."""
username = StringField('Username', validators=[DataRequired()])
email = StringField('E-mail', validators=[DataRequired(), Email()])
password = PasswordField('Password', validators=[Length(min=6)])
class EditForm(FlaskForm):
"""Edit g.user preferences"""
username = StringField('Username', validators=[DataRequired()])
email = StringField('E-mail', validators=[DataRequired(), Email()])
allergies = SelectField('Allergies', default=None, validators=[Optional()])
diet_prefs = SelectField('Dietary Preferences',
default=None, validators=[Optional()])
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Populate the allergy choices from the database
self.allergies.choices = [(None, 'Add Allergy')] + [(allergy.id, allergy.type)
for allergy in Allergy.query.all()]
# Populate the diet preferences choices from the database
self.diet_prefs.choices = [(None, 'Add Dietary Preference')] + [(diet.id, diet.type)
for diet in DietaryPreference.query.all()]
class LoginForm(FlaskForm):
"""Login form."""
username = StringField('Username', validators=[DataRequired()])
password = PasswordField('Password', validators=[Length(min=6)])
class IngredientSearchForm(FlaskForm):
ingredients = StringField('Ingredients', validators=[DataRequired()])
class AddRecipeForm(FlaskForm):
"""Form for adding users."""
title = StringField('Title', validators=[DataRequired()])
photo_url = StringField('Recipe Photo', default=None,
validators=[Optional(), URL()])
ingredients = TextAreaField('Ingredients', validators=[DataRequired()])
instructions = TextAreaField('Instructions', validators=[DataRequired()])