finish server

This commit is contained in:
Pavol Rusnak 2011-06-21 00:40:42 +02:00
parent 4dfa0ec3b0
commit 05aab85b5a
4 changed files with 46 additions and 22 deletions

View file

@ -10,8 +10,8 @@ app = Flask('BrmBar')
@app.route('/') @app.route('/')
def index(): def index():
cur = g.db.execute('SELECT id, code, name, price FROM items ORDER BY name') cur = g.db.execute('SELECT code, name, price FROM items ORDER BY name')
items = [dict(id = row[0], code = row[1], name = row[2], price = row[3]) for row in cur.fetchall()] items = [dict(code = row[0], name = row[1], price = row[2]) for row in cur.fetchall()]
return render_template('index.html', items = items) return render_template('index.html', items = items)
@app.route('/code/<code>') @app.route('/code/<code>')
@ -53,22 +53,47 @@ def code(code):
def action(user, item): def action(user, item):
if not match('^U[0-9]{4}$', user): if not match('^U[0-9]{4}$', user):
abort(400) abort(400)
if code == 'R0000': user = int(user[1:])
if item == 'R0000':
g.db.execute('DELETE FROM balance WHERE userid = ?', (user, )) g.db.execute('DELETE FROM balance WHERE userid = ?', (user, ))
g.db.execute('INSERT INTO log(userid, event) VALUES(?, ?)', (user, 'Credit reset'))
g.db.commit() g.db.commit()
pass if match('^C[0-9]{4}$', item):
if match('^\C[0-9]{4}$', code): cur = g.db.execute('SELECT balance FROM balance WHERE userid = ?', (user, ))
# TODO: process - add credit row = cur.fetchone()
pass if row:
if match('^[0-9]+$', code): balance = int(row[0]) + int(item[1:])
# TODO: process - deduct item price g.db.execute('UPDATE balance SET balance = ? WHERE userid = ?', (balance, user))
pass g.db.execute('INSERT INTO log(userid, event) VALUES(?, ?)', (user, 'Credit +%s' % item[1:]))
g.db.commit()
else:
balance = int(item[1:])
g.db.execute('INSERT INTO balance(balance, userid) VALUES(?, ?)', (balance, user))
g.db.execute('INSERT INTO log(userid, event) VALUES(?, ?)', (user, 'Credit +%s' % item[1:]))
g.db.commit()
if match('^[0-9]+$', item):
cur1 = g.db.execute('SELECT price FROM items WHERE code = ?', (item, ))
row1 = cur1.fetchone()
cur2 = g.db.execute('SELECT balance FROM balance WHERE userid = ?', (user, ))
row2 = cur2.fetchone()
if not row1:
abort(400)
if row2:
balance = - int(row1[0]) + int(row2[0])
g.db.execute('UPDATE balance SET balance = ? WHERE userid = ?', (balance, user))
g.db.execute('INSERT INTO log(userid, itemcode) VALUES(?, ?)', (user, item))
g.db.commit()
else:
balance = - int(row1[0])
g.db.execute('INSERT INTO balance(balance, userid) VALUES(?, ?)', (balance, user))
g.db.execute('INSERT INTO log(userid, itemcode) VALUES(?, ?)', (user, item))
g.db.commit()
return Response('OK', content_type = 'text/plain') return Response('OK', content_type = 'text/plain')
@app.route('/items/') @app.route('/items/')
def items(): def items():
cur = g.db.execute('SELECT id, code, name, price FROM items ORDER BY name') cur = g.db.execute('SELECT code, name, price FROM items ORDER BY name')
items = [dict(id = row[0], code = row[1], name = row[2], price = row[3]) for row in cur.fetchall()] items = [dict(code = row[0], name = row[1], price = row[2]) for row in cur.fetchall()]
return render_template('items.html', items = items) return render_template('items.html', items = items)
@app.route('/items/', methods=['POST']) @app.route('/items/', methods=['POST'])
@ -80,8 +105,8 @@ def items_post():
g.db.commit() g.db.commit()
return redirect('/items/') return redirect('/items/')
if f['action'].startswith('delete:'): if f['action'].startswith('delete:'):
id = int(f['action'][7:]) code = int(f['action'][7:])
g.db.execute('DELETE FROM items WHERE id = ?', (id, )) g.db.execute('DELETE FROM items WHERE code = ?', (code, ))
g.db.commit() g.db.commit()
return redirect('/items/') return redirect('/items/')
abort(400) abort(400)
@ -104,7 +129,7 @@ def barcodestxt():
@app.route('/log/') @app.route('/log/')
def log(): def log():
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') cur = g.db.execute('SELECT ts, userid, name, price, code, event FROM log LEFT JOIN items ON log.itemcode = items.code 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()] 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) return render_template('log.html', rows = rows)

