-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
183 lines (117 loc) · 4.59 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
from flask import Flask, render_template, url_for, request, redirect
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///fooddatabase.db'
app.config['SQLALCHEMY_BINDS'] = {'goal':'sqlite:///goaldatabase.db'}
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
food = db.Column(db.String(100))
calories = db.Column(db.Integer)
fat = db.Column(db.Integer)
protein = db.Column(db.Integer)
carbs = db.Column(db.Integer)
date_created = db.Column(db.DateTime, default=datetime.utcnow)
class Goal(db.Model):
__bind_key__ = 'goal'
id = db.Column(db.Integer, primary_key=True)
calorie_goal = db.Column(db.Integer)
fat_goal = db.Column(db.Integer)
protein_goal = db.Column(db.Integer)
carbs_goal = db.Column(db.Integer)
@app.route('/', methods=['POST', 'GET'])
def index():
user_calories_goal = 0
user_f_goal = 0
user_p_goal = 0
user_carbs_goal = 0
if request.method == "POST":
if 'food-log' in request.form:
food_content = request.form['food']
calories_content = request.form['calories']
fat_content = request.form['fat']
protein_content = request.form['protein']
carbs_content = request.form['carbs']
new_user = User(food=food_content, calories=calories_content, fat=fat_content, protein=protein_content, carbs=carbs_content)
try:
db.session.add(new_user)
db.session.commit()
return redirect('/')
except:
return "There was an issue !"
else:
user_calories_goal = request.form['calorie-goal']
user_f_goal = request.form['fat-goal']
user_p_goal = request.form['protein-goal']
user_carbs_goal = request.form['carbs-goal']
new_goal = Goal(calorie_goal=user_calories_goal, fat_goal=user_f_goal, protein_goal=user_p_goal, carbs_goal=user_carbs_goal)
try:
db.session.add(new_goal)
db.session.commit()
return redirect('/')
except:
return "There was an issue !"
else:
items = User.query.order_by(User.date_created).all()
goals = Goal.query.all()
ccal = 0
cf = 0
cp = 0
cc = 0
if not items:
print('list empty')
else:
for i in items:
ccal += float(i.calories)
cf += float(i.fat)
cp += float(i.protein)
cc += float(i.carbs)
if not goals:
goals.append(Goal(calorie_goal=user_calories_goal, fat_goal=user_f_goal, protein_goal=user_p_goal, carbs_goal=user_carbs_goal))
return render_template('index.html', items = items, goals = goals[-1], ccal = ccal, cf = cf, cp = cp, cc = cc)
@app.route('/reset')
def reset():
items = User.query.order_by(User.date_created).all()
goals = Goal.query.all()
for i in range(1, len(items)+1):
try:
db.session.delete(User.query.get_or_404(i))
db.session.commit()
except:
return "Error in resetting"
for i in range(1, len(goals)+1):
try:
db.session.delete(Goal.query.get_or_404(i))
db.session.commit()
except:
return "Error in resetting"
return redirect('/')
@app.route('/delete/<int:id>')
def delete(id):
delete_item = User.query.get_or_404(id)
try:
db.session.delete(delete_item)
db.session.commit()
return redirect('/')
except:
return "Sorry an error has occured"
@app.route('/update/<int:id>', methods=['GET', 'POST'])
def update(id):
item = User.query.get_or_404(id)
if request.method =='POST':
item.food = request.form['food']
item.calories = request.form['calories']
item.fat = request.form['fat']
item.protein = request.form['protein']
item.carbs = request.form['carbs']
try:
db.session.commit()
return redirect('/')
except:
return "an issue occured"
else:
return render_template('update.html', item=item)
if __name__ == '__main__':
app.run(debug=True)