Fixed get_all()
and added delete methods for user and habits
This commit is contained in:
parent
70c9a3fbdf
commit
cc8f2f56a1
1
app.py
1
app.py
@ -4,6 +4,7 @@ import hashlib
|
|||||||
from flask import Flask, render_template, redirect, url_for, request
|
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.User import User
|
from models.User import User
|
||||||
from utils import anonymous_required
|
from utils import anonymous_required
|
||||||
|
|
||||||
|
|||||||
@ -41,6 +41,17 @@ def get_user_by_email(email: str):
|
|||||||
return user
|
return user
|
||||||
|
|
||||||
|
|
||||||
|
def delete_user(id: int):
|
||||||
|
query = f"DELETE FROM habits WHERE user_id = {id};"
|
||||||
|
query2 = f"DELETE FROM users WHERE id = {id};"
|
||||||
|
conn = con3()
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute(query)
|
||||||
|
cursor.execute(query2)
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
def create_habit(name: str, user_id: int, times: int, unit: int, slot: int, note: str | None=None):
|
def create_habit(name: str, user_id: int, times: int, unit: int, slot: int, note: str | None=None):
|
||||||
now = datetime.now().isoformat()
|
now = datetime.now().isoformat()
|
||||||
query = (f"INSERT INTO habits (user_id, name, note, times, unit, list_index, created_at, updated_at) VALUES ('{user_id}', "
|
query = (f"INSERT INTO habits (user_id, name, note, times, unit, list_index, created_at, updated_at) VALUES ('{user_id}', "
|
||||||
@ -58,9 +69,9 @@ def get_habit(id: int):
|
|||||||
conn = con3()
|
conn = con3()
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute(query)
|
cursor.execute(query)
|
||||||
habits = cursor.fetchone()
|
habit = cursor.fetchone()
|
||||||
conn.close()
|
conn.close()
|
||||||
return habits
|
return habit
|
||||||
|
|
||||||
|
|
||||||
def get_habits(user_id: int):
|
def get_habits(user_id: int):
|
||||||
@ -68,6 +79,15 @@ def get_habits(user_id: int):
|
|||||||
conn = con3()
|
conn = con3()
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute(query)
|
cursor.execute(query)
|
||||||
habits = cursor.fetchone()
|
habits = cursor.fetchall()
|
||||||
conn.close()
|
conn.close()
|
||||||
return habits
|
return habits
|
||||||
|
|
||||||
|
|
||||||
|
def delete_habit(id: int):
|
||||||
|
query = f"DELETE FROM habits WHERE id = {id};"
|
||||||
|
conn = con3()
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute(query)
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
Loading…
x
Reference in New Issue
Block a user