mirror of
				https://github.com/StefBuwalda/cal_counter.git
				synced 2025-10-30 11:19:59 +00:00 
			
		
		
		
	Add daily overview and part_of_day to food logs
Introduces 'overview' and 'test' routes and templates for daily nutrition summaries. Updates FoodLog model to include 'part_of_day' and 'date_' fields for better log categorization. Adjusts log creation and seed data to support new fields. Removes SSL context from app run for local development.
This commit is contained in:
		
							
								
								
									
										3
									
								
								app.py
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								app.py
									
									
									
									
									
								
							| @@ -81,4 +81,5 @@ def scan(): | |||||||
| # Run | # Run | ||||||
|  |  | ||||||
| if __name__ == "__main__": | if __name__ == "__main__": | ||||||
|     app.run(host="0.0.0.0", debug=True, ssl_context=("cert.pem", "key.pem")) |     # app.run(host="0.0.0.0", debug=True, ssl_context=("cert.pem", "key.pem")) | ||||||
|  |     app.run(host="0.0.0.0", debug=True) | ||||||
|   | |||||||
| @@ -3,6 +3,7 @@ from flask_login import current_user | |||||||
| from application import db | from application import db | ||||||
| from forms import FoodItemForm, FoodLogForm | from forms import FoodItemForm, FoodLogForm | ||||||
| from models import FoodItem, FoodLog | from models import FoodItem, FoodLog | ||||||
|  | from datetime import datetime, timezone | ||||||
|  |  | ||||||
| user_bp = Blueprint( | user_bp = Blueprint( | ||||||
|     "user", |     "user", | ||||||
| @@ -163,8 +164,27 @@ def log_food(item_id): | |||||||
|                         item_id, |                         item_id, | ||||||
|                         current_user.id, |                         current_user.id, | ||||||
|                         form.amount.data, |                         form.amount.data, | ||||||
|  |                         part_of_day=0, | ||||||
|                     ) |                     ) | ||||||
|                 ) |                 ) | ||||||
|                 db.session.commit() |                 db.session.commit() | ||||||
|                 return redirect(url_for("user.dashboard")) |                 return redirect(url_for("user.dashboard")) | ||||||
|     return render_template("log_food.html", item_id=item_id, form=form) |     return render_template("log_food.html", item_id=item_id, form=form) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @user_bp.route("/overview", methods=["GET"]) | ||||||
|  | def overview(): | ||||||
|  |     return render_template("overview.html") | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @user_bp.route("/", methods=["GET"]) | ||||||
|  | def test(): | ||||||
|  |     today = datetime.now(timezone.utc).date() | ||||||
|  |     logs_today = current_user.food_logs.filter_by(date_=today).all() | ||||||
|  |     logs = {0: [], 1: [], 2: [], 3: []} | ||||||
|  |     for log in logs_today: | ||||||
|  |         logs[log.part_of_day].append(log) | ||||||
|  |     print(logs) | ||||||
|  |     return render_template( | ||||||
|  |         "test.html", date=(today.strftime("%d/%m/%y")), logs=logs | ||||||
|  |     ) | ||||||
|   | |||||||
							
								
								
									
										69
									
								
								application/user/templates/overview.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										69
									
								
								application/user/templates/overview.html
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,69 @@ | |||||||
|  | {% extends "base.html" %} | ||||||
|  |  | ||||||
|  | {% block title %} | ||||||
|  | Food Nutritional Info | ||||||
|  | {% endblock %} | ||||||
|  |  | ||||||
|  | {% block content %} | ||||||
|  |  | ||||||
|  | <div class="container my-4"> | ||||||
|  |     <div class="d-flex justify-content-center mb-4"> | ||||||
|  |         <h1>Daily Nutrition Overview</h1> | ||||||
|  |     </div> | ||||||
|  |  | ||||||
|  |     <div class="row row-cols-1 row-cols-md-2 row-cols-lg-4 g-4"> | ||||||
|  |         <!-- Calories --> | ||||||
|  |         <div class="col"> | ||||||
|  |             <div class="card text-bg-primary h-100"> | ||||||
|  |                 <div class="card-body"> | ||||||
|  |                     <h5 class="card-title">Calories</h5> | ||||||
|  |                     <h2>1,850 / 2,000 kcal</h2> | ||||||
|  |                     <div class="progress mt-3" style="height: 10px;"> | ||||||
|  |                         <div class="progress-bar bg-warning" style="width: 92%;"></div> | ||||||
|  |                     </div> | ||||||
|  |                 </div> | ||||||
|  |             </div> | ||||||
|  |         </div> | ||||||
|  |  | ||||||
|  |         <!-- Protein --> | ||||||
|  |         <div class="col"> | ||||||
|  |             <div class="card text-bg-success h-100"> | ||||||
|  |                 <div class="card-body"> | ||||||
|  |                     <h5 class="card-title">Protein</h5> | ||||||
|  |                     <h2>120g / 150g</h2> | ||||||
|  |                     <div class="progress mt-3" style="height: 10px;"> | ||||||
|  |                         <div class="progress-bar bg-light" style="width: 80%;"></div> | ||||||
|  |                     </div> | ||||||
|  |                 </div> | ||||||
|  |             </div> | ||||||
|  |         </div> | ||||||
|  |  | ||||||
|  |         <!-- Carbs --> | ||||||
|  |         <div class="col"> | ||||||
|  |             <div class="card text-bg-info h-100"> | ||||||
|  |                 <div class="card-body"> | ||||||
|  |                     <h5 class="card-title">Carbs</h5> | ||||||
|  |                     <h2>200g / 250g</h2> | ||||||
|  |                     <div class="progress mt-3" style="height: 10px;"> | ||||||
|  |                         <div class="progress-bar bg-dark" style="width: 80%;"></div> | ||||||
|  |                     </div> | ||||||
|  |                 </div> | ||||||
|  |             </div> | ||||||
|  |         </div> | ||||||
|  |  | ||||||
|  |         <!-- Fat --> | ||||||
|  |         <div class="col"> | ||||||
|  |             <div class="card text-bg-danger h-100"> | ||||||
|  |                 <div class="card-body"> | ||||||
|  |                     <h5 class="card-title">Fat</h5> | ||||||
|  |                     <h2>60g / 70g</h2> | ||||||
|  |                     <div class="progress mt-3" style="height: 10px;"> | ||||||
|  |                         <div class="progress-bar bg-light" style="width: 86%;"></div> | ||||||
|  |                     </div> | ||||||
|  |                 </div> | ||||||
|  |             </div> | ||||||
|  |         </div> | ||||||
|  |     </div> | ||||||
|  | </div> | ||||||
|  |  | ||||||
|  | {% endblock%} | ||||||
							
								
								
									
										69
									
								
								application/user/templates/test.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										69
									
								
								application/user/templates/test.html
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,69 @@ | |||||||
|  | {% extends "base.html" %} | ||||||
|  |  | ||||||
|  | {% block title %} | ||||||
|  | Food Nutritional Info | ||||||
|  | {% endblock %} | ||||||
|  |  | ||||||
|  | {% block content %} | ||||||
|  |  | ||||||
|  | <!-- Daily Overview Section --> | ||||||
|  | <div class="container"> | ||||||
|  |  | ||||||
|  |     <div class="mb-4 p-3 border rounded"> | ||||||
|  |         <div class="text-center"> | ||||||
|  |             <h2>Daily Overview ({{date}})</h2> | ||||||
|  |             <p>Summary info here...</p> | ||||||
|  |         </div> | ||||||
|  |  | ||||||
|  |     </div> | ||||||
|  |  | ||||||
|  |     <div class="p-3 border rounded"> | ||||||
|  |         <div class="text-center"> | ||||||
|  |             <h2>Detailed Information</h2> | ||||||
|  |             <p>More content here...</p> | ||||||
|  |         </div> | ||||||
|  |         <div class="p-3 mb-2 border rounded"> | ||||||
|  |             <h4>Breakfast</h4> | ||||||
|  |             {% for log in logs[0] %} | ||||||
|  |             <p>{{log.food_item.name}}</p> | ||||||
|  |             {% endfor %} | ||||||
|  |         </div> | ||||||
|  |         <div class="p-3 mb-2 border rounded"> | ||||||
|  |             <h4>Lunch</h4> | ||||||
|  |             {% for log in logs[1] %} | ||||||
|  |             <p>{{log.food_item.name}}</p> | ||||||
|  |             {% endfor %} | ||||||
|  |         </div> | ||||||
|  |         <div class="p-3 mb-2 border rounded"> | ||||||
|  |             <h4>Dinner</h4> | ||||||
|  |             {% for log in logs[2] %} | ||||||
|  |             <p>{{log.food_item.name}}</p> | ||||||
|  |             {% endfor %} | ||||||
|  |         </div> | ||||||
|  |         <div class="p-3 mb-2 border rounded"> | ||||||
|  |             <h4>Snacks</h4> | ||||||
|  |             {% for log in logs[3] %} | ||||||
|  |             <p>{{log.food_item.name}}</p> | ||||||
|  |             {% endfor %} | ||||||
|  |         </div> | ||||||
|  |     </div> | ||||||
|  |  | ||||||
|  | </div> | ||||||
|  |  | ||||||
|  |  | ||||||
|  | <div class="container-fluid my-4"> | ||||||
|  |     <div class="d-flex justify-content-center"> | ||||||
|  |         <h1>Overview {{date}}</h1> | ||||||
|  |     </div> | ||||||
|  |     <div class="d-flex justify-content-center"> | ||||||
|  |         <div class="mx-2">ENERGY</div> | ||||||
|  |         <div class="mx-2">PROTEIN</div> | ||||||
|  |         <div class="mx-2">CARBS</div> | ||||||
|  |         <div class="mx-2">FATS</div> | ||||||
|  |     </div> | ||||||
|  |     <div class="m-5"></div> | ||||||
|  |     <div class="d-flex justify-content-center"> | ||||||
|  |         <div> Breakfast </div> | ||||||
|  |     </div> | ||||||
|  | </div> | ||||||
|  | {% endblock%} | ||||||
							
								
								
									
										20
									
								
								models.py
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								models.py
									
									
									
									
									
								
							| @@ -3,7 +3,7 @@ from werkzeug.security import generate_password_hash, check_password_hash | |||||||
