Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions log2s3/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"today_color": "yellow",
}
exts = set(stream_ext.keys())
month_query = Query(pattern="(^[0-9]{4}|^$)", default="")
month_query = Query(pattern="(^[0-9]{4}|all|^$)", default="")


def update_config(conf: dict):
Expand Down Expand Up @@ -223,11 +223,22 @@ def html2_gen1(uri: str, month: str, files: dict[str, str]) -> str:
return buf.getvalue()


def html2_gen(ldir: dict[str, dict[str, str]], file_path: str):
def html2_gen(ldir: dict[str, dict[str, str]], file_path: str, current_month: str):
buf = io.StringIO()
buf.write(f"<html><title>{file_path}</title><body>")
thismonth = datetime.date.today().strftime("%Y-%m")
buf.write(f'<p><a href="?month={thismonth}">this month</a></p>')
if current_month in ("", "all"):
current_month = datetime.date.today().strftime("%Y-%m")
today = datetime.date.strptime(current_month, "%Y-%m")
lastm = today - datetime.timedelta(days=1)
nextm = today + datetime.timedelta(days=31)
fmt = "%Y-%m"
buf.write("<div>")
buf.write(f'<a href="?month={lastm.strftime(fmt)}">{lastm.strftime(fmt)}</a>')
buf.write(f' | <a href="?month={today.strftime(fmt)}">{today.strftime(fmt)}</a>')
buf.write(f' | <a href="?month={nextm.strftime(fmt)}">{nextm.strftime(fmt)}</a>')
buf.write(' | <a href="?month=all">all time</a>')
buf.write(' | <a href="./">this month</a>')
buf.write("</div>")
for title, files in ldir.items():
uri = uriescape(f"html2/{title}")
buf.write('<div style="float: left; margin: 1em;">')
Expand Down Expand Up @@ -255,10 +266,14 @@ def html2_gen(ldir: dict[str, dict[str, str]], file_path: str):

@router.get("/html2/{file_path:path}")
def html2(file_path: str, month=month_query):
if not month:
month = datetime.date.today().strftime("%Y-%m")
if month == "all":
month = ""
ldir = list_dir(file_path, month)
if len(ldir) == 0:
raise HTTPException(status_code=404, detail=f"not found: {file_path}")
return StreamingResponse(content=html2_gen(ldir, file_path), media_type="text/html")
return StreamingResponse(content=html2_gen(ldir, file_path, month), media_type="text/html")


def find_target(p: Path, accepts: list[str]) -> Path:
Expand Down
5 changes: 3 additions & 2 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def test_read_html1_notfound(self):
self.assertEqual(404, res.status_code)

def test_read_html2(self):
res = self.client.get("/html2/ba")
res = self.client.get("/html2/ba", params={"month": "all"})
self.assertEqual(200, res.status_code)
self.assertIn("text/html", res.headers.get("content-type"))
self.assertIn("2024-01-04", res.text)
Expand All @@ -158,7 +158,8 @@ def test_read_html2_month(self):
self.assertEqual(200, res.status_code)
self.assertIn("text/html", res.headers.get("content-type"))
self.assertIn("2024-01-04", res.text)
self.assertNotIn("2024-02", res.text)
self.assertIn("2024-02", res.text)
self.assertNotIn("2024-03", res.text)

def test_read_html2_notfound(self):
res = self.client.get("/html2/ba", params={"month": "2025-01"})
Expand Down
Loading