mirror of
				https://github.com/StefBuwalda/cal_counter.git
				synced 2025-10-29 19:00:00 +00:00 
			
		
		
		
	Refactor food item flow and barcode handling
Updated routes and templates to improve the process of adding and logging food items by barcode. The add_food_item route now accepts a barcode parameter, and barcode lookups redirect to item creation if not found. The log_food flow now uses session variables for item and meal selection, and the get_item.html template uses fetch to handle barcode lookups and redirects accordingly.
This commit is contained in:
		| @@ -59,9 +59,9 @@ fields = [ | ||||
| ] | ||||
|  | ||||
|  | ||||
| @user_bp.route("/add_food_item", methods=["POST"]) | ||||
| def add_food_item(): | ||||
|     form = FoodItemForm() | ||||
| @user_bp.route("/add_food_item/<string:barcode>", methods=["GET", "POST"]) | ||||
| def add_food_item(barcode): | ||||
|     form = FoodItemForm(barcode=barcode) | ||||
|  | ||||
|     if form.validate_on_submit(): | ||||
|         print("[DEBUG] Valid form") | ||||
| @@ -92,9 +92,9 @@ def add_food_item(): | ||||
|     else: | ||||
|         print("[DEBUG] Invalid form") | ||||
|     if form.barcode.data: | ||||
|         return redirect(url_for("user.food_item", barcode=form.barcode.data)) | ||||
|         return render_template("add_food_item.html", form=form) | ||||
|     else: | ||||
|         return redirect(url_for("scan")) | ||||
|         return redirect("/") | ||||
|  | ||||
|  | ||||
| @user_bp.route("/edit_food_item/<int:id>", methods=["GET", "POST"]) | ||||
| @@ -119,7 +119,7 @@ def edit_food_item(id: int): | ||||
|     return redirect(url_for("user.dashboard")) | ||||
|  | ||||
|  | ||||
| @user_bp.route("/food_item/<int:barcode>", methods=["GET"]) | ||||
| @user_bp.route("/food_item/<string:barcode>", methods=["GET"]) | ||||
| def food_item(barcode): | ||||
|     food = FoodItem.query.filter_by(barcode=barcode).first() | ||||
|     if food: | ||||
| @@ -161,24 +161,27 @@ def add_food_item_manual(): | ||||
|     return render_template("add_food_item_manual.html", form=form) | ||||
|  | ||||
|  | ||||
| @user_bp.route("/log_food/<int:item_id>", methods=["GET", "POST"]) | ||||
| def log_food(item_id): | ||||
| @user_bp.route("/log_food", methods=["GET", "POST"]) | ||||
| def log_food(): | ||||
|     form = FoodLogForm() | ||||
|     if item_id is not None: | ||||
|         if db.session.get(FoodItem, item_id): | ||||
|             if form.validate_on_submit(): | ||||
|                 assert form.amount.data is not None | ||||
|                 db.session.add( | ||||
|                     FoodLog( | ||||
|                         item_id, | ||||
|                         current_user.id, | ||||
|                         form.amount.data, | ||||
|                         part_of_day=0, | ||||
|                     ) | ||||
|     item_id = session["item_id"] | ||||
|     meal_type = session["meal_type"] | ||||
|     if item_id is None or meal_type is None: | ||||
|         return redirect("/") | ||||
|     if db.session.get(FoodItem, item_id): | ||||
|         if form.validate_on_submit(): | ||||
|             assert form.amount.data is not None | ||||
|             db.session.add( | ||||
|                 FoodLog( | ||||
|                     item_id, | ||||
|                     current_user.id, | ||||
|                     form.amount.data, | ||||
|                     part_of_day=meal_type, | ||||
|                 ) | ||||
|                 db.session.commit() | ||||
|                 return redirect(url_for("user.dashboard")) | ||||
|     return render_template("log_food.html", item_id=item_id, form=form) | ||||
|             ) | ||||
|             db.session.commit() | ||||
|             return redirect(url_for("user.dashboard")) | ||||
|     return render_template("log_food.html", form=form) | ||||
|  | ||||
|  | ||||
| @user_bp.route("/overview", methods=["GET"]) | ||||
| @@ -193,7 +196,14 @@ def select_meal(meal_type: int): | ||||
|     return redirect(url_for("user.scan_product")) | ||||
|  | ||||
|  | ||||
| @user_bp.route("/get_userid", methods=["GET"]) | ||||
| @user_bp.route("/select_item/<int:item_id>", methods=["GET"]) | ||||
| def select_item(item_id: int): | ||||
|     assert type(item_id) is int | ||||
|     session["item_id"] = item_id | ||||
|     return redirect(url_for("user.log_food")) | ||||
|  | ||||
|  | ||||
| @user_bp.route("/get_foodid", methods=["GET"]) | ||||
| def scan_product(): | ||||
|     return render_template("get_item.html") | ||||
|  | ||||
| @@ -221,9 +231,13 @@ def test(): | ||||
|  | ||||
| @user_bp.route("/foodId_from_barcode/<string:barcode>", methods=["GET"]) | ||||
| def foodId_from_barcode(barcode: str): | ||||
|     if barcode.isdigit(): | ||||
|         return jsonify({"food_id": barcode}) | ||||
|     else: | ||||
|     # Check if barcode contains only digits | ||||
|     if not barcode.isdigit(): | ||||
|         return abort( | ||||
|             400, description="Invalid barcode: must contain only digits" | ||||
|         ) | ||||
|  | ||||
|     item = current_user.food_items.filter_by(barcode=barcode).first() | ||||
|     if item is None: | ||||
|         return redirect(url_for("user.add_food_item", barcode=barcode)) | ||||
|     return jsonify({"item_id": item.id}) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user