diff options
| author | ngharo <nick@ngha.ro> | 2017-12-31 22:16:32 -0600 | 
|---|---|---|
| committer | ngharo <nick@ngha.ro> | 2017-12-31 22:16:32 -0600 | 
| commit | f920c7da0618fd9f4181c2c78ef054e324185355 (patch) | |
| tree | eba4a9f21fb8fd47e8a53f01e8a4dfae25a333e2 /server | |
| parent | 75d05326d067857d22474cc91d069003d7b64b17 (diff) | |
| download | imOk-www-f920c7da0618fd9f4181c2c78ef054e324185355.tar.xz imOk-www-f920c7da0618fd9f4181c2c78ef054e324185355.zip  | |
Makefile: compile Babel and Sass source files to index.*
Diffstat (limited to 'server')
| -rw-r--r-- | server/buttonDispatcher.py | 44 | ||||
| -rw-r--r-- | server/dispatcher.py | 4 | ||||
| -rw-r--r-- | server/server.py | 4 | 
3 files changed, 27 insertions, 25 deletions
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()  | 
