brmbar-github/server/brmbar-server
2011-06-16 03:36:54 +02:00

84 lines
2.5 KiB
Python
Executable file

#!/usr/bin/python
from flask import Flask, render_template, Response
from re import match
from users import users
app = Flask('BrmBar')
@app.route('/')
def index():
return render_template('index.html')
@app.route('/code/<code>')
def code(code):
LEN = 25
if match('^U[0-9]{4}$', code):
userid = int(code[1:])
# TODO: fetch credit and username
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')
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'
r = itemname[:LEN - len(itemprice) - 1].ljust(LEN - len(itemprice) - 1) + ' ' + itemprice
return Response(r, content_type = 'text/plain')
abort(400)
@app.route('/action/<user>/<item>')
def action(user, item):
if not match('^U[0-9]{4}$', user):
abort(400)
if code == 'R0000':
# TODO: process - reset credit
pass
if match('^\C[0-9]{4}$', code):
amount = int(code[1:])
# TODO: process - add credit
pass
if match('^[0-9]+$', code):
# TODO: process - deduct item price
pass
return Response('OK', content_type = 'text/plain')
@app.route('/admin/')
def admin():
return render_template('admin.html')
@app.route('/admin/barcode-generator.txt')
def admin_barcodegeneratortxt():
ret = []
ret.append('Credit 20;C0020')
ret.append('Credit 50;C0050')
ret.append('Credit 100;C0100')
ret.append('Credit 200;C0200')
ret.append('Credit 500;C0500')
ret.append('Credit 1000;C1000')
ret.append('Credit 2000;C2000')
ret.append('RESET;R0000')
for (id,name) in sorted(users.iteritems(), key = lambda (k,v): (v,k)):
ret.append('%s;U%d' % (name, id))
response = Response(response = '\n'.join(ret) + '\n', content_type = 'text/plain')
return response
@app.route('/log/')
def log():
return render_template('log.html')
@app.route('/stats/')
def stats():
return render_template('stats.html')
if __name__ == '__main__':
app.run(host = '0.0.0.0', port = 45678)