Mercurial > hg > rlgallery-misc
annotate web/archive.cgi @ 0:5ba2123d2c20
Put this project under version control, finally.
Scripts for rlgallery.org, using a PostgreSQL backend.  The recorder
system is in py/, CGI scripts are in web/.
| author | John "Elwin" Edwards <elwin@sdf.org> | 
|---|---|
| date | Wed, 25 Jul 2012 21:59:42 -0700 | 
| parents | |
| children | 8f49df4074d7 | 
| rev | line source | 
|---|---|
| 0 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 1 #!/usr/bin/python | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 2 | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 3 import cgi | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 4 import os | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 5 import sys | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 6 import re | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 7 import time | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 8 import calendar | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 9 import rlgalldb as rlgall | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 10 import cgitb | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 11 | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 12 cgitb.enable() | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 13 | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 14 infop = """<p>Looking for ttyrec files? Don't like digging through Web directory listings, converting time zones in your head, or guessing how many files | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 15 the game was spread across? The Archivist can find them all for you.</p> | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 16 """ | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 17 | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 18 ttyrecbase = "/var/dgl/dgldir/ttyrec/" | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 19 recre = r"(\d{4,4})-(\d{2,2})-(\d{2,2})\.(\d{2,2}):(\d{2,2}):(\d{2,2})\.ttyrec" | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 20 recrec = re.compile(recre) | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 21 | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 22 def name_in_time(filename, timerange): | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 23 "Checks whether filename is a ttyrec with time between Unix times \ | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 24 timerange[0] and timerange[1]." | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 25 nmatch = recrec.match(filename) | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 26 if not nmatch: | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 27 return False | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 28 ntime = calendar.timegm([int(val) for val in nmatch.groups()]) | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 29 if ntime > timerange[0] and ntime <= timerange[1]: | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 30 return True | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 31 else: | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 32 return False | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 33 | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 34 def input_game(outf, selected=None): | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 35 "Prints the form components for selecting a game." | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 36 selstr = '<option label="{0}" value="{1}" selected="selected">{0}</option>\n' | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 37 unselstr = '<option label="{0}" value="{1}">{0}</option>\n' | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 38 outf.write('<div>Dungeon name: <select name="game">\n') | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 39 for game in rlgall.gamelist: | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 40 # This will cause trouble if someone puts games with identical names | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 41 # into rlgall. | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 42 if selected and selected == game.uname: | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 43 outf.write(selstr.format(game.name, game.uname)) | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 44 else: | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 45 outf.write(unselstr.format(game.name, game.uname)) | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 46 outf.write('</select></div>\n') | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 47 return | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 48 | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 49 def input_name(outf, defaultval=None): | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 50 defstr = '<div>Adventurer\'s name: <input type="text" name="name" value="{0}"></div>\n' | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 51 if defaultval: | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 52 outf.write(defstr.format(defaultval)) | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 53 else: | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 54 outf.write('<div>Adventurer\'s Name: <input type="text" name="name"></div>\n') | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 55 return | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 56 | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 57 def input_time(outf, defaultval=None): | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 58 defstr = '<div>Time: <input type="text" name="time" value="{0}"></div>\n' | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 59 if defaultval: | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 60 outf.write(defstr.format(defaultval)) | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 61 else: | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 62 outf.write('<div>Time: <input type="text" name="time"></div>\n') | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 63 return | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 64 | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 65 def input_datetime(outf, dvals=[None, None, None, None, None, None]): | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 66 optstr = '<option value="{0}" label="{1}">{1}</option>\n' | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 67 soptstr = '<option value="{0}" label="{1}" selected="selected">{1}</option>\n' | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 68 tf = '<input type="text" size="2" maxlength="2" name="{0}" value="{1:02}">' | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 69 emptf = '<input type="text" size="2" maxlength="2" name="{0}">' | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 70 sstr = '<div>Date: <select name="year">\n' | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 71 # Default to today | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 72 now = time.gmtime() | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 73 for dindex in range(3): | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 74 if not dvals[dindex]: | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 75 dvals[dindex] = now[dindex] | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 76 for year in range(2010, now[0] + 1): | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 77 if year == dvals[0]: | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 78 sstr += soptstr.format(year, year) | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 79 else: | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 80 sstr += optstr.format(year, year) | 
| 
5ba2123d2c20
Put this project under version control, finally.
 John "Elwin" Edwards <elwin@sdf.org> parents: diff
changeset | 
