Convert web/archive.cgi to use the new SQL timestamps.

The Archivist has replaced UNIX timestamps with python datetime objects
derived from SQL timestamps.  There are still a lot of struct_time
objects which should be converted too.
This commit is contained in:
John "Elwin" Edwards 2012-07-26 08:07:00 -07:00
parent d7d87a3a91
commit 5eacaecbcb

View file

@ -6,6 +6,7 @@ import sys
import re import re
import time import time
import calendar import calendar
from datetime import datetime
import rlgalldb as rlgall import rlgalldb as rlgall
import cgitb import cgitb
@ -19,18 +20,6 @@ ttyrecbase = "/var/dgl/dgldir/ttyrec/"
recre = r"(\d{4,4})-(\d{2,2})-(\d{2,2})\.(\d{2,2}):(\d{2,2}):(\d{2,2})\.ttyrec" recre = r"(\d{4,4})-(\d{2,2})-(\d{2,2})\.(\d{2,2}):(\d{2,2}):(\d{2,2})\.ttyrec"
recrec = re.compile(recre) recrec = re.compile(recre)
def name_in_time(filename, timerange):
"Checks whether filename is a ttyrec with time between Unix times \
timerange[0] and timerange[1]."
nmatch = recrec.match(filename)
if not nmatch:
return False
ntime = calendar.timegm([int(val) for val in nmatch.groups()])
if ntime > timerange[0] and ntime <= timerange[1]:
return True
else:
return False
def input_game(outf, selected=None): def input_game(outf, selected=None):
"Prints the form components for selecting a game." "Prints the form components for selecting a game."
selstr = '<option label="{0}" value="{1}" selected="selected">{0}</option>\n' selstr = '<option label="{0}" value="{1}" selected="selected">{0}</option>\n'
@ -153,9 +142,9 @@ def processgame(fdata, errlist):
return None return None
def processtime(fdata, errlist, hlist): def processtime(fdata, errlist, hlist):
"Takes a CGI data object and converts to a Unix timestamp by finding fields \ "Takes a CGI data object and converts to a datetime object by finding \
called year, month, etc. Any errors get appended to errlist. hlist \ fields called year, month, etc. Any errors get appended to errlist. \
should contain 6 components, for ymd-hms fields." hlist should contain 6 components, for ymd-hms fields."
# Timestamp overrides human-readable, even if it's invalid. # Timestamp overrides human-readable, even if it's invalid.
badtime = 'The time field is for Unix clocks, which never say anything \ badtime = 'The time field is for Unix clocks, which never say anything \
@ -175,7 +164,7 @@ def processtime(fdata, errlist, hlist):
chtime = time.gmtime(utime) chtime = time.gmtime(utime)
for i in range(6): for i in range(6):
hlist[i] = chtime[i] hlist[i] = chtime[i]
return utime return datetime.fromtimestamp(utime, rlgall.utc)
# Now try to get a human-readable specification. # Now try to get a human-readable specification.
lerrors = [] lerrors = []
@ -271,7 +260,8 @@ def processtime(fdata, errlist, hlist):
if lerrors: if lerrors:
errlist.extend(lerrors) errlist.extend(lerrors)
return None return None
return calendar.timegm([year, month, day, hour, minute, second, 0, 0, 0]) #return calendar.timegm([year, month, day, hour, minute, second, 0, 0, 0])
return datetime(year, month, day, hour, minute, second, 0, rlgall.utc)
# Begin processing # Begin processing
fdata = cgi.FieldStorage() fdata = cgi.FieldStorage()
@ -281,38 +271,37 @@ fdata = cgi.FieldStorage()
isnotsearch = checkempty(fdata) isnotsearch = checkempty(fdata)
errors = [] errors = []
formname = dungeon = utime = None formname = dungeon = searchtime = None
timepieces = [None, None, None, None, None, None] timepieces = [None, None, None, None, None, None]
if not isnotsearch: if not isnotsearch:
formname = processname(fdata, errors) formname = processname(fdata, errors)
dungeon = processgame(fdata, errors) dungeon = processgame(fdata, errors)
utime = processtime(fdata, errors, timepieces) searchtime = processtime(fdata, errors, timepieces)
dosearch = formname != None and dungeon != None and utime != None dosearch = formname != None and dungeon != None and searchtime != None
# Find the actual files, and put them in a list called gamefiles. # Find the actual files, and put them in a list called gamefiles.
gtimes = [0, int(time.time())] gtimes = [0, int(time.time())]
relgame = None relgame = None
gamefiles = [] gamefiles = []
if dosearch: if dosearch:
ttyrecdir = "{0}/{1}/{2}/".format(ttyrecbase, formname, dungeon.uname) query1 = "SELECT ttyrecs FROM {0} WHERE name = %s AND startt <= %s AND endt >= %s;".format(dungeon.uname)
query1 = "SELECT ttyrecs FROM {0} WHERE name = %s AND stime <= %s AND etime >= %s;".format(dungeon.uname) query2 = "SELECT ttyrecs FROM {0} WHERE name = %s AND endt >= %s ORDER BY endt LIMIT 1;".format(dungeon.uname)
query2 = "SELECT ttyrecs FROM {0} WHERE name = %s AND etime >= %s ORDER BY etime LIMIT 1;".format(dungeon.uname) query3 = "SELECT ttyrecs FROM {0} WHERE name = %s AND startt <= %s ORDER BY startt DESC LIMIT 1;".format(dungeon.uname)
query3 = "SELECT ttyrecs FROM {0} WHERE name = %s AND stime <= %s ORDER BY stime DESC LIMIT 1;".format(dungeon.uname)
conn = rlgall.getconn() conn = rlgall.getconn()
cur = conn.cursor() cur = conn.cursor()
cur.execute(query1, [formname, utime, utime]) cur.execute(query1, [formname, searchtime, searchtime])
result = cur.fetchone() result = cur.fetchone()
if result: if result:
gamefiles = result[0] gamefiles = result[0]
else: else:
cur.execute(query2, [formname, utime]) cur.execute(query2, [formname, searchtime])
result = cur.fetchone() result = cur.fetchone()
if result: if result:
gamefiles = result[0] gamefiles = result[0]
else: else:
cur.execute(query3, [formname, utime]) cur.execute(query3, [formname, searchtime])
result = cur.fetchone() result = cur.fetchone()
if result: if result:
gamefiles = result[0] gamefiles = result[0]
@ -331,7 +320,7 @@ sys.stdout.write(rlgall.pti.format("Guild Archives"))
if dosearch: if dosearch:
sys.stdout.write("<p>Expedition by {0} to {1} about {2}:</p>\n".format( sys.stdout.write("<p>Expedition by {0} to {1} about {2}:</p>\n".format(
rlgall.playerlink(formname), dungeon.name, rlgall.playerlink(formname), dungeon.name,
time.strftime("%Y/%m/%d %H:%M:%S", time.gmtime(utime)))) searchtime.strftime("%Y/%m/%d %H:%M:%S")))
if not gamefiles: if not gamefiles:
sys.stdout.write("<p>No record found.</p>\n") sys.stdout.write("<p>No record found.</p>\n")
elif len(gamefiles) == 1: elif len(gamefiles) == 1:
@ -353,7 +342,7 @@ else:
sys.stdout.write(errmsg + " ") sys.stdout.write(errmsg + " ")
sys.stdout.write('</p>\n') sys.stdout.write('</p>\n')
# Print out a search form, whether a search was done or not. # Print out a search form, whether a search was done or not.
sys.stdout.write('<form action="/archivedb.cgi" method="get">\n') sys.stdout.write('<form action="/archive.cgi" method="get">\n')
if dungeon != None: if dungeon != None:
input_game(sys.stdout, dungeon.uname) input_game(sys.stdout, dungeon.uname)
else: else: