summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authorngharo <nick@ngha.ro>2017-12-29 00:02:05 -0600
committerngharo <nick@ngha.ro>2017-12-29 00:02:05 -0600
commit75d05326d067857d22474cc91d069003d7b64b17 (patch)
tree150958928ea0d3f129b55d3694227b3344adf978 /server
parent67c7dbdd19afcc17816e43ff66873e76101704a5 (diff)
downloadimOk-www-75d05326d067857d22474cc91d069003d7b64b17.tar.xz
imOk-www-75d05326d067857d22474cc91d069003d7b64b17.zip
VueJS front-end work in progress
Diffstat (limited to 'server')
-rw-r--r--server/button.py45
-rw-r--r--server/buttonDispatcher.py55
-rw-r--r--server/dispatcher.py29
-rw-r--r--server/lib/database.py5
4 files changed, 101 insertions, 33 deletions
diff --git a/server/button.py b/server/button.py
index a5b52b8..1e79629 100644
--- a/server/button.py
+++ b/server/button.py
@@ -3,6 +3,7 @@ import sqlite3
import json
from lib import uuid
from lib import database
+from time import time
STATUS_INVENTORY = 0
STATUS_ACTIVE = 1
@@ -12,7 +13,7 @@ STATUS_DEAD = 4
def loadAll():
db = database.connect()
- db.execute("SELECT id,status FROM buttons")
+ db.execute("SELECT id,status,last_pressed FROM buttons")
results = []
while True:
@@ -30,19 +31,22 @@ class Button(object):
self.persisted = False
self.id = None
self.status = STATUS_INVENTORY
+ self.last_pressed = 0
+ self.history = []
def toDict(self):
return {
'id': self.id,
- 'status': self.status
+ 'status': self.status,
+ 'last_pressed': self.last_pressed
}
def toJSON(self):
- return json.dumps(self.toDict())
+ return self.toDict()
def loadById(self, id):
db = database.connect()
- db.execute("SELECT id,status FROM buttons WHERE id = ?" , (str(id),))
+ db.execute("SELECT id,status,last_pressed FROM buttons WHERE id = ?" , (str(id),))
return self.loadFromDb(db.fetchone())
@@ -51,14 +55,18 @@ class Button(object):
raise cherrypy.HTTPError(404, 'Button not found')
self.persisted = True
- self.id, self.status = dbrs
+ self.id, self.status, self.last_pressed = dbrs
return self
+ def press(self):
+ if self.last_pressed:
+ self.history.insert(0, self.last_pressed)
+
+ self.last_pressed = int(time())
+ return self
+
def save(self):
- cherrypy.log('SAVE {} -> {} (persisted: {})'.format(
- self.id, self.status, self.persisted
- ))
db = database.connect()
# validate
@@ -67,17 +75,24 @@ class Button(object):
# upsert: update
elif self.persisted:
- cherrypy.log("setting other status to " + str(self.status))
- db.execute("UPDATE buttons SET status = ? WHERE id = ?",
- (int(self.status), str(self.id),)
+ db.execute("UPDATE buttons SET status = ?, last_pressed = ? WHERE id = ?",
+ (self.status, self.last_pressed, self.id,)
)
# upsert: insert
else:
- cherrypy.log('INSERT {} -> {} (persisted: {})'.format(
- self.id, self.status, self.persisted
- ))
- db.execute("INSERT INTO buttons VALUES (?, ?)", (self.id, self.status))
+ db.execute("INSERT INTO buttons VALUES (?, ?, ?)", (self.id, self.status, self.last_pressed))
db.connection.commit()
return self
+
+ def delete(self):
+ db = database.connect()
+
+ if self.id is None:
+ raise cherrypy.HTTPError(500, "uhh wat")
+
+ db.execute("DELETE FROM buttons WHERE id = ?", (self.id,))
+ db.connection.commit()
+
+ return None
diff --git a/server/buttonDispatcher.py b/server/buttonDispatcher.py
index 5dac61c..59672d6 100644
--- a/server/buttonDispatcher.py
+++ b/server/buttonDispatcher.py
@@ -1,38 +1,59 @@
import json
import cherrypy
from button import loadAll, Button
+from dispatcher import Dispatcher
from lib import uuid
+# Tweak: consider using serpy for serializing objects for JSON.
+
@cherrypy.expose
-class ButtonDispatcher(object):
+class ButtonDispatcher(Dispatcher):
def GET(self, id=None):
+ # GET /button - return all buttons
if id is None:
- return json.dumps(loadAll())
- elif uuid.validate(id):
- button = Button()
- return button.loadById(id).toJSON()
- else:
- raise cherrypy.HTTPError(400, 'Invalid ID')
+ return self.response(loadAll())
+
+ # GET /button/{id} - return single button
+ self.validate_uuid(id)
+
+ button = Button()
+ return self.response(button.loadById(id).toJSON())
def POST(self, id, status):
- if not uuid.validate(id):
- raise cherrypy.HTTPError(401)
+ # POST /button/{id}?status={} - updates button status
+ self.validate_uuid(id)
button = Button()
button.loadById(id)
cherrypy.log("Updating status to " + str(status))
- button.status = status
+ # TODO validate status
+ button.status = int(status)
button.save()
- return button.toJSON()
+ return self.response(button.toJSON())
- def PUT(self):
- cherrypy.log('Generating new button')
+ def PUT(self, id=None):
button = Button()
- button.id = uuid.gen()
- return button.save().toJSON()
+ # PUT /button - creates button
+ if id is None:
+ button.id = uuid.gen()
+ return self.response(button.save().toJSON())
+
+ # PUT /button/{id} - presses button
+ else:
+ button.loadById(id)
+ button.press()
+
+ button.save()
+
+ return self.response(button.toJSON())
+
+ def DELETE(self, id):
+ # DELETE /button/{id} - deletes button
+ button = Button()
+ button.loadById(id)
+ button.delete()
- def DELETE(self):
- return 'DELETE BUTTON'
+ return self.response(True)
diff --git a/server/dispatcher.py b/server/dispatcher.py
new file mode 100644
index 0000000..36dd8da
--- /dev/null
+++ b/server/dispatcher.py
@@ -0,0 +1,29 @@
+import json
+import cherrypy
+from lib import uuid
+
+class Dispatcher(object):
+ def validate_uuid(self, uuid4):
+ if not uuid.validate(uuid4):
+ self.error(1, 'Invalid UUID in request')
+
+ def response(self, data):
+ # Pretty print responses
+ return json.dumps(data, sort_keys=True, indent=2, separators=(',', ': '))
+
+ def error(self, code, message='An application error occurred'):
+ raise cherrypy.HTTPError(400,
+ self.response({'error': {'code': int(code), 'message': str(message)}})
+ )
+
+ def GET(self, id=None):
+ raise cherrypy.HTTPError(405)
+
+ def POST(self, id, status):
+ raise cherrypy.HTTPError(405)
+
+ def PUT(self):
+ raise cherrypy.HTTPError(405)
+
+ def DELETE(self):
+ raise cherrypy.HTTPError(405)
diff --git a/server/lib/database.py b/server/lib/database.py
index d4e47e6..c144d07 100644
--- a/server/lib/database.py
+++ b/server/lib/database.py
@@ -1,5 +1,8 @@
import sqlite3
+import os
def connect():
- connection = sqlite3.connect('../data/imok.db')
+ connection = sqlite3.connect(
+ os.path.dirname(os.path.realpath(__file__)) + '/../../data/imok.db'
+ )
return connection.cursor()