"""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'
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_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()
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))
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)
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: