#!/usr/bin/env python import cgitb cgitb.enable() import cgi import os import datetime import sys sys.path.append("scripts/") import comic import comicbase allowedchars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.' allowedchars_printable = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ ' def sanitize(string, chars=allowedchars): if string is None: return None return "".join([ x for x in string if x in chars ]) def html_escape(s): # escapes &, < and >, \n ->
(should be null-op if there are no \n's) return s.replace("&", "&").replace("<", "<").replace(">", ">").replace("\n", "
") def main(): data = cgi.FieldStorage() print "Content-Type: text/html" print allComics, futureComics = comic.comicslist() earliestDate = datetime.datetime(2006,11,23) recentComics = [ c for c in allComics if c.date and c.date > earliestDate ] usedComicsL = file("data/texansubmitrecord.txt").read().split() usedComicsD = { } for c in usedComicsL: usedComicsD[c] = 1 #print usedcomics texanComics = [ c for c in recentComics if not usedComicsD.has_key(c.id) ] page = sanitize(data.getfirst("page")) if page == "admin": allIDs = [ ] allIDs.extend([c.id for c in allComics if isinstance(c, comicbase.Comic)]) allIDs.extend([c.id for c in futureComics if isinstance(c, comicbase.Comic)]) allIDs.sort() lastid = allIDs[-1] print "" print '3132 Admin interface' print "" print "%d comics published,
%d comics in the online queue,
%d in the texan queue,
lastid: %s

"%\ (len(allComics), len(futureComics), len(texanComics), lastid) print 'Texan Submission Status

' print 'Upcoming

' print 'Upcoming 2+1

' print 'Latest 20

' print 'Main Page

' print '

Go to comic:
' print '100 Random Colors

' if page == "texanstatus": print "Texan queue: %d
"%len(texanComics) print "Unpublished online: %d
"%len(futureComics) print "


" # main print loop for c in texanComics: body = """

%(title)s

%(Body_web_simple)s """%c.__dict__ #print c.Body_web_simple%c.__dict__ print body print 'Link to high-resolution image
'%c.id print "Remove this comic from the list (after it has been used)"%c.id print "





" print "
" if page == "texanmarksent": cid = sanitize(data.getfirst("cid")) print "Comic id adding to the texan status file: %s"%cid file("data/texansubmitrecord.txt", "a").write(cid+"\n") print "

" if page == "emailctl": print "email control page:

" #print data name = sanitize(data.getfirst("name"), chars=allowedchars_printable) emailaddr = sanitize(data.getfirst("emailaddr"), chars=allowedchars_printable) choice = data.getfirst("choice") print "Name:", name, "
" print "Email:", emailaddr, "
" print "action:", choice, "

" # print comics print """An admin has been notifed and you'll be added shortly. You won't hear anything back until you start getting the comics tomorrow morning, at the earliest. If not, try submitting again.

""" print '3132 Home Page

' msg = """Email request. Name: %(name)s Email: %(emailaddr)s Action: %(choice)s %(name)s <%(emailaddr)s> """%{"name":name, "emailaddr":emailaddr, "choice":choice} os.popen("mail richard -a 'From: 3132 web ' -s '3132 Email Request'", "w").write(msg) print "




" if page == "feedback": #print data comicid = sanitize(data.getfirst("comicid"), chars=allowedchars) if not comicid: comicid = "" emailcomicid = "" if comicid: emailcomicid = "(http://rkd.zgib.net/3132/get/" + comicid + ")" body = """

Send me anonymous (if you like) feedback, such as ideas for future comics or flames for old ones. Or better yet, email me at 3132@zgib.net if you want me to get back to you.

Your name (optional):
About comic number:
Your message (optional but you probably want one anyway):

"""%{"comicid":comicid, "emailcomicid":emailcomicid} print body if page == "feedbackctl": #print data name = sanitize(data.getfirst("name"), chars=allowedchars_printable) if not name: name = "(no name)" comicid = sanitize(data.getfirst("comicid"), chars=allowedchars) if not comicid: comicid = "" body = sanitize(data.getfirst("body"), chars=allowedchars_printable+"\n") if not body: body = "(no body given)" print "feedback control page:

" print "Name:", html_escape(name), "
" if comicid: print "comicid:", comicid, "
" print 'URL: http://rkd.zgib.net/3132/get/%s'%(comicid, comicid), "
" print "", html_escape(body), "" print "



", # print comics print """It has been sent. Thanks a lot...


""" print '3132 Home Page

' msg = """Web Feedback ~~~~~~~~~~~~ Name: %(name)s comicid: %(comicid)s URL: http://rkd.zgib.net/3132/get/%(comicid)s %(body)s """%{"name":name, "body":body, "comicid":comicid} os.popen("mail richard -a 'From: 3132 web ' -s '3132 Web Feedback'", "w").write(msg) print "



" if page == "sorted_top": print "" pagenum = sanitize(data.getfirst("p")) if pagenum == None: pagenum = "0" try: pagenum = int(pagenum) except ValueError: pagenum = 0 #print pagenum voteAvgHash = comicbase.getVoteAvgHash() voteNHash = comicbase.getVoteNHash() #print voteAvgHash def comicrank(cid): return voteAvgHash.get(cid) allComicsWithRank = [ (comicrank(cid), cid) for cid in voteAvgHash.keys() ] allComicsWithRank.sort() allComicsWithRank.reverse() pagesList = [ ] while allComicsWithRank: pagesList.append(allComicsWithRank[:10]) del allComicsWithRank[:10] #print pagesList numpages = len(pagesList) ourPage = pagesList[pagenum] def navLinks(pagen, numpages, template="?page=sorted_top&p=%s"): if pagen > 0: Prev = 'Prev'%(template%(pagen-1)) else: Prev = 'Prev' otherpages = [ ] for i in range(numpages): if i == pagen: otherpages.append('%s'%i) else: otherpages.append('%s'%(template%i, i)) if pagen < numpages-1: Next = 'Next'%(template%(pagen+1)) else: Next = 'Next' linkbar = " ".join([Prev]+otherpages+[Next]) return linkbar # print out the page comicHash = comicbase.get_comichash() linkbar = navLinks(pagenum, numpages) print linkbar, "

" for rank, cid in ourPage: nvotes = voteNHash.get(cid, "?") body = """

%(title)s (avgvote=%(rank)s, n=%(nvotes)s)

%(Body_web_simple_novote)s """ #print c.Body_web_simple%c.__dict__ d = comicHash[cid].__dict__.copy() d.update({"rank": rank, "nvotes":nvotes}) print body%d print linkbar, "

" if page == "list": listname = sanitize(data.getfirst("l", "")) if not listname: print "No list name given (the cgi parameter is 'l'):
" listfile = "data/list." + listname + ".txt" if not os.access(listfile, os.R_OK): print "This listing (%s) does not exist!
"%listname return listofcomics = file(listfile).read().split() def getComicID(id): if id.isdigit(): return "c%04d"%int(x) return id #listofcomics = [ x for x in listofcomics if x.isdigit() ] #listofcomics = [ "c%04d"%int(x) for x in listofcomics ] listofcomics = [ getComicID(x) for x in listofcomics ] comichash = comicbase.get_comichash() listofcomics = [ comichash[c] for c in listofcomics if comichash.has_key(c) ] print 'Main Page

' for c in listofcomics: body = """

%(title)s

%(Body_web_simple)s """%c.__dict__ #print c.Body_web_simple%c.__dict__ print body print "





" print "
" if page == "upcoming": #comichash = comicbase.get_comichash() #listofcomics = [ comichash[c] for c in listofcomics if comichash.has_key(c) ] allComics, futureComics = comic.comicslist() print 'Main Page

' for c in futureComics: body = """

%(title)s

%(Body_web_simple)s """%c.__dict__ #print c.Body_web_simple%c.__dict__ print body print "




" print "
" if page == "test": print '''
this is some news
''' print "What you say?" if __name__ == "__main__": main()