/
views.py
221 lines (163 loc) · 7.18 KB
/
views.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""this will render html templates depending on what url we go to"""
from flask import Flask, render_template, g, redirect
import sqlite3
from flask.ext.wtf import Form
from wtforms import StringField, SelectField
from wtforms.validators import DataRequired, Optional
from models import *
import saladMath as sm
from contextlib import closing
#for the css compiler
from flask.ext.assets import Environment, Bundle
app = Flask(__name__)
# this is referencing our config.py file, and weirdly lets us get away with not using a secret key
app.config.from_object("config")
#This is a sass compiler for styling
assets = Environment(app)
scss = Bundle('sass/main.scss', filters='pyscss', output='css/all.css')
assets.register('scss_all', scss)
class SaladForm(Form):
### for some reason this didn't work up here so we did the work down in index() where we'd
### already opened a database connection. Maybe we can't open two at once?
# with app.app_context():
# db = get_db()
# with db:
# cur = db.cursor()
# cur.execute('SELECT category from category')
# categoryList = cur.fetchall()
name = StringField('name', validators=[DataRequired()])
# doing validators=[DataRequired()] did not work for the SelectField
# category = SelectField('category', validators=[Optional()])
ingredient = StringField('ingredient', validators=[DataRequired()])
# this is helpful because we want to get our category choices from the database
# if your choices were static, you could do: SelectFeild('category', choices=[(0,'greens'),(1,'veggies')], validators=...)
# def add_category_choices(self, category_tuples):
# self.category.choices = category_tuples
@app.route('/')
# the GET is important to display the form and stuff, and the POST lets us grab the form input
@app.route('/index')
def index():
# opening a connection to our database (get_db comes from models)
db = get_db()
# with db:
with closing(db.cursor()) as cur:
# cur = db.cursor()
greens_form = SaladForm()
veggies_form = SaladForm()
protein_form = SaladForm()
dressing_form = SaladForm()
other_form = SaladForm()
formList = [greens_form, veggies_form, protein_form,dressing_form, other_form]
category_keys = ["Greens", "Veggies", "Protein", "Dressing", "Other"]
form_cat_zip = [(formList[i], category_keys[i]) for i in range(5)]
# [(greens_form, "Greens"), (veggies_form, "Veggies")]
### this section is to get the categories from our category table to use as drop-down choices
cur.execute('SELECT category from category')
categoryList = cur.fetchall()
# the categories come as tuples, but we only want the first part, x[0]
# these come as unicode strings, and we can .encode('utf8') to make them pretty, normal strings
category_tuples = [(x[0].encode('utf8'),x[0].encode('utf8')) for x in categoryList]
# for form in formList:
# form.add_category_choices(category_tuples)
# db.close()
### this section is for pretty printing
db = get_db()
with closing(db.cursor()) as cur:
cur = db.cursor()
cur.execute('SELECT person, category, ingredient FROM Ingredient')
IngredientTableTuples = cur.fetchall()
categories = [x[0] for x in category_tuples]
categories_for_printing = {}
for cat in categories:
categories_for_printing[cat] = [(x[2].encode('utf8'), x[0].encode('utf8')) for x in IngredientTableTuples if x[1] == cat]
# print categories_for_printing
db.close()
ourRatios = sm.calculateRatios(categories_for_printing)
warningsList = sm.warnAboutRatios(sm.perfectSaladRatios, ourRatios)
warningsString = ", ".join(warningsList)
# # pretend_ingredients = ["cats", "spinace", "avodabo", "carobs"]
# pretend_categories = ["greebs", "vebebbggeez", "FROOBs"]
return render_template('index.html',
form_cat_zip=form_cat_zip,
categoryDict=categories_for_printing,
warnings=warningsString,
)
# ingredient_list=pretend_ingredients,
# categories=pretend_categories)
@app.route('/Veggies', methods=["POST"])
def veggies():
veggies_form = SaladForm()
db = get_db()
with closing(db.cursor()) as cur:
if veggies_form.validate_on_submit():
print "Veggies validated on submit"
cur.execute('insert into Ingredient (person, category, ingredient) values (?, ?, ?)',
[veggies_form.name.data, 'Veggies', veggies_form.ingredient.data])
else:
print "Veggies didn't validate"
db.commit()
db.close()
return redirect('/index')
@app.route('/Protein', methods=["POST"])
def protein():
protein_form = SaladForm()
db = get_db()
with closing(db.cursor()) as cur:
if protein_form.validate_on_submit():
print "Protein validated on submit"
cur.execute('insert into Ingredient (person, category, ingredient) values (?, ?, ?)',
[protein_form.name.data, 'Protein', protein_form.ingredient.data])
else:
print "Protein didn't validate"
db.commit()
db.close()
return redirect('/index')
@app.route('/Greens', methods=["POST"])
def greens():
greens_form = SaladForm()
db = get_db()
with closing(db.cursor()) as cur:
if greens_form.validate_on_submit():
print "Greens validated on submit"
cur.execute('insert into Ingredient (person, category, ingredient) values (?, ?, ?)',
[greens_form.name.data, 'Greens', greens_form.ingredient.data])
else:
print "Greens didn't validate"
db.commit()
db.close()
return redirect('/index')
@app.route('/Dressing', methods=["POST"])
def dressing():
dressing_form = SaladForm()
db = get_db()
with closing(db.cursor()) as cur:
if dressing_form.validate_on_submit():
print "Dressing validated on submit"
cur.execute('insert into Ingredient (person, category, ingredient) values (?, ?, ?)',
[dressing_form.name.data, 'Dressing', dressing_form.ingredient.data])
else:
print "Dressing didn't validate"
db.commit()
db.close()
return redirect('/index')
@app.route('/Other', methods=["POST"])
def other():
other_form = SaladForm()
db = get_db()
with closing(db.cursor()) as cur:
if other_form.validate_on_submit():
print "Other validated on submit"
cur.execute('insert into Ingredient (person, category, ingredient) values (?, ?, ?)',
[other_form.name.data, 'Other', other_form.ingredient.data])
else:
print "Other didn't validate"
db.commit()
db.close()
return redirect('/index')
# def get_categories():
# pass
# this is what makes our app start up when we run python views.py
# I'm pretty sure that the debug=True option makes it so that we don't need to
# restart the server each time we make a change to one of these files
if __name__ == '__main__':
app.run(debug=True)