Allow users to edit checkins
This commit is contained in:
@@ -99,6 +99,72 @@ def create():
|
||||
return redirect(url_for("checkin.index"))
|
||||
|
||||
|
||||
@bp.route("/checkin/<int:checkin_id>/edit", methods=["GET"])
|
||||
@login_required
|
||||
def edit_form(checkin_id):
|
||||
user = get_current_user()
|
||||
checkin = query_one(
|
||||
"SELECT * FROM checkins WHERE id = %s AND user_id = %s",
|
||||
(checkin_id, user["id"]),
|
||||
)
|
||||
if not checkin:
|
||||
return "", 404
|
||||
return render_template("partials/checkin_edit_row.html", c=checkin, user=user)
|
||||
|
||||
|
||||
@bp.route("/checkin/<int:checkin_id>/view", methods=["GET"])
|
||||
@login_required
|
||||
def view_row(checkin_id):
|
||||
user = get_current_user()
|
||||
checkin = query_one(
|
||||
"SELECT * FROM checkins WHERE id = %s AND user_id = %s",
|
||||
(checkin_id, user["id"]),
|
||||
)
|
||||
if not checkin:
|
||||
return "", 404
|
||||
return render_template("partials/checkin_row.html", c=checkin, user=user)
|
||||
|
||||
|
||||
@bp.route("/checkin/<int:checkin_id>", methods=["PUT"])
|
||||
@login_required
|
||||
def update(checkin_id):
|
||||
user = get_current_user()
|
||||
weight_kg = request.form.get("weight_kg")
|
||||
notes = request.form.get("notes", "").strip()
|
||||
|
||||
checkin = query_one(
|
||||
"SELECT * FROM checkins WHERE id = %s AND user_id = %s",
|
||||
(checkin_id, user["id"]),
|
||||
)
|
||||
if not checkin:
|
||||
return "", 404
|
||||
|
||||
if not weight_kg:
|
||||
flash("Weight is required.", "error")
|
||||
return render_template("partials/checkin_edit_row.html", c=checkin, user=user)
|
||||
|
||||
try:
|
||||
weight_kg = float(weight_kg)
|
||||
except ValueError:
|
||||
flash("Invalid weight value.", "error")
|
||||
return render_template("partials/checkin_edit_row.html", c=checkin, user=user)
|
||||
|
||||
bmi = calculate_bmi(weight_kg, user.get("height_cm"))
|
||||
|
||||
execute(
|
||||
"""UPDATE checkins SET weight_kg = %s, bmi = %s, notes = %s
|
||||
WHERE id = %s AND user_id = %s""",
|
||||
(weight_kg, bmi, notes or None, checkin_id, user["id"]),
|
||||
)
|
||||
|
||||
check_milestones(user["id"], user)
|
||||
|
||||
updated = query_one(
|
||||
"SELECT * FROM checkins WHERE id = %s", (checkin_id,)
|
||||
)
|
||||
return render_template("partials/checkin_row.html", c=updated, user=user)
|
||||
|
||||
|
||||
@bp.route("/checkin/<int:checkin_id>", methods=["DELETE"])
|
||||
@login_required
|
||||
def delete(checkin_id):
|
||||
|
||||
Reference in New Issue
Block a user