From: Christian Heller Date: Sun, 17 Mar 2024 02:47:56 +0000 (+0100) Subject: Add database connection, read and write Days through them. X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/decks/%7B%7Bdb.prefix%7D%7D/static/todo?a=commitdiff_plain;h=9c3ba57169fdc71d3ffefd4c7c20cae4e89f9c9c;p=plomtask Add database connection, read and write Days through them. --- diff --git a/plomtask/days.py b/plomtask/days.py index 4e0c474..02ad1a8 100644 --- a/plomtask/days.py +++ b/plomtask/days.py @@ -1,6 +1,8 @@ """Collecting Day and date-related items.""" from datetime import datetime +from sqlite3 import Row from plomtask.misc import HandledException +from plomtask.db import DatabaseConnection DATE_FORMAT = '%Y-%m-%d' @@ -23,6 +25,24 @@ class Day: if not self.datetime: raise HandledException(f'Given date of wrong format: {self.date}') + @classmethod + def add(cls, db_conn: DatabaseConnection, date: str): + """Add (or re-write) new Day(date) to database.""" + db_conn.exec('REPLACE INTO days VALUES (?)', (date,)) + + @classmethod + def from_table_row(cls, row: Row): + """Make new Day from database row.""" + return cls(row[0]) + + @classmethod + def all(cls, db_conn: DatabaseConnection): + """Return list of all Days in database.""" + days = [] + for row in db_conn.exec('SELECT * FROM days'): + days += [cls.from_table_row(row)] + return days + @property def weekday(self): """Return what weekday matches self.date.""" diff --git a/plomtask/db.py b/plomtask/db.py index b7291db..50eb737 100644 --- a/plomtask/db.py +++ b/plomtask/db.py @@ -40,3 +40,23 @@ class DatabaseFile: diff_msg = Differ().compare(retrieved_schema.splitlines(), stored_schema.splitlines()) raise HandledException(msg_err + '\n'.join(diff_msg)) + + +class DatabaseConnection: + """A single connection to the database.""" + + def __init__(self, db_file: DatabaseFile): + self.file = db_file + self.conn = sql_connect(self.file.path) + + def commit(self): + """Commit SQL transaction.""" + self.conn.commit() + + def exec(self, code: str, inputs: tuple = ()): + """Add commands to SQL transaction.""" + return self.conn.execute(code, inputs) + + def close(self): + """Close DB connection.""" + self.conn.close() diff --git a/plomtask/http.py b/plomtask/http.py index baa730e..2039cf9 100644 --- a/plomtask/http.py +++ b/plomtask/http.py @@ -6,13 +6,15 @@ from os.path import split as path_split from jinja2 import Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader from plomtask.days import Day from plomtask.misc import HandledException +from plomtask.db import DatabaseConnection class TaskServer(HTTPServer): """Variant of HTTPServer that knows .jinja as Jinja Environment.""" - def __init__(self, templates_dir, *args, **kwargs): + def __init__(self, templates_dir, db_file, *args, **kwargs): super().__init__(*args, **kwargs) + self.db = db_file self.jinja = JinjaEnv(loader=JinjaFSLoader(templates_dir)) @@ -46,7 +48,13 @@ class TaskHandler(BaseHTTPRequestHandler): def do_GET_calendar(self): """Show sorted Days.""" - days = [Day('2024-01-03'), Day('2024-01-01'), Day('2024-01-02')] + conn = DatabaseConnection(self.server.db) + Day.add(conn, '2024-01-03') + Day.add(conn, '2024-01-01') + Day.add(conn, '2024-01-02') + days = Day.all(conn) + conn.commit() + conn.close() days.sort() return self.server.jinja.get_template('calendar.html').render( days=days) diff --git a/run.py b/run.py index e0cdd8b..ef6aeab 100755 --- a/run.py +++ b/run.py @@ -25,7 +25,7 @@ if __name__ == '__main__': else: print('Not recognizing reply as "yes".') raise HandledException('Cannot run without database.') - server = TaskServer(TEMPLATES_DIR, + server = TaskServer(TEMPLATES_DIR, db_file, ('localhost', HTTP_PORT), TaskHandler) print(f'running at port {HTTP_PORT}') try: