days.sort()
return days
+ @classmethod
+ def by_date(cls, db_conn: DatabaseConnection, date: str):
+ """Retrieve Day by date if in DB, else return None."""
+ for row in db_conn.exec('SELECT * FROM days WHERE date = ?', (date,)):
+ return cls.from_table_row(row)
+ return None
+
@property
def weekday(self):
"""Return what weekday matches self.date."""
-"""plom's task manager"""
+"""Web server stuff."""
from http.server import BaseHTTPRequestHandler
from http.server import HTTPServer
-from urllib.parse import urlparse
+from urllib.parse import urlparse, parse_qs
from os.path import split as path_split
from jinja2 import Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader
from plomtask.days import Day
def do_GET(self):
"""Handle any GET request."""
try:
+ conn = DatabaseConnection(self.server.db)
parsed_url = urlparse(self.path)
site = path_split(parsed_url.path)[1]
+ params = parse_qs(parsed_url.query)
if 'calendar' == site:
- html = self.do_GET_calendar()
+ html = self.do_GET_calendar(conn)
+ elif 'day' == site:
+ date = params.get('date', ['2024-01-01'])[0]
+ html = self.do_GET_day(conn, date)
else:
raise HandledException('Test!')
+ conn.commit()
+ conn.close()
self.send_html(html)
except HandledException as error:
self.send_msg(error)
- def do_GET_calendar(self):
- """Show sorted Days."""
- conn = DatabaseConnection(self.server.db)
- Day('2024-01-03').save(conn)
- Day('2024-01-01').save(conn)
- Day('2024-01-02').save(conn)
- days = Day.all(conn)
- conn.commit()
- conn.close()
+ def do_GET_calendar(self, conn: DatabaseConnection):
+ """Show Days."""
return self.server.jinja.get_template('calendar.html').render(
- days=days)
+ days=Day.all(conn))
+
+ def do_GET_day(self, conn: DatabaseConnection, date: str):
+ """Show single Day."""
+ day = Day.by_date(conn, date)
+ return self.server.jinja.get_template('day.html').render(day=day)
"""Test Day.weekday."""
self.assertEqual(Day('2024-03-17').weekday, 'Sunday')
+ def test_Day_by_date(self):
+ """Test Day.by_date()."""
+ timestamp = datetime.now().timestamp()
+ db_file = DatabaseFile(f'test_db:{timestamp}')
+ db_file.remake()
+ conn = DatabaseConnection(db_file)
+ self.assertEqual(None, Day.by_date(conn, '2024-01-01'))
+ Day('2024-01-01').save(conn)
+ self.assertEqual(Day('2024-01-01'), Day.by_date(conn, '2024-01-01'))
+ conn.close()
+ remove_file(db_file.path)
+
def test_Day_all(self):
"""Test Day.all(), especially in regards to date range filtering."""
timestamp = datetime.now().timestamp()