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(Dispatcher): def GET(self, id=None): # GET /button - return all buttons if id is None: 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): # 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): button = Button() # 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() return self.response(True)