-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
120 lines (87 loc) · 3.91 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
from flask import Flask, render_template, g, request
from datetime import datetime
from data.database import get_db
app = Flask(__name__)
@app.teardown_appcontext
def close_db(error):
if hasattr(g, 'sqlite_db'):
g.sqlite_db.close()
def pretty_date(date):
dt = datetime.strptime(str(date), '%Y%m%d')
return datetime.strftime(dt, '%B %d, %Y')
@app.route('/', methods=['GET', 'POST'])
def index():
db = get_db()
if request.method == 'POST':
# assuming the date is in YYYY/MM/DD format
date = request.form['date']
dt = datetime.strptime(date, '%Y-%m-%d')
database_date = datetime.strftime(dt, '%Y%m%d')
db.execute('INSERT INTO log_date (entry_date) values (?)',
[database_date])
db.commit()
cur = db.execute("""SELECT log_date.entry_date, sum(protein) as protein, sum(carbohydrates) as carbohydrates, sum(fat) as fat, sum(calories) as calories
FROM log_date
LEFT JOIN food_dates ON food_dates.log_date_id = log_date.id
LEFT JOIN food ON food.id = food_dates.food_id
GROUP BY log_date.id ORDER BY entry_date DESC""")
results = cur.fetchall()
date_results = []
for i in results:
single_date = {}
single_date['entry_date'] = i['entry_date']
single_date['protein'] = i['protein']
single_date['carbohydrates'] = i['carbohydrates']
single_date['fat'] = i['fat']
single_date['calories'] = i['calories']
single_date['pretty_date'] = pretty_date(i['entry_date'])
date_results.append(single_date)
return render_template('home.html', results=date_results)
# date is going to be 20170214
@app.route('/view/<date>', methods=['GET', 'POST'])
def view(date):
db = get_db()
cur = db.execute(
'SELECT id, entry_date FROM log_date WHERE entry_date = ?', [date])
date_result = cur.fetchone()
if request.method == 'POST':
db.execute('INSERT INTO food_dates values (?,?)', [
request.form['food-select'], date_result['id']])
db.commit()
cur = db.execute('SELECT id, name FROM food')
food_results = cur.fetchall()
cur = db.execute("""SELECT name, protein, carbohydrates, fat, calories, entry_date
FROM log_date
JOIN food_dates ON food_dates.log_date_id = log_date.id
JOIN food ON food.id = food_dates.food_id
WHERE entry_date = ?""", [date])
food_dates_results = cur.fetchall()
totals = {'protein' : 0, 'carbohydrates' : 0, 'fat' : 0, 'calories' : 0}
for food in food_dates_results:
totals['protein'] += food['protein']
totals['carbohydrates'] += food['carbohydrates']
totals['fat'] += food['fat']
totals['calories'] += food['calories']
return render_template('day.html',
date={'pretty':pretty_date(date_result['entry_date']),'entry':date},
food_results=food_results,
food_dates_results=food_dates_results,
totals=totals)
@app.route('/food', methods=['GET', 'POST'])
def food():
db = get_db()
if request.method == 'POST':
name = request.form["name"]
protein = int(request.form["protein"])
carbohydrates = int(request.form["carbohydrates"])
fat = int(request.form["fat"])
calories = protein * 4 + carbohydrates * 4 + fat * 9
db.execute('insert into food (name, protein, carbohydrates, fat, calories) values (?,?,?,?,?)',
[name, protein, carbohydrates, fat, calories])
db.commit()
cur = db.execute(
'SELECT name, protein, carbohydrates, fat, calories FROM food')
results = cur.fetchall()
return render_template('add_food.html', results=results)
if __name__ == '__main__':
app.run(debug=True)