Finished check-Endpoint. Please test

This commit is contained in:
Verox001 2024-01-26 09:06:31 +01:00
parent 03eec7bd4a
commit 1131995385

30
app.py
View File

@ -5,6 +5,7 @@ from flask import Flask, render_template, redirect, url_for, request
from flask_login import login_required, LoginManager, login_user, logout_user, current_user from flask_login import login_required, LoginManager, login_user, logout_user, current_user
from models.Habit import Habit from models.Habit import Habit
from models.HabitTrackings import HabitTrackings
from models.User import User from models.User import User
from utils import anonymous_required from utils import anonymous_required
@ -223,8 +224,33 @@ def habit_create():
@app.route('/check', methods=['POST']) @app.route('/check', methods=['POST'])
@login_required @login_required
def check_habit(): def check_habit():
habit = request.get_json()["habitId"] habit_id = request.get_json()["habitId"]
return {}
habit = Habit.get(habit_id)
if habit is None:
return {"error": "Habit not found"}
# Check if habit belongs to user
if habit.user_id != current_user.id:
return {"error": "Habit does not belong to user"}
trackings = habit.get_habitTrackings()
# Check if habit has been tracked today
unchecked = False
for tracking in trackings:
if tracking.created_at.date() == datetime.date.today():
tracking.delete()
unchecked = True
if not unchecked:
HabitTrackings.create(habit_id, 1)
return {
"habitId": habit_id,
"unchecked": unchecked,
}
# Run the application # Run the application