-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
227 lines (171 loc) · 7.01 KB
/
app.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
222
223
224
225
226
227
from flask import Flask, request, session, g, url_for, \
render_template, flash, redirect
from basic_auth_edited import BasicAuth
from database import Database
# import numpy as np
# from helpers import fade_colors
import sys,os
app = Flask(__name__)
app.config.from_envvar('LIGHT_CONTROLS_SETTINGS', silent=True)
db = Database()
# password protected info for lights_control
## TODO: salt and hash password/username
# pi1 = pigpio.pi()
with open('lights_login.csv', 'r') as pass_file:
login_info = pass_file.read()
login_info = login_info.split(',')
username = login_info[0]
password = login_info[1]
app.config['BASIC_AUTH_USERNAME'] = username
app.config['BASIC_AUTH_PASSWORD'] = password
basic_auth = BasicAuth(app, failed_login='failed_login.html')
# don't cache css
@app.after_request
def add_header(r):
"""
Add headers to both force latest IE rendering engine or Chrome Frame,
and also to cache the rendered page for 10 minutes.
"""
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
r.headers['Cache-Control'] = 'public, max-age=0'
return r
@app.route('/')
def index():
return render_template('home.html')
@app.route('/climbing')
def climbing():
return render_template('climbing.html')
@app.route('/contact')
def contact():
return render_template('contact.html')
@app.route('/wildlife')
def wildlife():
return render_template('wildlife.html')
@app.route('/handler')
def handler():
return render_template('handler.html')
@app.route('/table')
def table():
return render_template('table.html')
@app.route('/website')
def website():
return render_template('website.html')
@app.route('/auto_app')
def auto_app():
return render_template('auto_app.html')
# a page with a description etc.
@app.route('/compare_grades')
def compare_grades():
return render_template('compare_grades.html')
# the full html page.
@app.route('/compare_grades_full')
def compare_grades_full():
return render_template('compare_grades_full.html')
# a page with a description etc.
@app.route('/genetic_art')
def genetic_art():
return render_template('genetic_art.html')
@app.route('/baking', methods=['GET', 'POST'])
def baking():
food_dict = db.query_foods()
if request.method == 'POST':
if 'Add a Food' == request.form['baking_button']:
return redirect(url_for('add_food'))
else:
food_id = request.form['baking_button']
return redirect(url_for('view_food',
food_id=food_id))
return render_template('baking.html', foods=food_dict)
@app.route('/view_food', methods=['GET', 'POST'])
def view_food():
if request.method == 'POST':
selection = list(request.form.items())[0]
if selection[0] == 'add_recipe':
return redirect(url_for('add_recipe', food_id=request.form.get('food_id')))
return redirect(url_for('view_recipe',
recipe_id=request.form['recipe_selector']))
food_id = request.args.get('food_id')
recipes = db.query_recipes(food_id)
food = db.food_name_from_id(food_id)
return render_template('view_food.html', food=food, recipes=recipes, food_id=food_id)
@app.route('/view_recipe', methods=['GET', 'POST'])
def view_recipe():
if request.method == 'POST':
return redirect(url_for('add_review', recipe_id=request.form.get('recipe_id')))
recipe_id = request.args.get('recipe_id')
recipe, reviews = db.query_reviews(recipe_id)
recipe = [str(line) for line in recipe[0][2:]]
reviews = [" | ".join([str(info1) for info1 in info]) for info in [review[2:] for review in reviews]]
return render_template('view_ratings.html', recipe=recipe, reviews=reviews, recipe_id=recipe_id)
@app.route('/add_food', methods=['GET', 'POST'])
def add_food():
if request.method == 'POST':
food_id = db.add_food(name=request.form['food_name'])
return redirect(url_for('view_food', food_id=food_id))
return render_template('add_food.html')
@app.route('/add_recipe', methods=['GET', 'POST'])
def add_recipe():
if request.method == "POST":
recipe = request.form['recipe_name']
recipe_id = db.add_recipe(food_id=request.args.get('food_id'),
recipe_name=recipe,
directions=request.form['directions'],
change=request.form['recipe_change'],
notes=request.form['notes'],
ingredients=request.form['ingredients'])
return redirect(url_for('view_recipe', recipe_id=recipe_id, food=request.args.get('food')))
return render_template('add_recipe.html')
@app.route('/add_review', methods=['GET', 'POST'])
def add_review():
if request.method == 'POST':
form = request.form
db.add_review(recipe_id=request.args.get('recipe_id'),
reviewer_name=form['reviewer_name'],
taste=form['taste'],
texture=form['texture'],
appearance=form['appearance'],
overall=form['overall'],
comments=form['comments'])
return redirect(url_for('view_recipe', recipe_id=request.args.get('recipe_id')))
return render_template('add_review.html', recipe_id=request.args.get('recipe_id'))
# this will never be used. I Could just redirect all traffic to the example page, but want to keep it as it's
# an ok example of authentication
@app.route('/controls', methods=['GET', 'POST'])
@basic_auth.required
def light_controls():
# safely try to get the current values of the GPIO Pins. If multiple people
# are controlling the table, it pulls the current value every time they
# load it. I could move this into the html so people get live updates
## TODO: move into html for live update.
r = 0
g = 0
b = 0
# try:
# r = pi1.get_PWM_dutycycle(17)
# g = pi1.get_PWM_dutycycle(27)
# b = pi1.get_PWM_dutycycle(22)
# except AttributeError:
# r = 0
# g = 0
# b = 0
# except:
# print(Exception)
#
# if request.method == 'POST':
# try:
# new_colors = np.array([request.form['red'], request.form['green'],
# request.form['blue']])
# new_colors = new_colors.astype('int')
# assert (np.all(new_colors < 256) and np.all(new_colors >= 0))
# fade_colors(pi1, np.array([r, g, b]), new_colors, [17, 27, 22])
# r = pi1.get_PWM_dutycycle(17)
# g = pi1.get_PWM_dutycycle(27)
# b = pi1.get_PWM_dutycycle(22)
# except (ValueError, AssertionError):
# flash('You need to type a value between 0 and 255 for all boxes')
return render_template('light_controls.html', r_value=r, g_value=g,
b_value=b)
if __name__ == '__main__':
app.run(threaded=True, port=80, host='0.0.0.0')