summaryrefslogtreecommitdiff
path: root/jimbrella
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2021-10-22 22:06:08 +0800
committerFrederick Yin <fkfd@fkfd.me>2021-10-22 22:06:08 +0800
commit3b16f04f4f3c6085ffef2ac058fca0a17fcb49ca (patch)
treeeb957de4f905aae58ea9dae610a08a2eb9577363 /jimbrella
parentec7ff623a83146d1a814fbe30b922500a1bfe711 (diff)
Routines, to be run at an interval
Diffstat (limited to 'jimbrella')
-rw-r--r--jimbrella/routine.py68
1 files changed, 68 insertions, 0 deletions
diff --git a/jimbrella/routine.py b/jimbrella/routine.py
new file mode 100644
index 0000000..784b60c
--- /dev/null
+++ b/jimbrella/routine.py
@@ -0,0 +1,68 @@
+from .database import Database
+from .jform import JForm
+from .config import *
+from .exceptions import *
+
+"""A set of routine methods, run at an interval (somewhere from ten minutes to one hour), to:
+- sync JImbrella's databse against data pulled from jForm
+- check if any umbrella is now overdue
+- send an SMS where applicable, to a user or an admin
+"""
+
+
+def chronological_merge(*sheet_lists) -> list:
+ """Merges all lists answer sheets passed in, in chronological order, into a single list.
+
+ All lists of sheets in sheet_lists MUST be already in chronological order.
+ By "chronological", we mean sorting by the value under key "date".
+ """
+ chronicle = []
+ while any(sheet_lists): # at least one list in `sheet_lists` is non-empty
+ # for each list of sheets, read date from its first element, then find the earliest
+ # is an instance of datetime.datetime
+ # only pass non-empty lists to min():
+ earliest_date = min(
+ [sheet_list[0]["date"] for sheet_list in filter(None, sheet_lists)]
+ )
+ for idx, sheet_list in enumerate(sheet_lists):
+ if not sheet_list: # is empty
+ continue
+ if sheet_list[0]["date"] == earliest_date:
+ # remove the first element and append it to `chronicle`, if it is the earliest
+ chronicle.append(sheet_lists[idx].pop(0))
+ # we do not directly break here because there exists a tiny chance
+ # two answer sheets were submitted at the exact same millisecond
+ return chronicle
+
+
+def sync_database(takeaway: JForm, giveback: JForm, db: Database):
+ takeaway_unread = takeaway.get_unread()
+ giveback_unread = giveback.get_unread()
+ unread = chronological_merge(takeaway_unread, giveback_unread)
+ for sheet in unread:
+ if sheet["jform_name"] == "takeaway":
+ try:
+ db.take_away(
+ sheet["key"], sheet["date"], sheet["name"], sheet["id"], sheet["phone"]
+ )
+ except UmbrellaStatusError:
+ pass
+ except UmbrellaNotFoundError:
+ pass
+ elif sheet["jform_name"] == "giveback":
+ try:
+ db.give_back(sheet["key"], sheet["date"])
+ except UmbrellaStatusError:
+ pass
+ except UmbrellaNotFoundError:
+ pass
+
+
+if __name__ == "__main__":
+ takeaway = JForm("takeaway", JFORM_TAKEAWAY_URL, JFORM_BOOKMARK_DIR)
+ # giveback = JForm(
+ # "giveback", JFORM_GIVEBACK_URL, JFORM_BOOKMARK_DIR
+ # )
+ giveback = None
+ db = Database(DATABASE_PATH)
+ sync_database(takeaway, giveback, db)