diff options
-rw-r--r-- | Dockerfile | 2 | ||||
-rw-r--r-- | docker-compose.yml | 2 | ||||
-rw-r--r-- | server.py | 24 | ||||
-rw-r--r-- | static/index.html (renamed from index.html) | 0 | ||||
-rw-r--r-- | static/new_form.html | 74 |
5 files changed, 97 insertions, 5 deletions
@@ -3,7 +3,7 @@ FROM ubuntu RUN apt update -y && apt install -y python3-pip RUN pip install psycopg2-binary -COPY index.html /src/index.html +#COPY static /src/static COPY server.py /src/server.py WORKDIR /src diff --git a/docker-compose.yml b/docker-compose.yml index ee1518d..bad313e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,6 +18,8 @@ services: - 60934:8000 environment: PYTHONUNBUFFERED: 1 + volumes: + - ./static:/src/static grafana: image: grafana/grafana restart: always @@ -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() diff --git a/index.html b/static/index.html index 83136dc..83136dc 100644 --- a/index.html +++ b/static/index.html diff --git a/static/new_form.html b/static/new_form.html new file mode 100644 index 0000000..7022d5a --- /dev/null +++ b/static/new_form.html @@ -0,0 +1,74 @@ +<html> + <head> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + </head> + <body> + <nav></nav> + <div class="main"> + + <div> + <label for="type">Type:</label> + <select name="type" onchange="update_select()"> + <option value="multiple_select">multiple_select</option> + <option value="number">number</option> + <option value="range">range</option> + <option value="textarea">textarea</option> + <option value="text">text</option> + </select> + </div> + + <div> + <label for="prompt">prompt</label> + <input name="prompt" type="text"></input> + </div> + <div> + <label for="prompt_id">prompt_id</label> + <input name="prompt_id" type="text"></input> + </div> + + <div class="range"> + <div> + <label for="min">min</label> + <input name="min" type="text"></input> + </div> + <div> + <label for="max">max</label> + <input name="max" type="text"></input> + </div> + </div> + + <div><button onclick="on_submit()">Submit</button></div> + <script> + async function enqueue_data(path, payload, do_alert=false, timestamp=true){ + if(timestamp){ + payload["timestamp"] = Date.now() + } + await fetch(path, {method: "POST", body: JSON.stringify(payload)}) + if(do_alert){ + alert("saved!") + } + } + function update_select(){ + let v = document.querySelectorAll("select")[0].value + document.querySelectorAll(".range").forEach(el => { + el.style.display = v == "range" ? "block" : "none" + }) + } + function on_submit(){ + let payload = { + "type": document.querySelectorAll("select")[0].value, + } + document.querySelectorAll("input").forEach(el => { + if(el.value.length > 0){ + payload[el.name] = el.value + } + }) + enqueue_data("/submit_new_form", payload, do_alert=true, timestamp=false) + } + window.onload = function(){ + update_select() + } + </script> + </div> + </body> +</html> |