Small update with bug fixes
This commit is contained in:
parent
4764112296
commit
05b5869be7
11
app.py
11
app.py
@ -128,7 +128,8 @@ def index():
|
||||
name = "Bitte melde dich an."
|
||||
|
||||
# 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(
|
||||
'index.html',
|
||||
@ -324,7 +325,8 @@ def check_habit():
|
||||
return {"error": "Habit not found"}
|
||||
|
||||
# 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"}
|
||||
|
||||
trackings = habit.get_habitTrackings()
|
||||
@ -360,7 +362,7 @@ def delete_habit():
|
||||
return {"error": "Habit not found"}
|
||||
|
||||
# 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"}
|
||||
|
||||
habit.delete()
|
||||
@ -377,7 +379,8 @@ def reorder_habits():
|
||||
return {"error": "Habit not found"}
|
||||
|
||||
# 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"}
|
||||
|
||||
habit.update_slot(new_index)
|
||||
|
||||
@ -295,6 +295,16 @@ def delete_habitList(id: int):
|
||||
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__":
|
||||
habits = get_habits(1)
|
||||
for habit in habits:
|
||||
|
||||
@ -4,7 +4,7 @@ from datetime import datetime
|
||||
|
||||
from models.HabitTrackings import HabitTrackings
|
||||
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:
|
||||
@ -30,6 +30,7 @@ class Habit:
|
||||
@staticmethod
|
||||
def create(list_id: int, name: str, times: int, note: str | None = None, unit: int | None = 1):
|
||||
slot = get_next_slot(list_id)
|
||||
print(slot)
|
||||
id = create_habit(list_id, name, times, unit, slot, note)
|
||||
return Habit(id, list_id, name, note, times, unit, slot)
|
||||
|
||||
@ -115,3 +116,9 @@ class Habit:
|
||||
|
||||
def to_json(self):
|
||||
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
|
||||
|
||||
@ -2,8 +2,9 @@ from dataclasses import dataclass
|
||||
from datetime import date, datetime
|
||||
|
||||
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.User import User
|
||||
|
||||
|
||||
@dataclass
|
||||
@ -13,6 +14,7 @@ class HabitList:
|
||||
description: str
|
||||
created_at: date
|
||||
updated_at: date
|
||||
habits: list = None
|
||||
|
||||
@staticmethod
|
||||
def create(user_id: int, name: str, description: str):
|
||||
@ -35,3 +37,12 @@ class HabitList:
|
||||
habits.append(habit)
|
||||
|
||||
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
|
||||
|
||||
@ -3,8 +3,6 @@ from datetime import datetime
|
||||
from flask_login import UserMixin
|
||||
from db.SQLiteClient import create_user, get_user, get_user_by_email, get_habits, delete_user, update_user, \
|
||||
get_habitLists
|
||||
from models.Habit import Habit
|
||||
from models.HabitList import HabitList
|
||||
|
||||
|
||||
class User(UserMixin):
|
||||
@ -44,6 +42,8 @@ class User(UserMixin):
|
||||
# return habits
|
||||
|
||||
def get_habitLists(self):
|
||||
from models.HabitList import HabitList
|
||||
|
||||
raw_habitLists = get_habitLists(self.id)
|
||||
habitLists = []
|
||||
for habitList in raw_habitLists:
|
||||
|
||||
@ -95,7 +95,7 @@
|
||||
</div>
|
||||
|
||||
<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 }}">
|
||||
<div class="col-auto drag-handle" style="cursor: grab;">
|
||||
<i class="bi bi-grip-vertical"></i>
|
||||
@ -245,8 +245,6 @@
|
||||
handle: '.drag-handle',
|
||||
animation: 150,
|
||||
onEnd: function (evt) {
|
||||
console.log(evt.oldIndex, evt.newIndex);
|
||||
|
||||
var habitId = el.children[evt.newIndex].id.split('-')[1];
|
||||
var oldIndex = evt.oldIndex;
|
||||
var newIndex = evt.newIndex;
|
||||
@ -257,7 +255,6 @@
|
||||
}
|
||||
}).then(function (response) {
|
||||
// Handle the success response if needed
|
||||
console.log(response.data);
|
||||
}).catch(function (error) {
|
||||
// Handle the error if needed
|
||||
console.error('Error:', error);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user