diff --git a/server/brmbar-server b/server/brmbar-server index 1bb0bc9..32f02d9 100755 --- a/server/brmbar-server +++ b/server/brmbar-server @@ -19,23 +19,32 @@ def code(code): LEN = 25 if match('^U[0-9]{4}$', code): userid = int(code[1:]) - # TODO: fetch credit and username + cur = g.db.execute('SELECT balance FROM balance WHERE userid = ?', (userid, )) + row = cur.fetchone() + if row: + usercredit = '%d Kc' % row[0] + else: + usercredit = '0 Kc' try: username = users[userid] - usercredit = '0 Kc' - r = username[:LEN - len(usercredit) - 1].ljust(LEN - len(usercredit) - 1) + ' ' + usercredit except: - r = 'Unknown'.ljust(LEN - 5) + ' - Kc' - return Response('%s %s' % (username, usercredit), content_type = 'text/plain') + username = 'Unknown' + r = username[:LEN - len(usercredit) - 1].ljust(LEN - len(usercredit) - 1) + ' ' + usercredit + return Response(r, content_type = 'text/plain') if code == 'R0000': return Response('Reset Credit to 0', content_type = 'text/plain') if match('^\C[0-9]{4}$', code): amount = int(code[1:]) return Response('Credit %d' % amount, content_type = 'text/plain') if match('^[0-9]+$', code): - # TODO: fetch item, show name and price - itemname = 'Unknown' - itemprice = '0 Kc' + cur = g.db.execute('SELECT name, price FROM items WHERE code = ?', (code, )) + row = cur.fetchone() + if row: + itemname = row[0] + itemprice = '%d Kc' % row[1] + else: + itemname = 'Unknown' + itemprice = '0 Kc' r = itemname[:LEN - len(itemprice) - 1].ljust(LEN - len(itemprice) - 1) + ' ' + itemprice return Response(r, content_type = 'text/plain') abort(400) @@ -45,10 +54,10 @@ def action(user, item): if not match('^U[0-9]{4}$', user): abort(400) if code == 'R0000': - # TODO: process - reset credit + g.db.execute('DELETE FROM balance WHERE userid = ?', (user, )) + g.db.commit() pass if match('^\C[0-9]{4}$', code): - amount = int(code[1:]) # TODO: process - add credit pass if match('^[0-9]+$', code): @@ -95,7 +104,9 @@ def barcodestxt(): @app.route('/log/') def log(): - return render_template('log.html') + cur = g.db.execute('SELECT ts, userid, name, price, code, event FROM log LEFT JOIN items ON log.itemid=items.id ORDER BY ts DESC') + rows = [dict(ts = row[0], user = users[row[1]], itemname = row[2], price = row[3], code = row[4], event = row[5]) for row in cur.fetchall()] + return render_template('log.html', rows = rows) @app.route('/stats/') def stats(): diff --git a/server/db/reset b/server/db/reset index b98ce62..89c6360 100755 --- a/server/db/reset +++ b/server/db/reset @@ -1,3 +1,4 @@ #!/bin/sh +rm -f brmbar.db sqlite3 brmbar.db < schema.sql sqlite3 brmbar.db < items.sql diff --git a/server/db/schema.sql b/server/db/schema.sql index f9ec992..41cca16 100644 --- a/server/db/schema.sql +++ b/server/db/schema.sql @@ -1,7 +1,27 @@ -DROP TABLE IF EXISTS items; CREATE TABLE items ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, price INTEGER NOT NULL, code TEXT NOT NULL ); + +CREATE INDEX items_name ON items ( name ); +CREATE INDEX items_price ON items ( price ); +CREATE UNIQUE INDEX items_code ON items ( code ); + +CREATE TABLE log ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + userid INTEGER NOT NULL, + itemid INTEGER, + event TEXT, + ts INTEGER NOT NULL +); + +CREATE INDEX log_userid ON log ( userid ); +CREATE INDEX log_itemid ON log ( userid ); +CREATE INDEX log_ts ON log ( ts ); + +CREATE TABLE balance ( + userid INTEGER PRIMARY KEY, + balance INTEGER NOT NULL +); diff --git a/server/templates/log.html b/server/templates/log.html index fd1c86b..a30d7bf 100644 --- a/server/templates/log.html +++ b/server/templates/log.html @@ -1,5 +1,18 @@ {% extends "base.html" %} {% block title %}Log{% endblock %} {% block content %} -log + +
date/time | user | item |
---|---|---|
{{ row.ts|safe }} | {{ row.user|safe }} |
+{%- if row.itemname -%}
+![]() |