summaryrefslogtreecommitdiff
path: root/server/buttonDispatcher.py
blob: d3331bd197fe89a5fbf498e1757b1ebef1191fdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import json
from bottle import Bottle, route
from button import loadAll, Button
from dispatcher import Dispatcher
from lib import uuid

# Tweak: consider using serpy for serializing objects for JSON.

@app.route('/')
class ButtonDispatcher(Dispatcher):
    @cherrypy.tools.json_out()
    def GET(self, id=None):
        # GET /button - return all buttons
        if id is None:
            return loadAll()

        # GET /button/{id} - return single button
        self.validate_uuid(id)

        button = Button()
        return button.loadById(id).toDict()

    @cherrypy.tools.json_out()
    def POST(self, id=None):
        button = Button()

        # PUT /button - creates button
        if id is None:
            button.id = uuid.gen() 
            return button.save().toDict()

        # PUT /button/{id} - presses button
        else:
            button.loadById(id)
            button.press()

        button.save()

        return button.toDict()

    @cherrypy.tools.json_out()
    def PUT(self, id):
        # 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, status, *path, **args):
        # DELETE /button/{id} - deletes button
        self.validate_uuid(id)

        button = Button()
        button.loadById(id)
        button.delete()

        return True