unique id for events + creation date

This commit is contained in:
cquest 2016-04-14 19:05:59 +02:00
parent 183664f1fb
commit 49b116bb5f
2 changed files with 50 additions and 1 deletions

View file

@ -4,6 +4,7 @@
import falcon
import psycopg2
import uuid
import json
class StatsResource(object):
def on_get(self, req, resp):
@ -21,12 +22,37 @@ class StatsResource(object):
resp.set_header('Access-Control-Allow-Headers', 'X-Requested-With')
resp.status = falcon.HTTP_200
class EventResource(object):
def on_post(self, req, resp):
# get request body payload (json)
body = req.stream.read().decode('utf-8')
j=json.loads(body)
# connect to db and insert
db = psycopg2.connect("dbname=oedb")
cur = db.cursor()
cur.execute("""INSERT INTO events ( events_type, events_what, events_when, events_tags) VALUES (%s, %s, %s, %s) RETURNING events_id;""",(j['type'],j['what'],j['when'], body))
# get newly created event id
e = cur.fetchone()
db.commit()
cur.close()
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
# falcon.API instances are callable WSGI apps
app = falcon.API()
# Resources are represented by long-lived class instances
event = EventResource()
stats = StatsResource()
# things will handle all requests to the matching URL path
app.add_route('/event', event) # handle single event requests
app.add_route('/stats', stats)

View file

@ -41,6 +41,20 @@ CREATE EXTENSION IF NOT EXISTS postgis WITH SCHEMA public;
COMMENT ON EXTENSION postgis IS 'PostGIS geometry, geography, and raster spatial types and functions';
--
-- Name: uuid-ossp; Type: EXTENSION; Schema: -; Owner: -
--
CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA public;
--
-- Name: EXTENSION "uuid-ossp"; Type: COMMENT; Schema: -; Owner: -
--
COMMENT ON EXTENSION "uuid-ossp" IS 'generate universally unique identifiers (UUIDs)';
SET search_path = public, pg_catalog;
--
@ -72,10 +86,19 @@ CREATE TABLE events (
events_where geometry,
events_when timestamp with time zone,
events_type text,
events_tags json
events_tags json,
events_id uuid DEFAULT uuid_generate_v4(),
createdate timestamp without time zone DEFAULT now()
);
--
-- Name: events_idx_id; Type: INDEX; Schema: public; Owner: -
--
CREATE UNIQUE INDEX events_idx_id ON events USING btree (events_id);
--
-- Name: events_idx_what; Type: INDEX; Schema: public; Owner: -
--