View file

@ -1,8 +1,7 @@
CREATE TABLE items ( CREATE TABLE items (
id INTEGER PRIMARY KEY AUTOINCREMENT, code TEXT NOT NULL PRIMARY KEY,
name TEXT NOT NULL, name TEXT NOT NULL,
price INTEGER NOT NULL, price INTEGER NOT NULL,
code TEXT NOT NULL
); );
CREATE INDEX items_name ON items ( name ); CREATE INDEX items_name ON items ( name );
@ -12,9 +11,9 @@ CREATE UNIQUE INDEX items_code ON items ( code );
CREATE TABLE log ( CREATE TABLE log (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
userid INTEGER NOT NULL, userid INTEGER NOT NULL,
itemid INTEGER, itemcode INTEGER,
event TEXT, event TEXT,
ts INTEGER NOT NULL ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP
); );
CREATE INDEX log_userid ON log ( userid ); CREATE INDEX log_userid ON log ( userid );

View file

@ -7,7 +7,7 @@
<tr><th>image</th><th>code</th><th>item</th><th>price</th><th>action</th></tr> <tr><th>image</th><th>code</th><th>item</th><th>price</th><th>action</th></tr>
<tr><td></td><td><input name="code" size="14"/></td><td><input name="name" size="30"/></td><td><input name="price" size="2"/></td><td><input type="submit" name="action" value="add"/></tr> <tr><td></td><td><input name="code" size="14"/></td><td><input name="name" size="30"/></td><td><input name="price" size="2"/></td><td><input type="submit" name="action" value="add"/></tr>
{% for item in items %} {% for item in items %}
<tr><td><img src="/static/items/{{ item.code|safe }}.jpg" width="64" height="64"/></td><td>{{ item.code|safe }}</td><td>{{ item.name|safe }}</td><td>{{ item.price }}</td><td><input type="submit" name="action" value="delete:{{ item.id }}"/></tr> <tr><td><img src="/static/items/{{ item.code|safe }}.jpg" width="64" height="64"/></td><td>{{ item.code|safe }}</td><td>{{ item.name|safe }}</td><td>{{ item.price }}</td><td><input type="submit" name="action" value="delete:{{ item.code }}"/></tr>
{% endfor %} {% endfor %}
</table> </table>
</form> </form>

View file

@ -5,9 +5,9 @@
<table> <table>
<tr><th>date/time</th><th>user</th><th>item</th></tr> <tr><th>date/time</th><th>user</th><th>item</th></tr>
{% for row in rows %} {% for row in rows %}
<tr><td>{{ row.ts|safe }}</td><td></td>{{ row.user|safe }}</td><td> <tr><td>{{ row.ts|safe }}</td><td>{{ row.user|safe }}</td><td>
{%- if row.itemname -%} {%- if row.itemname -%}
<img src="/static/items/{{ row.code|safe }}.jpg" width="64" height="64"/> {{ row.itemname|safe }} - {{ row.price }} Kc <img src="/static/items/{{ row.code|safe }}.jpg" width="32" height="32"/> {{ row.itemname|safe }} - {{ row.price }} Kc
{%- else -%} {%- else -%}
{{- row.event -}} {{- row.event -}}
{%- endif -%} {%- endif -%}