From ea1814e166cc236c04a2c3b036baea0315322be5 Mon Sep 17 00:00:00 2001 From: Pratyush Desai Date: Sun, 15 Dec 2024 21:00:42 +0530 Subject: [PATCH] Finish the base web doselog display The issues are .. One can't load all the doses and then filter dynamically via search. One has to limit records. And it doesn't randomize. Signed-off-by: Pratyush Desai --- plugin.py | 223 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 172 insertions(+), 51 deletions(-) diff --git a/plugin.py b/plugin.py index b643da7..42b3def 100644 --- a/plugin.py +++ b/plugin.py @@ -75,75 +75,101 @@ class TripsitServerCallback(httpserver.SupyHTTPServerCallback): self.plugin = plugin # to access db def doGet(self, handler, path): - # '/doses' if path == '/doses': - nick = 'mogad0n' - dose_logs = self.plugin.db.get(nick, {}).get('doses', []) + # Collect all dose logs from self.db + dose_logs = [] + for nick, data in self.plugin.db.items(): + for dose in data.get('doses', []): + dose_logs.append({ + 'nick': nick, + 'time': dose['time'], + 'dose': dose['dose'], + 'drug': dose['drug'], + 'method': dose['method'], + }) - # HTML - response = """ + # Create HTML response + html_response = """ Dose Logs + -

Dose Logs for {}

- - - - - - - - """.format(nick) - +

Dose Logs

+ +
TimeDoseDrugMethod
+ + + + + + + + + + + """ + # Add rows for each dose log for log in dose_logs: - response += """ - - - - - - - """.format( - log['time'], - log['dose'], - log['drug'], - log['method'] - ) + html_response += f""" + + + + + + + + """ - response += """ + html_response += """ +
NickTimeDoseDrugMethod
{}{}{}{}
{log['nick']}{log['time']}{log['dose']}{log['drug']}{log['method']}
+ """ - # Response + # Send the HTML response handler.send_response(200) handler.send_header('Content-type', 'text/html') handler.end_headers() - handler.wfile.write(response.encode('utf-8')) - return - - # handle unknown paths - handler.send_response(404) - handler.send_header('Content-type', 'text/html') - handler.end_headers() - handler.wfile.write(b""" - - - - - 404 Not Found - - -

404 Not Found

-

The requested resource was not found on this server.

- - - """) + handler.wfile.write(html_response.encode('utf-8')) + else: + # 404 response for unknown paths + handler.send_response(404) + handler.send_header('Content-type', 'text/html') + handler.end_headers() + handler.wfile.write(b"

404 Not Found

") class Tripsit(callbacks.Plugin): """Harm-Reduction tools from tripsit's tripbot and the tripsitwiki""" @@ -294,7 +320,102 @@ class Tripsit(callbacks.Plugin): This command takes no arguments. Retrieves the number of doses logged for a given nick - """ + """ def doGet(self, handler, path): + if path == '/doses': + # Collect all dose logs from self.db + dose_logs = [] + for nick, data in self.plugin.db.items(): + for dose in data.get('doses', []): + dose_logs.append({ + 'nick': nick, + 'time': dose['time'], + 'dose': dose['dose'], + 'drug': dose['drug'], + 'method': dose['method'], + }) + + # Create HTML response + html_response = """ + + + + + Dose Logs + + + +

Dose Logs

+ + + + + + + + + + + + + """ + # Add rows for each dose log + for log in dose_logs: + html_response += f""" + + + + + + + + """ + + html_response += """ + +
NickTimeDoseDrugMethod
{log['nick']}{log['time']}{log['dose']}{log['drug']}{log['method']}
+ + + + """ + + # Send the HTML response + handler.send_response(200) + handler.send_header('Content-type', 'text/html') + handler.end_headers() + handler.wfile.write(html_response.encode('utf-8')) + else: + # 404 response for unknown paths + handler.send_response(404) + handler.send_header('Content-type', 'text/html') + handler.end_headers() + handler.wfile.write(b"

404 Not Found

") nick = msg.nick if nick in self.db: try: