now with log support

This commit is contained in:
Niels Serup 2012-03-22 16:21:56 +01:00
parent dcb7218ed4
commit b3ad96f817
4 changed files with 81 additions and 18 deletions

View File

@ -10,6 +10,7 @@
Alias /favicon.ico /home/niels/www/meta/subsites/lunchuman/favicon.ico Alias /favicon.ico /home/niels/www/meta/subsites/lunchuman/favicon.ico
Alias /favicon.png /home/niels/www/meta/subsites/lunchuman/favicon.png Alias /favicon.png /home/niels/www/meta/subsites/lunchuman/favicon.png
Alias /static /home/niels/www/meta/subsites/lunchuman/static Alias /static /home/niels/www/meta/subsites/lunchuman/static
Alias /media /home/niels/www/meta/subsites/lunchuman/media
Alias /lunchuman.wsgi /home/niels/www/meta/subsites/lunchuman/lunchuman.wsgi Alias /lunchuman.wsgi /home/niels/www/meta/subsites/lunchuman/lunchuman.wsgi
Alias /apache-config-template /home/niels/www/meta/subsites/lunchuman/apache-config-template Alias /apache-config-template /home/niels/www/meta/subsites/lunchuman/apache-config-template

View File

@ -1,3 +1,4 @@
/.dynamicsettings /.dynamicsettings
/.old /.old
/lunchuman.sqlite /lunchuman.sqlite
/media

View File

@ -1,11 +1,3 @@
<h2>Add user</h2>
<form action='/adduser' method='post'>
<p>Name: <input type='text' name='name' /></p>
<input type='submit' value='Submit' />
</form>
<h2>Update user data</h2> <h2>Update user data</h2>
<form action='/updateuserdata' method='post'> <form action='/updateuserdata' method='post'>
@ -26,6 +18,26 @@
</form> </form>
<h2>Add user</h2>
<form action='/adduser' method='post'>
<p>Name: <input type='text' name='name' /></p>
<input type='submit' value='Submit' />
</form>
<h2>Log</h2>
<pre>
{log}
</pre>
<h2>Image gallery</h2>
<a href='/media'>Datalosjen haz pics.</a>
<h2>FAQ</h2> <h2>FAQ</h2>
<p><strong>Question:</strong> What is one food?</p> <p><strong>Question:</strong> What is one food?</p>

View File

@ -26,6 +26,7 @@ import os
import bottle import bottle
from bottle import route, abort, error, request, redirect from bottle import route, abort, error, request, redirect
import sqlite3 import sqlite3
import datetime
import threading import threading
import itertools import itertools
import atexit import atexit
@ -74,6 +75,13 @@ name text,
moneys int, moneys int,
foods int foods int
) )
''')
self.cursor.execute('''
create table if not exists actions (
id integer primary key autoincrement,
action_when datetime,
action text
)
''') ''')
self.conn.commit() self.conn.commit()
@ -83,14 +91,51 @@ foods int
def add_user(self, name): def add_user(self, name):
self.cursor.execute('insert into users values (NULL, ?, 0, 0)', (name,)) self.cursor.execute('insert into users values (NULL, ?, 0, 0)', (name,))
self.log('new user created: {}'.format(repr(name)))
self.conn.commit() self.conn.commit()
def log(self, msg):
self.cursor.execute('insert into actions values (NULL, ?, ?)',
(datetime.datetime.utcnow(), msg))
self.conn.commit()
def get_log(self):
self.cursor.execute('select * from actions order by action_when desc')
return self.cursor.fetchall()
def update_user_data(self, *users): def update_user_data(self, *users):
log_msg = []
for id, new_name, moneys_incr, foods_incr in users: for id, new_name, moneys_incr, foods_incr in users:
try:
foods_incr = int(foods_incr)
except ValueError:
foods_incr = 0
try:
moneys_incr = int(moneys_incr)
except ValueError:
moneys_incr = 0
self.cursor.execute('select name from users where id=?', (id,))
old_name = self.cursor.fetchone()[0]
self.cursor.execute(''' self.cursor.execute('''
update users set name=?, moneys=moneys + (?), foods=foods + (?) update users set name=?, moneys=moneys + (?), foods=foods + (?)
where id=? where id=?
''', (new_name, moneys_incr, foods_incr, id)) ''', (new_name, moneys_incr, foods_incr, id))
loclog = []
if old_name != new_name:
loclog.append('change name to {}\n'.format(new_name))
if foods_incr > 0:
loclog.append('eat {} foods'.format(foods_incr))
elif foods_incr < 0:
loclog.append('uneat {} foods'.format(foods_incr))
if moneys_incr > 0:
loclog.append('spend {} moneys'.format(moneys_incr))
elif moneys_incr < 0:
loclog.append('unspend {} moneys'.format(moneys_incr))
if loclog:
log_msg.append('{} do:\n'.format(old_name)
+ ' ' + '\n '.join(loclog))
if log_msg:
self.log('\n'.join(log_msg))
self.conn.commit() self.conn.commit()
def add_settings_from_file(fname, settings): def add_settings_from_file(fname, settings):
@ -160,7 +205,8 @@ def frontpage():
users[i][2] / users[i][3]) if users[i][3] > 0 users[i][2] / users[i][3]) if users[i][3] > 0
else None)) else None))
for i in range(len(users))) for i in range(len(users)))
return _frontpage.format(userdata=userdata) log = '\n\n'.join('{} UTC:\n{}'.format(*row[1:]) for row in db.get_log())
return _frontpage.format(userdata=userdata, log=log)
@route('/adduser', method='post') @route('/adduser', method='post')
def add_user(): def add_user():
@ -169,14 +215,17 @@ def add_user():
@route('/updateuserdata', method='post') @route('/updateuserdata', method='post')
def update_user_data(): def update_user_data():
users = (vals[1:] for vals in try:
filter(lambda vals: vals[0] != vals[2] or users = (vals[1:] for vals in
(vals[3] != '0' and vals[3] != '') or filter(lambda vals: vals[0] != vals[2] or
(vals[4] != '0' and vals[4] != ''), (vals[3] != '0' and vals[3] != '') or
zip(*(get_seq_input(request.forms, attr) (vals[4] != '0' and vals[4] != ''),
for attr in ('orig_name', 'id', 'new_name', zip(*(get_seq_input(request.forms, attr)
'moneys_incr', 'foods_incr'))))) for attr in ('orig_name', 'id', 'new_name',
db.update_user_data(*users) 'moneys_incr', 'foods_incr')))))
db.update_user_data(*users)
except Exception:
traceback.print_exc()
redirect('/') redirect('/')