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):
|
||||
|
||||
19
app/templates/partials/checkin_edit_row.html
Normal file
19
app/templates/partials/checkin_edit_row.html
Normal file
@@ -0,0 +1,19 @@
|
||||
<tr id="checkin-{{ c.id }}" class="editing-row">
|
||||
<td>{{ c.checked_in_at | sydney }}</td>
|
||||
<td>
|
||||
<input type="number" name="weight_kg" class="edit-input" step="0.1" value="{{ '%.1f' % (c.weight_kg | float) }}"
|
||||
required autofocus>
|
||||
</td>
|
||||
<td style="color: var(--text-muted); font-size: 0.8rem;">auto</td>
|
||||
<td>
|
||||
<input type="text" name="notes" class="edit-input" placeholder="Notes" value="{{ c.notes or '' }}">
|
||||
</td>
|
||||
<td>
|
||||
<div class="checkin-actions">
|
||||
<button class="btn-icon btn-icon-success" hx-put="/checkin/{{ c.id }}" hx-target="#checkin-{{ c.id }}"
|
||||
hx-swap="outerHTML" hx-include="closest tr" title="Save">✓</button>
|
||||
<button class="btn-icon" hx-get="/checkin/{{ c.id }}/view" hx-target="#checkin-{{ c.id }}"
|
||||
hx-swap="outerHTML" title="Cancel">✕</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
@@ -4,7 +4,11 @@
|
||||
<td>{{ '%.1f' % (c.bmi | float) if c.bmi else '—' }}</td>
|
||||
<td>{{ c.notes or '—' }}</td>
|
||||
<td>
|
||||
<div class="checkin-actions">
|
||||
<button class="btn-icon" hx-get="/checkin/{{ c.id }}/edit" hx-target="#checkin-{{ c.id }}"
|
||||
hx-swap="outerHTML" title="Edit">✏️</button>
|
||||
<button class="btn-icon" hx-delete="/checkin/{{ c.id }}" hx-target="#checkin-{{ c.id }}" hx-swap="outerHTML"
|
||||
hx-confirm="Delete this check-in?">🗑️</button>
|
||||
hx-confirm="Delete this check-in?" title="Delete">🗑️</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
Reference in New Issue
Block a user