read config settings from 'bantracker.conf' so I don't have to edit the cgi script every bloody time I update it.
Also, handle errors more gracefully.
This commit is contained in:
parent
0556d87d01
commit
9d69828d44
|
@ -17,20 +17,59 @@ import os
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import urllib
|
import urllib
|
||||||
# This needs to be set to the location of the commoncgi.py file
|
import ConfigParser
|
||||||
sys.path.append('/var/www/bot')
|
|
||||||
from commoncgi import *
|
CONFIG_FILENAME = "bantracker.conf"
|
||||||
|
config = ConfigParser.RawConfigParser()
|
||||||
|
config.add_section('webpage')
|
||||||
|
|
||||||
|
# set default values
|
||||||
|
config.set('webpage', 'database', 'bans.db')
|
||||||
|
config.set('webpage', 'results_per_page', '100')
|
||||||
|
config.set('webpage', 'anonymous_access', 'True')
|
||||||
|
config.set('webpage', 'PLUGIN_PATH', '')
|
||||||
|
|
||||||
|
try:
|
||||||
|
config.readfp(open(CONFIG_FILENAME))
|
||||||
|
except IOError:
|
||||||
|
config.write(open(CONFIG_FILENAME, 'w'))
|
||||||
|
|
||||||
|
# This needs to be set to the location of the commoncgi.py file
|
||||||
|
PLUGIN_PATH = config.get('webpage', 'PLUGIN_PATH')
|
||||||
|
if PLUGIN_PATH:
|
||||||
|
sys.path.append(PLUGIN_PATH)
|
||||||
|
|
||||||
|
try:
|
||||||
|
from commoncgi import *
|
||||||
|
except:
|
||||||
|
print "Content-Type: text/html"
|
||||||
|
print
|
||||||
|
print "<p>Failed to load the module commoncgi</p>"
|
||||||
|
print "<p>Check that the config option PLUGIN_PATH in '%s' is correct.</p>" % CONFIG_FILENAME
|
||||||
|
sys.exit(-1)
|
||||||
|
|
||||||
|
db = config.get('webpage', 'database')
|
||||||
|
num_per_page = config.getint('webpage', 'results_per_page')
|
||||||
|
anonymous_access = config.getboolean('webpage', 'anonymous_access')
|
||||||
|
|
||||||
### Variables
|
|
||||||
db = '/home/bot/data/bans.db'
|
|
||||||
num_per_page = 100
|
|
||||||
pagename = os.path.basename(sys.argv[0])
|
pagename = os.path.basename(sys.argv[0])
|
||||||
disable_anonymous = False # Set this to True to disable anonymous access
|
|
||||||
|
|
||||||
t1 = time.time()
|
t1 = time.time()
|
||||||
|
|
||||||
con = sqlite.connect(db)
|
try:
|
||||||
cur = con.cursor()
|
con = sqlite.connect(db)
|
||||||
|
cur = con.cursor()
|
||||||
|
except sqlite.DatabaseError:
|
||||||
|
print "Unable to connect to to database '%s'" % db
|
||||||
|
send_page('bans.tmpl')
|
||||||
|
|
||||||
|
def db_execute(query, args):
|
||||||
|
try:
|
||||||
|
cur.execute(query, args)
|
||||||
|
return cur
|
||||||
|
except sqlite.OperationalError:
|
||||||
|
print "The database is locked, wait a bit and try again."
|
||||||
|
send_page('bans.tmpl')
|
||||||
|
|
||||||
# Login check
|
# Login check
|
||||||
error = ''
|
error = ''
|
||||||
|
@ -55,11 +94,10 @@ if cookie.has_key('sess'):
|
||||||
con.commit()
|
con.commit()
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if not user and disable_anonymous:
|
if not user and not anonymous_access:
|
||||||
print "Sorry, bantracker is not available for anonymous users<br />"
|
print "Sorry, bantracker is not available for anonymous users<br />"
|
||||||
print "Join <a href=irc://irc.freenode.net/ubuntu-ops>#ubuntu-ops</a> on irc.freenode.net to discuss bans"
|
print "Join <a href=irc://irc.freenode.net/ubuntu-ops>#ubuntu-ops</a> on irc.freenode.net to discuss bans"
|
||||||
send_page('bans.tmpl')
|
send_page('bans.tmpl')
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
def urlencode(**kwargs):
|
def urlencode(**kwargs):
|
||||||
"""Return the url options as a string, inserting additional ones if given."""
|
"""Return the url options as a string, inserting additional ones if given."""
|
||||||
|
@ -86,12 +124,7 @@ if form.has_key('log'):
|
||||||
regex = True
|
regex = True
|
||||||
regex_value = 'checked="checked"'
|
regex_value = 'checked="checked"'
|
||||||
|
|
||||||
con = sqlite.connect(db)
|
log = db_execute("SELECT log FROM bans WHERE id=%s", log_id).fetchall()
|
||||||
cur = con.cursor()
|
|
||||||
cur.execute("SELECT log FROM bans WHERE id=%s", log_id)
|
|
||||||
log = cur.fetchall()
|
|
||||||
con.commit()
|
|
||||||
con.close()
|
|
||||||
|
|
||||||
if not log or not log[0] or not log[0][0]:
|
if not log or not log[0] or not log[0][0]:
|
||||||
if plain:
|
if plain:
|
||||||
|
@ -296,13 +329,11 @@ def getBans(id=None, mask=None, kicks=True, oldbans=True, bans=True, floodbots=T
|
||||||
#print sql_count, "<br/>"
|
#print sql_count, "<br/>"
|
||||||
#print args, "<br/>"
|
#print args, "<br/>"
|
||||||
# Things seems faster if we do the query BEFORE counting. Due to caches probably.
|
# Things seems faster if we do the query BEFORE counting. Due to caches probably.
|
||||||
cur.execute(sql, args)
|
bans = db_execute(sql, args).fetchall()
|
||||||
bans = cur.fetchall()
|
|
||||||
count = None
|
count = None
|
||||||
if withCount:
|
if withCount:
|
||||||
sql_count = "SELECT count(*) FROM bans%s" % where
|
sql_count = "SELECT count(*) FROM bans%s" % where
|
||||||
cur.execute(sql_count, args)
|
count = int(db_execute(sql_count, args).fetchone()[0])
|
||||||
count = int(cur.fetchone()[0])
|
|
||||||
return bans, count
|
return bans, count
|
||||||
return bans
|
return bans
|
||||||
|
|
||||||
|
@ -391,7 +422,6 @@ else:
|
||||||
# nothign to show
|
# nothign to show
|
||||||
print '<div style="clear: both"></div>' # if I don't print this the page is messed up.
|
print '<div style="clear: both"></div>' # if I don't print this the page is messed up.
|
||||||
send_page('bans.tmpl')
|
send_page('bans.tmpl')
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
# Empty log div, will be filled with AJAX
|
# Empty log div, will be filled with AJAX
|
||||||
print '<div id="log" class="log"> </div>'
|
print '<div id="log" class="log"> </div>'
|
||||||
|
@ -460,7 +490,7 @@ for b in bans:
|
||||||
print '<tr class="bg2">'
|
print '<tr class="bg2">'
|
||||||
else:
|
else:
|
||||||
print "<tr>"
|
print "<tr>"
|
||||||
cur.execute('SELECT who, comment, time FROM comments WHERE ban_id = %d', (b[6],))
|
db_execute('SELECT who, comment, time FROM comments WHERE ban_id = %d', (b[6],))
|
||||||
comments = cur.fetchall()
|
comments = cur.fetchall()
|
||||||
if len(comments) == 0:
|
if len(comments) == 0:
|
||||||
print '<td colspan="5" class="comment">'
|
print '<td colspan="5" class="comment">'
|
||||||
|
|
Loading…
Reference in New Issue