| from application import db | from application import db | ||||||
| from typing import Optional | from typing import Optional | ||||||
| from forms import FoodItemForm | from forms import FoodItemForm | ||||||
| from datetime import datetime, timezone | from datetime import datetime, timezone, date | ||||||
|  |  | ||||||
|  |  | ||||||
| class User(UserMixin, db.Model): | class User(UserMixin, db.Model): | ||||||
| @@ -107,17 +107,31 @@ class FoodItem(db.Model): | |||||||
| class FoodLog(db.Model): | class FoodLog(db.Model): | ||||||
|     __tablename__ = "food_log" |     __tablename__ = "food_log" | ||||||
|     id = db.Column(db.Integer, primary_key=True) |     id = db.Column(db.Integer, primary_key=True) | ||||||
|     datetime = db.Column( |     datetime_created = db.Column( | ||||||
|         db.DateTime, default=datetime.now(timezone.utc), nullable=False |         db.DateTime, default=datetime.now(timezone.utc), nullable=False | ||||||
|     ) |     ) | ||||||
|  |     date_ = db.Column( | ||||||
|  |         db.Date, default=datetime.now(timezone.utc).date, nullable=False | ||||||
|  |     ) | ||||||
|     food_item_id = db.Column( |     food_item_id = db.Column( | ||||||
|         db.Integer, db.ForeignKey("food_item.id"), nullable=False |         db.Integer, db.ForeignKey("food_item.id"), nullable=False | ||||||
|     ) |     ) | ||||||
|  |     part_of_day = db.Column(db.Integer, nullable=False) | ||||||
|     user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False) |     user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False) | ||||||
|     amount = db.Column(db.Integer, nullable=False) |     amount = db.Column(db.Integer, nullable=False) | ||||||
|  |  | ||||||
|     def __init__(self, food_item_id: int, user_id: int, amount: int): |     def __init__( | ||||||
|  |         self, | ||||||
|  |         food_item_id: int, | ||||||
|  |         user_id: int, | ||||||
|  |         amount: int, | ||||||
|  |         part_of_day: int, | ||||||
|  |         date_: Optional[date] = None, | ||||||
|  |     ): | ||||||
|         super().__init__() |         super().__init__() | ||||||
|         self.food_item_id = food_item_id |         self.food_item_id = food_item_id | ||||||
|         self.user_id = user_id |         self.user_id = user_id | ||||||
|         self.amount = amount |         self.amount = amount | ||||||
|  |         if date_ is not None: | ||||||
|  |             self.date_ = date_ | ||||||
|  |         self.part_of_day = part_of_day | ||||||
|   | |||||||
							
								
								
									
										6
									
								
								seed.py
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								seed.py
									
									
									
									
									
								
							| @@ -22,6 +22,10 @@ with app.app_context(): | |||||||
|     ) |     ) | ||||||
|  |  | ||||||
|     FoodLog.query.delete() |     FoodLog.query.delete() | ||||||
|     db.session.add(FoodLog(1, 1, 200)) |     db.session.add(FoodLog(1, 1, 200, 0)) | ||||||
|  |     db.session.add(FoodLog(1, 1, 200, 1)) | ||||||
|  |     db.session.add(FoodLog(1, 1, 200, 2)) | ||||||
|  |     db.session.add(FoodLog(1, 1, 200, 3)) | ||||||
|  |     db.session.add(FoodLog(1, 1, 100, 1)) | ||||||
|  |  | ||||||
|     db.session.commit() |     db.session.commit() | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user