changeset 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 def7fecbd437
children a943cfdfbad9
files web/archive.cgi
diffstat 1 files changed, 18 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/web/archive.cgi	Wed Jul 25 23:05:12 2012 -0700
+++ b/web/archive.cgi	Thu Jul 26 08:07:00 2012 -0700
@@ -6,6 +6,7 @@
 import re
 import time
 import calendar
+from datetime import datetime
 import rlgalldb as rlgall
 import cgitb
 
@@ -19,18 +20,6 @@
 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)
 
-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):
   "Prints the form components for selecting a game."
   selstr = '<option label="{0}" value="{1}" selected="selected">{0}</option>\n'
@@ -153,9 +142,9 @@
   return None
 
 def processtime(fdata, errlist, hlist):
-  "Takes a CGI data object and converts to a Unix timestamp by finding fields \
-   called year, month, etc.  Any errors get appended to errlist.  hlist \
-   should contain 6 components, for ymd-hms fields."
+  "Takes a CGI data object and converts to a datetime object by finding \
+   fields called year, month, etc.  Any errors get appended to errlist.  \
+   hlist should contain 6 components, for ymd-hms fields."
 
   # Timestamp overrides human-readable, even if it's invalid.
   badtime = 'The time field is for Unix clocks, which never say anything \
@@ -175,7 +164,7 @@
     chtime = time.gmtime(utime)
     for i in range(6):
       hlist[i] = chtime[i]
-    return utime
+    return datetime.fromtimestamp(utime, rlgall.utc)
 
   # Now try to get a human-readable specification.
   lerrors = []
@@ -271,7 +260,8 @@
   if lerrors:
     errlist.extend(lerrors)
     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
 fdata = cgi.FieldStorage()
@@ -281,38 +271,37 @@
 isnotsearch = checkempty(fdata)
 
 errors = []
-formname = dungeon = utime = None
+formname = dungeon = searchtime = None
 timepieces = [None, None, None, None, None, None]
 
 if not isnotsearch:
   formname = processname(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.
 gtimes = [0, int(time.time())]
 relgame = None
 gamefiles = []
 if dosearch:
-  ttyrecdir = "{0}/{1}/{2}/".format(ttyrecbase, formname, 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 etime >= %s ORDER BY etime 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)
+  query1 = "SELECT ttyrecs FROM {0} WHERE name = %s AND startt <= %s AND endt >= %s;".format(dungeon.uname)
+  query2 = "SELECT ttyrecs FROM {0} WHERE name = %s AND endt >= %s ORDER BY endt 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)
   conn = rlgall.getconn()
   cur = conn.cursor()
-  cur.execute(query1, [formname, utime, utime])
+  cur.execute(query1, [formname, searchtime, searchtime])
   result = cur.fetchone()
   if result:
     gamefiles = result[0]
   else:
-    cur.execute(query2, [formname, utime])
+    cur.execute(query2, [formname, searchtime])
     result = cur.fetchone()
     if result:
       gamefiles = result[0]
     else:
-      cur.execute(query3, [formname, utime])
+      cur.execute(query3, [formname, searchtime])
       result = cur.fetchone()
       if result:
         gamefiles = result[0]
@@ -331,7 +320,7 @@
 if dosearch:
   sys.stdout.write("<p>Expedition by {0} to {1} about {2}:</p>\n".format(
       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:
     sys.stdout.write("<p>No record found.</p>\n")
   elif len(gamefiles) == 1:
@@ -353,7 +342,7 @@
     sys.stdout.write(errmsg + " ")
   sys.stdout.write('</p>\n')
 # 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:
   input_game(sys.stdout, dungeon.uname)
 else: