From 1f8741a14b11aae822f05a1ee26ba4ebb15e6d85 Mon Sep 17 00:00:00 2001 From: Christian Quest Date: Tue, 3 May 2016 17:11:43 +0200 Subject: [PATCH] timestamp ranges instead of single timestamp --- backend.py | 26 +++++++++++++++++++++----- setup.sql | 6 +++--- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/backend.py b/backend.py index 36884f6..74ab95b 100644 --- a/backend.py +++ b/backend.py @@ -43,9 +43,28 @@ WHERE events_id=%s;""", (id,)) db.close() def on_post(self, req, resp): + resp.set_header('X-Powered-By', 'OpenEventDatabase') + resp.set_header('Access-Control-Allow-Origin', '*') + resp.set_header('Access-Control-Allow-Headers', 'X-Requested-With') + # get request body payload (geojson Feature) body = req.stream.read().decode('utf-8') j=json.loads(body) + if "properties" not in j or "geometry" not in j: + resp.body = "missing 'geometry' or 'properties' elements" + resp.status = falcon.HTTP_400 + if "start" not in j['properties']: + event_start = j['properties']['when'] + else: + event_start = j['properties']['start'] + if "stop" not in j['properties']: + event_stop = j['properties']['when'] + else: + event_stop = j['properties']['stop'] + if event_start == event_stop: + when = "["+event_start+", "+event_stop+"]" + else: + when = "["+event_start+", "+event_stop+")" # connect to db and insert db = psycopg2.connect("dbname=oedb") cur = db.cursor() @@ -58,7 +77,7 @@ WHERE events_id=%s;""", (id,)) if h is None: cur.execute("""SELECT md5(st_asewkt(st_geomfromgeojson( %s )));""",(geometry,)) h = cur.fetchone() - cur.execute("""INSERT INTO events ( events_type, events_what, events_when, events_tags, events_geo) VALUES (%s, %s, %s, %s, %s) RETURNING events_id;""",(j['properties']['type'],j['properties']['what'],j['properties']['when'],json.dumps(j['properties']),h[0])) + cur.execute("""INSERT INTO events ( events_type, events_what, events_when, events_tags, events_geo) VALUES (%s, %s, %s, %s, %s) RETURNING events_id;""",(j['properties']['type'],j['properties']['what'],when,json.dumps(j['properties']),h[0])) # get newly created event id e = cur.fetchone() db.commit() @@ -66,10 +85,7 @@ WHERE events_id=%s;""", (id,)) db.close() # send back to client resp.body = """{"id":"%s"}""" % (e[0]) - resp.set_header('X-Powered-By', 'OpenEventDatabase') - resp.set_header('Access-Control-Allow-Origin', '*') - resp.set_header('Access-Control-Allow-Headers', 'X-Requested-With') - resp.status = falcon.HTTP_200 + resp.status = falcon.HTTP_201 # falcon.API instances are callable WSGI apps diff --git a/setup.sql b/setup.sql index 7c0c803..0c8c1e1 100644 --- a/setup.sql +++ b/setup.sql @@ -92,13 +92,13 @@ SET default_with_oids = false; CREATE TABLE events ( events_what text, - events_when timestamp with time zone, events_type text, events_tags json, events_id uuid DEFAULT uuid_generate_v4(), createdate timestamp without time zone DEFAULT now(), lastupdate timestamp without time zone, - events_geo text + events_geo text, + events_when tstzrange ); @@ -147,7 +147,7 @@ CREATE INDEX events_idx_what ON events USING spgist (events_what); -- Name: events_idx_when; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX events_idx_when ON events USING btree (events_when); +CREATE INDEX events_idx_when ON events USING spgist (events_when); --