diff options
Diffstat (limited to 'server.py')
-rw-r--r-- | server.py | 24 |
1 files changed, 20 insertions, 4 deletions
@@ -30,14 +30,19 @@ class TrackerHTTPRequestHandler(BaseHTTPRequestHandler): cur.execute("UPDATE book SET completed = TRUE where id = %s", (book_id,)) conn.commit() + def send_static_file(self, path): + with open(f"static/{path}", "rb") as f: + self.send_response(200) + self.end_headers() + self.wfile.write(f.read()) + def do_GET(self): u = urlparse(self.path) print("GET", u.path) if u.path == "/": - with open("index.html", "rb") as f: - self.send_response(200) - self.end_headers() - self.wfile.write(f.read()) + self.send_static_file("index.html") + elif u.path == "/new_form": + self.send_static_file("new_form.html") elif date_pattern.match(u.path[1:]): with conn.cursor() as cur: cur.execute("SELECT datatype, key, value FROM datapoint where date(created) = %s", (u.path[1:],)) @@ -106,6 +111,17 @@ class TrackerHTTPRequestHandler(BaseHTTPRequestHandler): self.send_response(204) self.end_headers() self.wfile.write(b"") + elif u.path == "/submit_new_form": + prompt_type = post_data.pop("type") + prompt = post_data.pop("prompt") + prompt_id = post_data.pop("prompt_id") + extra = json.dumps(post_data) + with conn.cursor() as cur: + cur.execute("INSERT INTO form (type, prompt, prompt_id, extra) VALUES (%s, %s, %s, %s)", (prompt_type, prompt, prompt_id, extra)) + conn.commit() + self.send_response(204) + self.end_headers() + self.wfile.write(b"") else: self.send_response(404) self.end_headers() |