Compare commits
2 Commits
03eec7bd4a
...
eb8badd3ae
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eb8badd3ae | ||
|
|
1131995385 |
30
app.py
30
app.py
@ -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 models.Habit import Habit
|
||||
from models.HabitTrackings import HabitTrackings
|
||||
from models.User import User
|
||||
from utils import anonymous_required
|
||||
|
||||
@ -223,8 +224,33 @@ def habit_create():
|
||||
@app.route('/check', methods=['POST'])
|
||||
@login_required
|
||||
def check_habit():
|
||||
habit = request.get_json()["habitId"]
|
||||
return {}
|
||||
habit_id = request.get_json()["habitId"]
|
||||
|
||||
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
|
||||
|
||||
@ -140,6 +140,16 @@ def get_habitTrackings(id: int):
|
||||
return habit_tracking
|
||||
|
||||
|
||||
def get_habitTrackings_by_habit_id(habit_id: int):
|
||||
query = f"SELECT * FROM habit_trackings WHERE habit_id = {habit_id};"
|
||||
conn = con3()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(query)
|
||||
habit_trackings = cursor.fetchall()
|
||||
conn.close()
|
||||
return habit_trackings
|
||||
|
||||
|
||||
def delete_habitTrackings(id: int):
|
||||
query = f"DELETE FROM habit_trackings WHERE id = {id};"
|
||||
conn = con3()
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
from dataclasses import dataclass
|
||||
from db.SQLiteClient import create_habit, get_habit, delete_habit, get_next_slot
|
||||
from db.SQLiteClient import create_habit, get_habit, delete_habit, get_next_slot, get_habitTrackings_by_habit_id
|
||||
from models.HabitTrackings import HabitTrackings
|
||||
from models.User import User
|
||||
|
||||
|
||||
# Unit wird als Integers wie folgt gemessen:
|
||||
@ -32,3 +34,9 @@ class Habit:
|
||||
@staticmethod
|
||||
def delete(id: int):
|
||||
delete_habit(id)
|
||||
|
||||
def get_user(self):
|
||||
return User.get(self.user_id)
|
||||
|
||||
def get_habitTrackings(self) -> list[HabitTrackings]:
|
||||
return get_habitTrackings_by_habit_id(self.id)
|
||||
|
||||
@ -18,6 +18,5 @@ class HabitTrackings:
|
||||
habitTrackings = get_habitTrackings(id)
|
||||
return HabitTrackings(habitTrackings[0], habitTrackings[1], habitTrackings[2]) if habitTrackings else None
|
||||
|
||||
@staticmethod
|
||||
def delete(id: int):
|
||||
delete_habitTrackings(id)
|
||||
def delete(self):
|
||||
delete_habitTrackings(self.id)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user