#!/usr/bin/python

import cgi, traceback, StringIO, mx.DateTime, mx.DateTime.Parser, boozeup

pageTopTemplate = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

  <head>
    <title>Python South-East UK booze-up page</title>
    <link rel="stylesheet" href="http://www.brunningonline.net/simon/blog/styles-site.css" type="text/css"/>
  </head>
  
  </body>
    <div id="banner">Python South-East UK booze-up page</div>
    <div class="blogbody">
      <p>As at %(relativeDateFormatted)s, the next Python South-East UK booze-up is on %(nextBoozeupDateFormatted)s, at <a href="%(venueURL)s">%(venueName)s</a>, at seven p.m. (<a href="%(venueMapURL)s">Click here for a map</a>.)</p>
    
      <p>To find the next session as at other dates, enter here...
      <form method="post" action="boozeup.cgi">
        <input type="text" name="requestDate" size="50"/>
        <input type="submit" value="When's the booze-up?"/>
      </form></p>
      
      <p>All the venues:
        <ul>
"""
	  
pageMiddleTemplate = """<li><a href="%(venueURL)s">%(venueName)s</a> - <a href="%(venueMapURL)s">Map</a></li>
          """
      
pageBottomTemplate = """
        </ul>
      </p>
    </div>
  </body>
</html>
"""

errorTemplate = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

  <head>
    <title>Python South-East UK booze-up page</title>
    <link rel="stylesheet" href="http://www.brunningonline.net/simon/blog/styles-site.css" type="text/css"/>
  </head>
  
  </body>
    <div id="banner">Python South-East UK booze-up page</div>
    <div class="blogbody">
      <p>No, sorry, cound't make anything of "%s". Try somthing a bit simpler.
        <form method="post" action="boozeup.cgi">
          <input type="text" name="requestDate" size="50"/>
          <input type="submit" value="When's the booze-up?" />
        </form>
      </p>
    </div>
  </body>
</html>
"""

venues = (
          ("The Fire Station, Waterloo, London", "http://www.beerandtotty.co.uk/samservlets/pubJump/177/frames", "http://www.streetmap.co.uk/streetmap.dll?G2M?X=531231&Y=179864&A=Y&Z=2"),
          ("The Turf Tavern, Oxford", "http://www.beerintheevening.com/pubs/show.shtml/406/", "http://www.streetmap.co.uk/streetmap.dll?G2M?X=451658&Y=206494&A=Y&Z=2"),
          ("The Falcon, Clapham Junction, London", "http://www.beerintheevening.com/pubs/show.shtml/295/", "http://www.streetmap.co.uk/streetmap.dll?G2M?X=527337&Y=175463&A=Y&Z=2"),
          ("G.J.'s, Colliers Wood, London", 'http://www.gj-s.com/sw19.htm', 'http://www.streetmap.co.uk/streetmap.dll?G2M?X=526933&Y=170661&A=Y&Z=2'),
          ("The Slug and Lettuce, Seven Dials, London", "http://slug.rapidhost.co.uk/directions/westend.htm", "http://www.streetmap.co.uk/streetmap.dll?G2M?X=530085&Y=180947&A=Y&Z=2"),
          ("The Dickens Tavern, Paddington, London", "http://www.beerintheevening.com/pubs/show.shtml/259/", "http://www.streetmap.co.uk/streetmap.dll?G2M?X=526773&Y=181227&A=Y&Z=2"),
          ("The William Morris, Colliers Wood, London", "http://www.beerintheevening.com/pubs/show.shtml/529/", "http://www.streetmap.co.uk/streetmap.dll?G2M?X=526446&Y=169814&A=Y&Z=2"),
          ("The Hobgoblin, Reading", "http://www.beerintheevening.com/pubs/show.shtml/367/", "http://www.streetmap.co.uk/streetmap.dll?G2M?X=471619&Y=173464&A=Y&Z=2"),
          ("The Lamb and Flag, Covent Garden, London", "http://www.beerandtotty.co.uk/samservlets/pubJump/116/London/Covent_Garden/The_Lamb_and_Flag/details.htm", "http://www.streetmap.co.uk/streetmap.dll?G2M?X=530154&Y=180848&A=Y&Z=2"),
          ("The Head of Steam, Euston, London", "http://www.beerintheevening.com/pubs/show.shtml/122/", "http://www.streetmap.co.uk/streetmap.dll?G2M?X=529725&Y=183195&A=Y&Z=2"),
          ('Cubana, Paddington, London', 'http://www.cubana.co.uk/', 'http://www.streetmap.co.uk/streetmap.dll?G2M?X=527007&Y=181360&A=Y&Z=2'),
          ("The Plumbers Arms, Victoria, London", "http://www.beerintheevening.com/pubs/show.shtml/498/", "http://www.streetmap.co.uk/streetmap.dll?G2M?X=528871&Y=178741&A=Y&Z=2"),
         )

print "Content-type: text/html\n"

def main():
    
    try:
        dateParseError = None
        formdata = cgi.FieldStorage()
        if formdata.has_key("requestDate"):
            try:
                relativeDate = mx.DateTime.Parser.DateFromString(formdata["requestDate"].value)
            except:
                dateParseError = 1
        else:
            relativeDate = mx.DateTime.today()

        if dateParseError:
            print errorTemplate % (formdata["requestDate"].value,)
        else:
            relativeDateFormatted = relativeDate.Format('%A, %B %d, %Y')
            nextBoozeupDate = boozeup.getNextBoozeupDate(relativeDate=relativeDate)
            nextBoozeupDateFormatted = nextBoozeupDate.Format('%A, %B %d, %Y')
            nextBoozeupMonth = nextBoozeupDate.month
            venueName, venueURL, venueMapURL = venues[nextBoozeupMonth-1]
            
            print pageTopTemplate % locals()

            for venueName, venueURL, venueMapURL in venues:
                print pageMiddleTemplate % locals()

            print pageBottomTemplate % locals()
            
    except:
        foo = StringIO.StringIO()
        traceback.print_exc(file=foo)
        print foo.getvalue()

if __name__ == '__main__':
    main()