From bd0adf19ab86b43820cfb540d59a74ca4470f522 Mon Sep 17 00:00:00 2001 From: "Elf M. Sternberg" Date: Mon, 8 Feb 2016 20:14:24 -0800 Subject: [PATCH] [feat] The ability to get a list of all allocated appointments and office hours is up. TODO: Add *this users* appointments to the list of appointments provided in the given time range. Then: add/update/delete appointments. --- main.hy | 42 +++++++++++++++++++++++++++++++++++++++--- main.py | 10 ---------- officehours.sql | 8 ++++++-- 3 files changed, 45 insertions(+), 15 deletions(-) delete mode 100644 main.py diff --git a/main.hy b/main.hy index 6d0b93c..e0c2f8f 100644 --- a/main.hy +++ b/main.hy @@ -1,10 +1,46 @@ #!/usr/local/bin/hy -(import [flask [Flask]]) +(import psycopg2 psycopg2.extras datetime [flask [Flask jsonify]]) (def app (Flask __name__)) -(with-decorator (.route app "/") (defn hello [] "Hello World")) +(defn connect [] + (psycopg2.connect "host='localhost' dbname='officehours' user='officehours' password='eatabug'")) + +(defn tap [a] (print (str a)) a) + +(defn select [curs cmd &rest args] + (.execute curs cmd (list args)) + (.fetchall curs)) + + +(defn during-to-utc [d] + (let [[ud (.isoformat (getattr (get d 0) "upper"))] + [ld (.isoformat (getattr (get d 0) "lower"))]] + (, ld ud))) + +(with-decorator + (.route app "/") + (defn hello [] + (let [[conn (connect)] + [curs (.cursor conn)] + [week (psycopg2.extras.DateTimeRange (datetime.datetime 2015 1 31 0 0) + (datetime.datetime 2015 2 7 23 59))] + [cmd1 (+ "SELECT during FROM staff_appointments WHERE staff_id IN " + "(SELECT staff_id FROM staff_client_relationships " + "WHERE client_id = %s) AND during && %s")] + [appointments (select curs cmd1 3 week)] + [cmd2 (+ "SELECT during, appointment_id, client_id FROM staff_appointments " + "WHERE client_id = %s AND during && %s")] + [client_appointments (select curs cmd2 3 week)] + [cmd3 (+ "SELECT during FROM officehours WHERE staff_id IN " + "(SELECT staff_id FROM staff_client_relationships " + "WHERE client_id = %s) AND during && %s")] + [officehours (select curs cmd3 3 week)]] + (jsonify { "appointments" (list (map during-to-utc appointments)) + "officehours" (list (map during-to-utc officehours)) })))) (if (= __name__ "__main__") - (.run app)) + (do + (setv app.debug True) + (.run app))) diff --git a/main.py b/main.py deleted file mode 100644 index 59dba86..0000000 --- a/main.py +++ /dev/null @@ -1,10 +0,0 @@ -from flask import Flask - -app = Flask(__name__) - -@app.route("/") # take note of this decorator syntax, it's a common pattern -def hello(): - return "Hello World!" - -if __name__ == "__main__": - app.run() diff --git a/officehours.sql b/officehours.sql index 25675c9..0253d1a 100644 --- a/officehours.sql +++ b/officehours.sql @@ -101,8 +101,12 @@ CREATE VIEW staff_client_relationships AS INNER JOIN client_members ON relationship.client_id = client_members.client_id); CREATE VIEW staff_appointments AS - (SELECT staff_name, staff_client_relationships.staff_id AS staff_id, - client_name, staff_client_relationships.client_id AS client_id, during + (SELECT staff_name, + staff_client_relationships.staff_id AS staff_id, + client_name, + staff_client_relationships.client_id AS client_id, + appointments.id AS appointment_id, + during FROM appointments INNER JOIN staff_client_relationships ON appointments.client_id = staff_client_relationships.client_id);