From 75d05326d067857d22474cc91d069003d7b64b17 Mon Sep 17 00:00:00 2001 From: ngharo Date: Fri, 29 Dec 2017 00:02:05 -0600 Subject: VueJS front-end work in progress --- server/buttonDispatcher.py | 55 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 17 deletions(-) (limited to 'server/buttonDispatcher.py') 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) -- cgit v1.2.3