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/button.py | 45 ++++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) (limited to 'server/button.py') diff --git a/server/button.py b/server/button.py index a5b52b8..1e79629 100644 --- a/server/button.py +++ b/server/button.py @@ -3,6 +3,7 @@ import sqlite3 import json from lib import uuid from lib import database +from time import time STATUS_INVENTORY = 0 STATUS_ACTIVE = 1 @@ -12,7 +13,7 @@ STATUS_DEAD = 4 def loadAll(): db = database.connect() - db.execute("SELECT id,status FROM buttons") + db.execute("SELECT id,status,last_pressed FROM buttons") results = [] while True: @@ -30,19 +31,22 @@ class Button(object): self.persisted = False self.id = None self.status = STATUS_INVENTORY + self.last_pressed = 0 + self.history = [] def toDict(self): return { 'id': self.id, - 'status': self.status + 'status': self.status, + 'last_pressed': self.last_pressed } def toJSON(self): - return json.dumps(self.toDict()) + return self.toDict() def loadById(self, id): db = database.connect() - db.execute("SELECT id,status FROM buttons WHERE id = ?" , (str(id),)) + db.execute("SELECT id,status,last_pressed FROM buttons WHERE id = ?" , (str(id),)) return self.loadFromDb(db.fetchone()) @@ -51,14 +55,18 @@ class Button(object): raise cherrypy.HTTPError(404, 'Button not found') self.persisted = True - self.id, self.status = dbrs + self.id, self.status, self.last_pressed = dbrs return self + def press(self): + if self.last_pressed: + self.history.insert(0, self.last_pressed) + + self.last_pressed = int(time()) + return self + def save(self): - cherrypy.log('SAVE {} -> {} (persisted: {})'.format( - self.id, self.status, self.persisted - )) db = database.connect() # validate @@ -67,17 +75,24 @@ class Button(object): # upsert: update elif self.persisted: - cherrypy.log("setting other status to " + str(self.status)) - db.execute("UPDATE buttons SET status = ? WHERE id = ?", - (int(self.status), str(self.id),) + db.execute("UPDATE buttons SET status = ?, last_pressed = ? WHERE id = ?", + (self.status, self.last_pressed, self.id,) ) # upsert: insert else: - cherrypy.log('INSERT {} -> {} (persisted: {})'.format( - self.id, self.status, self.persisted - )) - db.execute("INSERT INTO buttons VALUES (?, ?)", (self.id, self.status)) + db.execute("INSERT INTO buttons VALUES (?, ?, ?)", (self.id, self.status, self.last_pressed)) db.connection.commit() return self + + def delete(self): + db = database.connect() + + if self.id is None: + raise cherrypy.HTTPError(500, "uhh wat") + + db.execute("DELETE FROM buttons WHERE id = ?", (self.id,)) + db.connection.commit() + + return None -- cgit v1.2.3