summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorngharo <nick@ngha.ro>2017-12-28 00:48:34 -0600
committerngharo <nick@ngha.ro>2017-12-28 00:48:34 -0600
commit67c7dbdd19afcc17816e43ff66873e76101704a5 (patch)
tree46fb4285199570de63cc593c67198686f6a54a2d
parentdf3f5844ea334a83955efbab7f58861ae6ccd763 (diff)
downloadimOk-www-67c7dbdd19afcc17816e43ff66873e76101704a5.tar.xz
imOk-www-67c7dbdd19afcc17816e43ff66873e76101704a5.zip
Got CRU part of CRUD working decently
-rw-r--r--server/button.py90
-rw-r--r--server/buttonDispatcher.py38
-rw-r--r--server/lib/uuid.py5
-rw-r--r--server/server.py4
4 files changed, 110 insertions, 27 deletions
diff --git a/server/button.py b/server/button.py
index 8530768..a5b52b8 100644
--- a/server/button.py
+++ b/server/button.py
@@ -4,38 +4,80 @@ import json
from lib import uuid
from lib import database
-@cherrypy.expose
-class Button(object):
- STATUS_INVENTORY = 0
- STATUS_ACTIVE = 1
- STATUS_INACTIVE = 2
- STATUS_SUSPENDED = 3
- STATUS_DEAD = 4
+STATUS_INVENTORY = 0
+STATUS_ACTIVE = 1
+STATUS_INACTIVE = 2
+STATUS_SUSPENDED = 3
+STATUS_DEAD = 4
+
+def loadAll():
+ db = database.connect()
+ db.execute("SELECT id,status FROM buttons")
+
+ results = []
+ while True:
+ try:
+ button = Button()
+ button.loadFromDb(db.fetchone())
+ results.append(button.toDict())
+ except:
+ break
+ return results
+
+class Button(object):
def __init__(self):
+ self.persisted = False
self.id = None
- self.status = 0
+ self.status = STATUS_INVENTORY
+
+ def toDict(self):
+ return {
+ 'id': self.id,
+ 'status': self.status
+ }
- def GET(self, id):
- if not uuid.validate(id):
- return cherrypy.HTTPError(404)
+ def toJSON(self):
+ return json.dumps(self.toDict())
+ def loadById(self, id):
db = database.connect()
db.execute("SELECT id,status FROM buttons WHERE id = ?" , (str(id),))
- return db.fetchone()
-# return json.dumps({
-# 'id': self.id,
-# 'status': self.status
-# })
- def POST(self):
- return 'POST BIUTTON'
+ return self.loadFromDb(db.fetchone())
+
+ def loadFromDb(self, dbrs):
+ if dbrs is None:
+ raise cherrypy.HTTPError(404, 'Button not found')
+
+ self.persisted = True
+ self.id, self.status = dbrs
+
+ return self
+
+ def save(self):
+ cherrypy.log('SAVE {} -> {} (persisted: {})'.format(
+ self.id, self.status, self.persisted
+ ))
+ db = database.connect()
+
+ # validate
+ if self.id is None:
+ raise cherrypy.HTTPError(500, "What u doin?")
- def PUT(self, id):
- if uuid.validate(id):
- return self.GET(id)
+ # 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),)
+ )
- raise cherrypy.HTTPError(400, 'Invalid UUID')
+ # upsert: insert
+ else:
+ cherrypy.log('INSERT {} -> {} (persisted: {})'.format(
+ self.id, self.status, self.persisted
+ ))
+ db.execute("INSERT INTO buttons VALUES (?, ?)", (self.id, self.status))
- def DELETE(self):
- return 'DELETE BUTTON'
+ db.connection.commit()
+ return self
diff --git a/server/buttonDispatcher.py b/server/buttonDispatcher.py
new file mode 100644
index 0000000..5dac61c
--- /dev/null
+++ b/server/buttonDispatcher.py
@@ -0,0 +1,38 @@
+import json
+import cherrypy
+from button import loadAll, Button
+from lib import uuid
+
+@cherrypy.expose
+class ButtonDispatcher(object):
+ def GET(self, id=None):
+ 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')
+
+ def POST(self, id, status):
+ if not uuid.validate(id):
+ raise cherrypy.HTTPError(401)
+
+ button = Button()
+ button.loadById(id)
+
+ cherrypy.log("Updating status to " + str(status))
+ button.status = status
+ button.save()
+
+ return button.toJSON()
+
+ def PUT(self):
+ cherrypy.log('Generating new button')
+ button = Button()
+ button.id = uuid.gen()
+
+ return button.save().toJSON()
+
+ def DELETE(self):
+ return 'DELETE BUTTON'
diff --git a/server/lib/uuid.py b/server/lib/uuid.py
index 9558f21..e77e3a8 100644
--- a/server/lib/uuid.py
+++ b/server/lib/uuid.py
@@ -1,4 +1,4 @@
-from uuid import UUID
+from uuid import UUID, uuid4
def validate(uuid_to_test, version=4):
try:
@@ -7,3 +7,6 @@ def validate(uuid_to_test, version=4):
return False
return str(uuid_obj) == uuid_to_test
+
+def gen():
+ return str(uuid4())
diff --git a/server/server.py b/server/server.py
index 9ec12a1..da9ea17 100644
--- a/server/server.py
+++ b/server/server.py
@@ -9,10 +9,10 @@ class Root(object):
def index(self):
return 'Sup?'
-from button import Button
+from buttonDispatcher import ButtonDispatcher
cherrypy.tree.mount(Root())
-cherrypy.tree.mount(Button(), '/button', {
+cherrypy.tree.mount(ButtonDispatcher(), '/button', {
'/': {'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
})