From f6d73021d096228bcd4382d4c0f119b632284ebc Mon Sep 17 00:00:00 2001 From: samerbam Date: Mon, 28 Aug 2023 11:33:56 -0400 Subject: [PATCH] run in background --- application/database.py | 9 ++++++++- application/main.py | 8 ++++++++ application/scheduler_process.py | 29 +++++++++++++++++++++++++++++ application/static/scripts.js | 2 ++ 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 application/scheduler_process.py diff --git a/application/database.py b/application/database.py index 21e1622..3b6f9d5 100644 --- a/application/database.py +++ b/application/database.py @@ -20,7 +20,7 @@ class TodoDatabase: self.database["recurring"] = [] if date not in self.database: # if not len(self.database[date]) > 0: - self.database[date] = copy.deepcopy(self.database["recurring"]) + self.database[date] = [] #copy.deepcopy(self.database["recurring"]) if todo.recurring: self.database["recurring"].append(todo.model_dump()) self.database[date].append(todo.model_dump()) @@ -37,6 +37,11 @@ class TodoDatabase: if date in self.database: del self.database[date] + def remove_recurring(self, todo): + if 'recurring' in self.database: + self.database['recurring'] = [i for i in self.database['recurring'] if (i["time"] == todo.time and i['task'] == todo.task)] + # t = [i for i in t if i.name!="Remove me"] + def read_todos(self, date): # if self.datebase[date] is None: # return [] @@ -44,6 +49,8 @@ class TodoDatabase: if "recurring" not in self.database: return [] return self.database["recurring"] + if "recurring" in self.database: + return self.database[date] + self.database['recurring'] return self.database[date] def _update_quotes(self): diff --git a/application/main.py b/application/main.py index c3e7da4..f537351 100644 --- a/application/main.py +++ b/application/main.py @@ -12,6 +12,7 @@ from .database import TodoDatabase from .thermal_print import ThermalPrinter from .sync_calendar import SyncData from .tododatatypes import * +from .scheduler_process import run_continuously import errno import os @@ -23,6 +24,8 @@ auth = VerifyToken() data = TodoDatabase() calendar = SyncData() printer = ThermalPrinter(data) + +stop_run_continuously = run_continuously() # printer.print_default() #temp debug # printer.print_custom("Configuration Site: \n\n") # printer.print_qrcode() @@ -230,6 +233,11 @@ def write_todos(todos: TodoList, auth_result: str = Security(auth.verify)): # return {"result": "success"} +@app.post("/api/todos/remove_recurring") +def remove_recurring_todo(todo: Todo, auth_result: str = Security(auth.verify)): + data.remove_recurring(todo) + return {"status": "success"} + @app.post("/api/todos/print") def print_todos(action: PrintAction, auth_result: str = Security(auth.verify)): """A valid access token is required to access this route""" diff --git a/application/scheduler_process.py b/application/scheduler_process.py new file mode 100644 index 0000000..1c1a66f --- /dev/null +++ b/application/scheduler_process.py @@ -0,0 +1,29 @@ +import threading +import time + +import schedule + + +def run_continuously(interval=1): + """Continuously run, while executing pending jobs at each + elapsed time interval. + @return cease_continuous_run: threading. Event which can + be set to cease continuous run. Please note that it is + *intended behavior that run_continuously() does not run + missed jobs*. For example, if you've registered a job that + should run every minute and you set a continuous run + interval of one hour then your job won't be run 60 times + at each interval but only once. + """ + cease_continuous_run = threading.Event() + + class ScheduleThread(threading.Thread): + @classmethod + def run(cls): + while not cease_continuous_run.is_set(): + schedule.run_pending() + time.sleep(interval) + + continuous_thread = ScheduleThread() + continuous_thread.start() + return cease_continuous_run \ No newline at end of file diff --git a/application/static/scripts.js b/application/static/scripts.js index e7aa801..76d4d6c 100644 --- a/application/static/scripts.js +++ b/application/static/scripts.js @@ -538,6 +538,8 @@ function recurringButtonListener(e) { this.querySelector("svg").classList.add("text-black") this.querySelector("svg").classList.remove("text-lime-600") this.parentElement.parentElement.dataset.recurring = false + //make call to with this todo "/api/todos/remove_recurring" + return } this.querySelector("svg").classList.remove("text-black")