Small update with bug fixes

This commit is contained in:
Verox 2024-02-12 22:31:51 +01:00
parent 4764112296
commit 05b5869be7
6 changed files with 40 additions and 12 deletions

11
app.py
View File

@ -128,7 +128,8 @@ def index():
name = "Bitte melde dich an." name = "Bitte melde dich an."
# Sort habits by whether they have been checked today and then by slot # Sort habits by whether they have been checked today and then by slot
# habits.sort(key=lambda habit: (habit.checked, habit.slot)) for habit_list in habit_lists:
habit_list.habits = sorted(habit_list.get_habits(), key=lambda habit: (not habit.checked, habit.slot))
return render_template( return render_template(
'index.html', 'index.html',
@ -324,7 +325,8 @@ def check_habit():
return {"error": "Habit not found"} return {"error": "Habit not found"}
# Check if habit belongs to user # Check if habit belongs to user
if habit.user_id != current_user.id: users = habit.habit_list().get_users()
if current_user not in users:
return {"error": "Habit does not belong to user"} return {"error": "Habit does not belong to user"}
trackings = habit.get_habitTrackings() trackings = habit.get_habitTrackings()
@ -360,7 +362,7 @@ def delete_habit():
return {"error": "Habit not found"} return {"error": "Habit not found"}
# Check if habit belongs to user # Check if habit belongs to user
if habit.user_id != current_user.id: if current_user not in habit.habit_list().get_users():
return {"error": "Habit does not belong to user"} return {"error": "Habit does not belong to user"}
habit.delete() habit.delete()
@ -377,7 +379,8 @@ def reorder_habits():
return {"error": "Habit not found"} return {"error": "Habit not found"}
# Check if habit belongs to user # Check if habit belongs to user
if habit.user_id != current_user.id: users = habit.habit_list().get_users()
if current_user not in users:
return {"error": "Habit does not belong to user"} return {"error": "Habit does not belong to user"}
habit.update_slot(new_index) habit.update_slot(new_index)

View File

@ -295,6 +295,16 @@ def delete_habitList(id: int):
conn.close() conn.close()
def get_users(list_id: int):
query = f"SELECT users.* FROM users JOIN habit_users ON users.id = habit_users.user_id WHERE habit_users.list_id = {list_id};"
conn = con3()
cursor = conn.cursor()
cursor.execute(query)
users = cursor.fetchall()
conn.close()
return users
if __name__ == "__main__": if __name__ == "__main__":
habits = get_habits(1) habits = get_habits(1)
for habit in habits: for habit in habits:

View File

@ -4,7 +4,7 @@ from datetime import datetime
from models.HabitTrackings import HabitTrackings from models.HabitTrackings import HabitTrackings
from db.SQLiteClient import update_slot, create_habit, get_habit, delete_habit, get_next_slot, \ from db.SQLiteClient import update_slot, create_habit, get_habit, delete_habit, get_next_slot, \
get_habitTrackings_by_habit_id, get_slots, update_habit get_habitTrackings_by_habit_id, get_slots, update_habit, get_habitList, get_habitLists
# Unit wird als Integers wie folgt gemessen: # Unit wird als Integers wie folgt gemessen:
@ -30,6 +30,7 @@ class Habit:
@staticmethod @staticmethod
def create(list_id: int, name: str, times: int, note: str | None = None, unit: int | None = 1): def create(list_id: int, name: str, times: int, note: str | None = None, unit: int | None = 1):
slot = get_next_slot(list_id) slot = get_next_slot(list_id)
print(slot)
id = create_habit(list_id, name, times, unit, slot, note) id = create_habit(list_id, name, times, unit, slot, note)
return Habit(id, list_id, name, note, times, unit, slot) return Habit(id, list_id, name, note, times, unit, slot)
@ -115,3 +116,9 @@ class Habit:
def to_json(self): def to_json(self):
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4) return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4)
def habit_list(self):
from models.HabitList import HabitList
raw_habitLists = get_habitList(self.list_id)
return HabitList(raw_habitLists[0], raw_habitLists[1], raw_habitLists[2], datetime.strptime(raw_habitLists[3], "%Y-%m-%dT%H:%M:%S.%f"), datetime.strptime(raw_habitLists[4], "%Y-%m-%dT%H:%M:%S.%f")) if raw_habitLists else None

View File

@ -2,8 +2,9 @@ from dataclasses import dataclass
from datetime import date, datetime from datetime import date, datetime
from db.SQLiteClient import create_habitTrackings, get_habitTrackings, delete_habitTrackings, create_habitList, \ from db.SQLiteClient import create_habitTrackings, get_habitTrackings, delete_habitTrackings, create_habitList, \
get_habitList, get_habits get_habitList, get_habits, get_users
from models.Habit import Habit from models.Habit import Habit
from models.User import User
@dataclass @dataclass
@ -13,6 +14,7 @@ class HabitList:
description: str description: str
created_at: date created_at: date
updated_at: date updated_at: date
habits: list = None
@staticmethod @staticmethod
def create(user_id: int, name: str, description: str): def create(user_id: int, name: str, description: str):
@ -35,3 +37,12 @@ class HabitList:
habits.append(habit) habits.append(habit)
return habits return habits
def get_users(self):
raw_users = get_users(self.id)
users = []
for user in raw_users:
user = User(user[0], user[1], user[2], user[3])
users.append(user)
return users

View File

@ -3,8 +3,6 @@ from datetime import datetime
from flask_login import UserMixin from flask_login import UserMixin
from db.SQLiteClient import create_user, get_user, get_user_by_email, get_habits, delete_user, update_user, \ from db.SQLiteClient import create_user, get_user, get_user_by_email, get_habits, delete_user, update_user, \
get_habitLists get_habitLists
from models.Habit import Habit
from models.HabitList import HabitList
class User(UserMixin): class User(UserMixin):
@ -44,6 +42,8 @@ class User(UserMixin):
# return habits # return habits
def get_habitLists(self): def get_habitLists(self):
from models.HabitList import HabitList
raw_habitLists = get_habitLists(self.id) raw_habitLists = get_habitLists(self.id)
habitLists = [] habitLists = []
for habitList in raw_habitLists: for habitList in raw_habitLists:

View File

@ -95,7 +95,7 @@
</div> </div>
<ul class="task-list row"> <ul class="task-list row">
{% for habit in habit_list.get_habits() %} {% for habit in habit_list.habits %}
<li class="row d-flex align-items-center mb-2" id="habit-{{ habit.id }}"> <li class="row d-flex align-items-center mb-2" id="habit-{{ habit.id }}">
<div class="col-auto drag-handle" style="cursor: grab;"> <div class="col-auto drag-handle" style="cursor: grab;">
<i class="bi bi-grip-vertical"></i> <i class="bi bi-grip-vertical"></i>
@ -245,8 +245,6 @@
handle: '.drag-handle', handle: '.drag-handle',
animation: 150, animation: 150,
onEnd: function (evt) { onEnd: function (evt) {
console.log(evt.oldIndex, evt.newIndex);
var habitId = el.children[evt.newIndex].id.split('-')[1]; var habitId = el.children[evt.newIndex].id.split('-')[1];
var oldIndex = evt.oldIndex; var oldIndex = evt.oldIndex;
var newIndex = evt.newIndex; var newIndex = evt.newIndex;
@ -257,7 +255,6 @@
} }
}).then(function (response) { }).then(function (response) {
// Handle the success response if needed // Handle the success response if needed
console.log(response.data);
}).catch(function (error) { }).catch(function (error) {
// Handle the error if needed // Handle the error if needed
console.error('Error:', error); console.error('Error:', error);