comparison web/archive.cgi @ 2:8f49df4074d7

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.
author John "Elwin" Edwards <elwin@sdf.org>
date Thu, 26 Jul 2012 08:07:00 -0700
parents 5ba2123d2c20
children a943cfdfbad9
comparison
equal deleted inserted replaced
1:def7fecbd437 2:8f49df4074d7
4 import os 4 import os
5 import sys 5 import sys
6 import re 6 import re
7 import time 7 import time
8 import calendar 8 import calendar
9 from datetime import datetime
9 import rlgalldb as rlgall 10 import rlgalldb as rlgall
10 import cgitb 11 import cgitb
11 12
12 cgitb.enable() 13 cgitb.enable()
13 14
16 """ 17 """
17 18
18 ttyrecbase = "/var/dgl/dgldir/ttyrec/" 19 ttyrecbase = "/var/dgl/dgldir/ttyrec/"
19 recre = r"(\d{4,4})-(\d{2,2})-(\d{2,2})\.(\d{2,2}):(\d{2,2}):(\d{2,2})\.ttyrec" 20 recre = r"(\d{4,4})-(\d{2,2})-(\d{2,2})\.(\d{2,2}):(\d{2,2}):(\d{2,2})\.ttyrec"
20 recrec = re.compile(recre) 21 recrec = re.compile(recre)
21
22 def name_in_time(filename, timerange):
23 "Checks whether filename is a ttyrec with time between Unix times \
24 timerange[0] and timerange[1]."
25 nmatch = recrec.match(filename)
26 if not nmatch:
27 return False
28 ntime = calendar.timegm([int(val) for val in nmatch.groups()])
29 if ntime > timerange[0] and ntime <= timerange[1]:
30 return True
31 else:
32 return False
33 22
34 def input_game(outf, selected=None): 23 def input_game(outf, selected=None):
35 "Prints the form components for selecting a game." 24 "Prints the form components for selecting a game."
36 selstr = '<option label="{0}" value="{1}" selected="selected">{0}</option>\n' 25 selstr = '<option label="{0}" value="{1}" selected="selected">{0}</option>\n'
37 unselstr = '<option label="{0}" value="{1}">{0}</option>\n' 26 unselstr = '<option label="{0}" value="{1}">{0}</option>\n'
151 return agame 140 return agame
152 errlist.append(cantfind.format(cgi.escape(formgame))) 141 errlist.append(cantfind.format(cgi.escape(formgame)))
153 return None 142 return None
154 143
155 def processtime(fdata, errlist, hlist): 144 def processtime(fdata, errlist, hlist):
156 "Takes a CGI data object and converts to a Unix timestamp by finding fields \ 145 "Takes a CGI data object and converts to a datetime object by finding \
157 called year, month, etc. Any errors get appended to errlist. hlist \ 146 fields called year, month, etc. Any errors get appended to errlist. \
158 should contain 6 components, for ymd-hms fields." 147 hlist should contain 6 components, for ymd-hms fields."
159 148
160 # Timestamp overrides human-readable, even if it's invalid. 149 # Timestamp overrides human-readable, even if it's invalid.
161 badtime = 'The time field is for Unix clocks, which never say anything \ 150 badtime = 'The time field is for Unix clocks, which never say anything \
162 like {0}. Try the form instead.' 151 like {0}. Try the form instead.'
163 utime = None 152 utime = None
173 utime = 0 162 utime = 0
174 if utime != None: 163 if utime != None:
175 chtime = time.gmtime(utime) 164 chtime = time.gmtime(utime)
176 for i in range(6): 165 for i in range(6):
177 hlist[i] = chtime[i] 166 hlist[i] = chtime[i]
178 return utime 167 return datetime.fromtimestamp(utime, rlgall.utc)
179 168
180 # Now try to get a human-readable specification. 169 # Now try to get a human-readable specification.
181 lerrors = [] 170 lerrors = []
182 year = month = day = hour = minute = second = None 171 year = month = day = hour = minute = second = None
183 fyear = fdata.getfirst("year") 172 fyear = fdata.getfirst("year")
269 else: 258 else:
270 hlist[5] = second 259 hlist[5] = second
271 if lerrors: 260 if lerrors:
272 errlist.extend(lerrors) 261 errlist.extend(lerrors)
273 return None 262 return None
274 return calendar.timegm([year, month, day, hour, minute, second, 0, 0, 0]) 263 #return calendar.timegm([year, month, day, hour, minute, second, 0, 0, 0])
264 return datetime(year, month, day, hour, minute, second, 0, rlgall.utc)
275 265
276 # Begin processing 266 # Begin processing
277 fdata = cgi.FieldStorage() 267 fdata = cgi.FieldStorage()
278 268
279 # If this is true, the user didn't supply any search criteria, so assume it 269 # If this is true, the user didn't supply any search criteria, so assume it
280 # doesn't want a search. Otherwise, every error or omission must be printed. 270 # doesn't want a search. Otherwise, every error or omission must be printed.
281 isnotsearch = checkempty(fdata) 271 isnotsearch = checkempty(fdata)
282 272
283 errors = [] 273 errors = []
284 formname = dungeon = utime = None 274 formname = dungeon = searchtime = None
285 timepieces = [None, None, None, None, None, None] 275 timepieces = [None, None, None, None, None, None]
286 276
287 if not isnotsearch: 277 if not isnotsearch:
288 formname = processname(fdata, errors) 278 formname = processname(fdata, errors)
289 dungeon = processgame(fdata, errors) 279 dungeon = processgame(fdata, errors)
290 utime = processtime(fdata, errors, timepieces) 280 searchtime = processtime(fdata, errors, timepieces)
291 281
292 dosearch = formname != None and dungeon != None and utime != None 282 dosearch = formname != None and dungeon != None and searchtime != None
293 283
294 # Find the actual files, and put them in a list called gamefiles. 284 # Find the actual files, and put them in a list called gamefiles.
295 gtimes = [0, int(time.time())] 285 gtimes = [0, int(time.time())]
296 relgame = None 286 relgame = None
297 gamefiles = [] 287 gamefiles = []
298 if dosearch: 288 if dosearch:
299 ttyrecdir = "{0}/{1}/{2}/".format(ttyrecbase, formname, dungeon.uname) 289 query1 = "SELECT ttyrecs FROM {0} WHERE name = %s AND startt <= %s AND endt >= %s;".format(dungeon.uname)
300 query1 = "SELECT ttyrecs FROM {0} WHERE name = %s AND stime <= %s AND etime >= %s;".format(dungeon.uname) 290 query2 = "SELECT ttyrecs FROM {0} WHERE name = %s AND endt >= %s ORDER BY endt LIMIT 1;".format(dungeon.uname)
301 query2 = "SELECT ttyrecs FROM {0} WHERE name = %s AND etime >= %s ORDER BY etime LIMIT 1;".format(dungeon.uname) 291 query3 = "SELECT ttyrecs FROM {0} WHERE name = %s AND startt <= %s ORDER BY startt DESC LIMIT 1;".format(dungeon.uname)
302 query3 = "SELECT ttyrecs FROM {0} WHERE name = %s AND stime <= %s ORDER BY stime DESC LIMIT 1;".format(dungeon.uname)
303 conn = rlgall.getconn() 292 conn = rlgall.getconn()
304 cur = conn.cursor() 293 cur = conn.cursor()
305 cur.execute(query1, [formname, utime, utime]) 294 cur.execute(query1, [formname, searchtime, searchtime])
306 result = cur.fetchone() 295 result = cur.fetchone()
307 if result: 296 if result:
308 gamefiles = result[0] 297 gamefiles = result[0]
309 else: 298 else:
310 cur.execute(query2, [formname, utime]) 299 cur.execute(query2, [formname, searchtime])
311 result = cur.fetchone() 300 result = cur.fetchone()
312 if result: 301 if result:
313 gamefiles = result[0] 302 gamefiles = result[0]
314 else: 303 else:
315 cur.execute(query3, [formname, utime]) 304 cur.execute(query3, [formname, searchtime])
316 result = cur.fetchone() 305 result = cur.fetchone()
317 if result: 306 if result:
318 gamefiles = result[0] 307 gamefiles = result[0]
319 cur.close() 308 cur.close()
320 conn.close() 309 conn.close()
329 sys.stdout.write(rlgall.pti.format("Guild Archives")) 318 sys.stdout.write(rlgall.pti.format("Guild Archives"))
330 319
331 if dosearch: 320 if dosearch:
332 sys.stdout.write("<p>Expedition by {0} to {1} about {2}:</p>\n".format( 321 sys.stdout.write("<p>Expedition by {0} to {1} about {2}:</p>\n".format(
333 rlgall.playerlink(formname), dungeon.name, 322 rlgall.playerlink(formname), dungeon.name,
334 time.strftime("%Y/%m/%d %H:%M:%S", time.gmtime(utime)))) 323 searchtime.strftime("%Y/%m/%d %H:%M:%S")))
335 if not gamefiles: 324 if not gamefiles:
336 sys.stdout.write("<p>No record found.</p>\n") 325 sys.stdout.write("<p>No record found.</p>\n")
337 elif len(gamefiles) == 1: 326 elif len(gamefiles) == 1:
338 sys.stdout.write('<p><a href="/ttyrecs/{0}/{1}/{2}">1 ttyrec found.</a>\ 327 sys.stdout.write('<p><a href="/ttyrecs/{0}/{1}/{2}">1 ttyrec found.</a>\
339 </p>\n'.format(formname, dungeon.uname, gamefiles[0])) 328 </p>\n'.format(formname, dungeon.uname, gamefiles[0]))
351 sys.stdout.write('<p>') 340 sys.stdout.write('<p>')
352 for errmsg in errors: 341 for errmsg in errors:
353 sys.stdout.write(errmsg + " ") 342 sys.stdout.write(errmsg + " ")
354 sys.stdout.write('</p>\n') 343 sys.stdout.write('</p>\n')
355 # Print out a search form, whether a search was done or not. 344 # Print out a search form, whether a search was done or not.
356 sys.stdout.write('<form action="/archivedb.cgi" method="get">\n') 345 sys.stdout.write('<form action="/archive.cgi" method="get">\n')
357 if dungeon != None: 346 if dungeon != None:
358 input_game(sys.stdout, dungeon.uname) 347 input_game(sys.stdout, dungeon.uname)
359 else: 348 else:
360 input_game(sys.stdout) 349 input_game(sys.stdout)
361 if formname != None: 350 if formname != None: