Fixes issue #28

This commit is contained in:
CasVT
2023-01-28 18:59:27 +01:00
parent 8bd0626b12
commit 53f69b7547
12 changed files with 255 additions and 61 deletions

View File

@@ -8,7 +8,7 @@ from typing import Union
from flask import g
__DATABASE_VERSION__ = 2
__DATABASE_VERSION__ = 3
class Singleton(type):
_instances = {}
@@ -79,7 +79,17 @@ def migrate_db(current_db_version: int) -> None:
for reminder in reminders:
new_reminders_append([round((datetime.fromtimestamp(reminder[0]) - utc_offset).timestamp()), reminder[1]])
cursor.executemany("UPDATE reminders SET time = ? WHERE id = ?;", new_reminders)
__DATABASE_VERSION__ = 2
current_db_version = 2
if current_db_version == 2:
# V2 -> V3
cursor.executescript("""
ALTER TABLE reminders
ADD color VARCHAR(7);
ALTER TABLE templates
ADD color VARCHAR(7);
""")
current_db_version = 3
return
@@ -115,6 +125,8 @@ def setup_db() -> None:
repeat_interval INTEGER,
original_time INTEGER,
color VARCHAR(7),
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (notification_service) REFERENCES notification_services(id)
);
@@ -125,6 +137,8 @@ def setup_db() -> None:
text TEXT,
notification_service INTEGER NOT NULL,
color VARCHAR(7),
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (notification_service) REFERENCES notification_services(id)
);

View File

