From f920c7da0618fd9f4181c2c78ef054e324185355 Mon Sep 17 00:00:00 2001 From: ngharo Date: Sun, 31 Dec 2017 22:16:32 -0600 Subject: Makefile: compile Babel and Sass source files to index.* --- server/buttonDispatcher.py | 44 ++++++++++++++++++++++++-------------------- server/dispatcher.py | 4 ---- server/server.py | 4 +++- 3 files changed, 27 insertions(+), 25 deletions(-) (limited to 'server') diff --git a/server/buttonDispatcher.py b/server/buttonDispatcher.py index 59672d6..8938288 100644 --- a/server/buttonDispatcher.py +++ b/server/buttonDispatcher.py @@ -8,38 +8,26 @@ from lib import uuid @cherrypy.expose class ButtonDispatcher(Dispatcher): + @cherrypy.tools.json_out() def GET(self, id=None): # GET /button - return all buttons if id is None: - return self.response(loadAll()) + return loadAll() # GET /button/{id} - return single button self.validate_uuid(id) button = Button() - return self.response(button.loadById(id).toJSON()) + return button.loadById(id).toDict() - def POST(self, id, status): - # POST /button/{id}?status={} - updates button status - self.validate_uuid(id) - - button = Button() - button.loadById(id) - - cherrypy.log("Updating status to " + str(status)) - # TODO validate status - button.status = int(status) - button.save() - - return self.response(button.toJSON()) - - def PUT(self, id=None): + @cherrypy.tools.json_out() + def POST(self, id=None): button = Button() # PUT /button - creates button if id is None: button.id = uuid.gen() - return self.response(button.save().toJSON()) + return button.save().toDict() # PUT /button/{id} - presses button else: @@ -48,12 +36,28 @@ class ButtonDispatcher(Dispatcher): button.save() - return self.response(button.toJSON()) + return button.toDict() + + @cherrypy.tools.json_out() + def PUT(self, id, status): + # POST /button/{id}?status={} - updates button status + self.validate_uuid(id) + + button = Button() + button.loadById(id) + + cherrypy.log("Updating status to " + str(status)) + # TODO validate status + button.status = int(status) + button.save() + + return button.toDict() + @cherrypy.tools.json_out() def DELETE(self, id): # DELETE /button/{id} - deletes button button = Button() button.loadById(id) button.delete() - return self.response(True) + return True diff --git a/server/dispatcher.py b/server/dispatcher.py index 36dd8da..26216ae 100644 --- a/server/dispatcher.py +++ b/server/dispatcher.py @@ -7,10 +7,6 @@ class Dispatcher(object): 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)}}) diff --git a/server/server.py b/server/server.py index da9ea17..5a16cdc 100644 --- a/server/server.py +++ b/server/server.py @@ -13,7 +13,9 @@ from buttonDispatcher import ButtonDispatcher cherrypy.tree.mount(Root()) cherrypy.tree.mount(ButtonDispatcher(), '/button', { - '/': {'request.dispatch': cherrypy.dispatch.MethodDispatcher()} + '/': { + 'request.dispatch': cherrypy.dispatch.MethodDispatcher() + } }) cherrypy.engine.start() -- cgit v1.2.3