annot-server/models.py
changeset 0 e1d4d7a8255a
child 22 986ee928a866
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/annot-server/models.py	Wed Oct 08 15:14:58 2014 +0200
@@ -0,0 +1,37 @@
+
+#
+# See LICENCE for detail
+# Copyright (c) 2014 IRI
+#
+
+import datetime
+import json
+import uuid
+
+def get_table_create_stmt():
+    return (
+        "CREATE TABLE IF NOT EXISTS annotations ( "
+        "id serial PRIMARY KEY, "
+        "uuid uuid UNIQUE, "
+        "created timestamp default (now() at time zone 'utc') NOT NULL, "
+        "ts timestamptz NOT NULL, "
+        "event varchar(255) NOT NULL, "
+        "channel varchar(255) NOT NULL, "
+        "content json);"
+    )
+
+def insert_annot_async(params, conn):
+
+    content = params.get('content', {})
+    if not isinstance(content, basestring):
+        params['content'] = json.dumps(content)
+    if 'uuid' not in params:
+        params['uuid'] = uuid.uuid4()
+
+    if 'ts' not in params:
+        params['ts'] = datetime.utcnow()
+
+    defer = conn.runOperation("INSERT INTO annotations (uuid, ts, event, channel, content) VALUES (%(uuid)s, %(ts)s, %(event)s, %(channel)s, %(content)s)", params)
+    defer.addCallback(lambda _: params)
+
+    return defer