@@ -156,7 +156,8 @@ class Reminder:
r.notification_service,
ns.title AS notification_service_title,
r.repeat_quantity,
r.repeat_interval
r.repeat_interval,
r.color
FROM
reminders r
INNER JOIN notification_services ns
@@ -176,7 +177,8 @@ class Reminder:
notification_service: int = None,
text: str = None,
repeat_quantity: Literal["year", "month", "week", "day", "hours", "minutes"] = None,
repeat_interval: int = None
repeat_interval: int = None,
color: str = None
) -> dict:
"""Edit the reminder
@@ -185,6 +187,9 @@ class Reminder:
time (int): The new UTC epoch timestamp the the reminder should be send. Defaults to None.
notification_service (int): The new id of the notification service to use to send the reminder. Defaults to None.
text (str, optional): The new body of the reminder. Defaults to None.
repeat_quantity (Literal["year", "month", "week", "day", "hours", "minutes"], optional): The new quantity of the repeat specified for the reminder. Defaults to None.
repeat_interval (int, optional): The new amount of repeat_quantity, like "5" (hours). Defaults to None.
color (str, optional): The new hex code of the color of the reminder, which is shown in the web-ui. Defaults to None.
Returns:
dict: The new reminder info
@@ -212,10 +217,11 @@ class Reminder:
'notification_service': notification_service,
'text': text,
'repeat_quantity': repeat_quantity,
'repeat_interval': repeat_interval
'repeat_interval': repeat_interval,
'color': color
}
for k, v in new_values.items():
if k in ('repeat_quantity', 'repeat_interval') or v is not None:
if k in ('repeat_quantity', 'repeat_interval', 'color') or v is not None:
data[k] = v
# Update database
@@ -224,7 +230,7 @@ class Reminder:
next_time = data["time"]
cursor.execute("""
UPDATE reminders
SET title=?, text=?, time=?, notification_service=?, repeat_quantity=?, repeat_interval=?
SET title=?, text=?, time=?, notification_service=?, repeat_quantity=?, repeat_interval=?, color=?
WHERE id = ?;
""", (
data["title"],
@@ -233,13 +239,14 @@ class Reminder:
data["notification_service"],
data["repeat_quantity"],
data["repeat_interval"],
data["color"],
self.id
))
else:
next_time = _find_next_time(data["time"], data["repeat_quantity"], data["repeat_interval"])
cursor.execute("""
UPDATE reminders
SET title=?, text=?, time=?, notification_service=?, repeat_quantity=?, repeat_interval=?, original_time=?
SET title=?, text=?, time=?, notification_service=?, repeat_quantity=?, repeat_interval=?, original_time=?, color=?
WHERE id = ?;
""", (
data["title"],
@@ -249,6 +256,7 @@ class Reminder:
data["repeat_quantity"],
data["repeat_interval"],
data["time"],
data["color"],
self.id
))
except IntegrityError:
@@ -284,7 +292,7 @@ class Reminders:
sort_by (Literal["time", "time_reversed", "title", "title_reversed"], optional): How to sort the result. Defaults to "time".
Returns:
List[dict]: The id, title, text, time, notification_service and notification_service_title of each reminder
List[dict]: The id, title, text, time, notification_service, notification_service_title and color of each reminder
"""
sort_function = self.sort_functions.get(
sort_by,
@@ -300,7 +308,8 @@ class Reminders:
r.notification_service,
ns.title AS notification_service_title,
r.repeat_quantity,
r.repeat_interval
r.repeat_interval,
r.color
FROM
reminders r
INNER JOIN notification_services ns
@@ -350,7 +359,8 @@ class Reminders:
notification_service: int,
text: str = '',
repeat_quantity: Literal["year", "month", "week", "day", "hours", "minutes"] = None,
repeat_interval: int = None
repeat_interval: int = None,
color: str = None
) -> Reminder:
"""Add a reminder
@@ -361,6 +371,7 @@ class Reminders:
text (str, optional): The body of the reminder. Defaults to ''.
repeat_quantity (Literal["year", "month", "week", "day", "hours", "minutes"], optional): The quantity of the repeat specified for the reminder. Defaults to None.
repeat_interval (int, optional): The amount of repeat_quantity, like "5" (hours). Defaults to None.
color (str, optional): The hex code of the color of the reminder, which is shown in the web-ui. Defaults to None.
Returns:
dict: The info about the reminder
@@ -377,15 +388,15 @@ class Reminders:
try:
if repeat_quantity is None and repeat_interval is None:
id = get_db().execute("""
INSERT INTO reminders(user_id, title, text, time, notification_service)
VALUES (?,?,?,?,?);
""", (self.user_id, title, text, time, notification_service)
INSERT INTO reminders(user_id, title, text, time, notification_service, color)
VALUES (?,?,?,?,?, ?);
""", (self.user_id, title, text, time, notification_service, color)
).lastrowid
else:
id = get_db().execute("""
INSERT INTO reminders(user_id, title, text, time, notification_service, repeat_quantity, repeat_interval, original_time)
VALUES (?, ?, ?, ?, ?, ?, ?, ?);
""", (self.user_id, title, text, time, notification_service, repeat_quantity, repeat_interval, time)
INSERT INTO reminders(user_id, title, text, time, notification_service, repeat_quantity, repeat_interval, original_time, color)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);
""", (self.user_id, title, text, time, notification_service, repeat_quantity, repeat_interval, time, color)
).lastrowid
except IntegrityError:
raise NotificationServiceNotFound

View File

@@ -27,7 +27,8 @@ class Template:
SELECT
id,
title, text,
notification_service
notification_service,
color
FROM templates
WHERE id = ?;
""",
@@ -39,7 +40,8 @@ class Template:
def update(self,
title: str = None,
notification_service: int = None,
text: str = None
text: str = None,
color: str = None
) -> dict:
"""Edit the template
@@ -47,6 +49,7 @@ class Template:
title (str): The new title of the entry. Defaults to None.
notification_service (int): The new id of the notification service to use to send the reminder. Defaults to None.
text (str, optional): The new body of the template. Defaults to None.
color (str, optional): The new hex code of the color of the template, which is shown in the web-ui. Defaults to None.
Returns:
dict: The new template info
@@ -57,21 +60,23 @@ class Template:
new_values = {
'title': title,
'notification_service': notification_service,
'text': text
'text': text,
'color': color
}
for k, v in new_values.items():
if v is not None:
if k in ('color',) or v is not None:
data[k] = v
try:
cursor.execute("""
UPDATE templates
SET title=?, notification_service=?, text=?
SET title=?, notification_service=?, text=?, color=?
WHERE id = ?;
""", (
data['title'],
data['notification_service'],
data['text'],
data['color'],
self.id
))
except IntegrityError:
@@ -95,13 +100,14 @@ class Templates:
"""Get all templates
Returns:
List[dict]: The id, title, text and notification_service
List[dict]: The id, title, text, notification_service and color
"""
templates: list = list(map(dict, get_db(dict).execute("""
SELECT
id,
title, text,
notification_service
notification_service,
color
FROM templates
WHERE user_id = ?
ORDER BY title, id;
@@ -126,7 +132,8 @@ class Templates:
self,
title: str,
notification_service: int,
text: str = ''
text: str = '',
color: str = None
) -> Template:
"""Add a template
@@ -134,16 +141,17 @@ class Templates:
title (str): The title of the entry
notification_service (int): The id of the notification service to use to send the reminder.
text (str, optional): The body of the reminder. Defaults to ''.
color (str, optional): The hex code of the color of the template, which is shown in the web-ui. Defaults to None.
Returns:
Template: The info about the template
"""
try:
id = get_db().execute("""
INSERT INTO templates(user_id, title, text, notification_service)
VALUES (?,?,?,?);
INSERT INTO templates(user_id, title, text, notification_service, color)
VALUES (?,?,?,?,?);
""",
(self.user_id, title, text, notification_service)
(self.user_id, title, text, notification_service, color)
).lastrowid
except IntegrityError:
raise NotificationServiceNotFound