#!/usr/local/bin/python
## ===========================================================================
##  (C) 1994 Harald Scheirich
## ===========================================================================
##  NAME:       dated
##  TYPE:       python script
##  PROJECT:    WWW-Veranstaltungskalender
##  CONTENT:    print dated news , sorted in html
##  VERSION:    1.2
## ===========================================================================
##  AUTHORS:    hs      Harald Scheirich
## ===========================================================================
##  HISTORY:
##
##  12-jan-95 16:47:02  hs      last modification
##  12-jan-95 16:47:02  hs      short list, change filter
##  07-dec-94 13:10:59  hs      Support form input
##  31-oct-94 13:40:46  hs      Major rewrite created event class
##                              this invalidates most of the changes made to
##                              this file
##  12-oct-94 11:00:05  hs      Changed to assoc Arrays in happenings
##  04-oct-94 20:00:05  hs      Added city support
##  26-sep-94 16:49:44  hs      Improved Sorting
##  05-sep-94 10:44:50  hs      created
## ===========================================================================
print 'Content-type: text/html\n'
print '<HTML>'

import sys
sys.stderr = sys.stdout
sys.path.append('/home/private/scheich/happenings')
filename = 'happenings.txt'

import string
import time
import cgi
import event
import Date

#print '<head>'
#print '<title>Service currently disabled!</title>'
#print '</head>'
#print '<body>'
#print '<h2>Due to software problems this services is currently disabled.</h2>'
#print 'Please contact Harald Scheirich at '
#print '<A HREF="mailto:scheich@cg.tuwien.ac.at">scheich@cg.tuwien.ac.at</A>'
#print 'if you want this service to be continued.'
#print '</html>'
#sys.exit(0)


#open the file 

events=event.EventList()
events.open(filename)
events.read()    

try :
    form = cgi.FormContent()
except KeyError:
    form=None
    keys=[]
    
if not form :
    noForm = 1
    listType='standard'
else :
    filter = {}
    keys=form.keys()    


    if 'type' in keys : # What type of query ...
    	# Legal types :
	# short/only the short form wanted
	# single/only a single event wanted (requires sequence number)
	listType = form['type'][0]
    else : listType='standard'

    if 'query' in keys : # This is a query
    	doQuery=0
    	if 'qtype' in keys :
	    doQuery = 1 # There is actually data in the query
    	    filter['type']=form['qtype']
	if 'qwithin' in keys :
	    doQuery = 1
	    x = [Date.today(), string.atoi(form['qwithin'][0])]
	    if len(form['qwithin'])==2 :
		x.append(string.atoi(form['qwithin'][1]))
	    filter['within']=x
	if 'qwithindate' in keys :
	    try :
		
		if len(form['qwithindate'])==2 :
		    date1=Date.DateOfString(form['qwithindate'][0])
		    date2=Date.DateOfString(form['qwithindate'][1])
		else :
		    date1=Date.DateOfString(form['qwithindate'][0])
		    date2=None

		if date2 :
		    if date1 > date2 :
			num = date1-date2
			theDate = date2
		    else :
			num = date2-date1
			theDate = date1
		    filter['within']=[theDate, 0, int(num)]
		else :
		    filter['within']=[theDate, 0, 0]
		doquery=1
	    except ValueError:
		pass
		
	# Here is room for more
	if doQuery == 1 :
		events = events.filter(filter)
	
# Now do the html !!!
print """
<HEAD>
<TITLE>Was ist los !</TITLE>
<HEAD>
<h1>In &Ouml;sterreich ist was los</h1>
<P>

"""
if listType == 'standard' :
    events.printHTML()
elif listType == 'short' :
    
    print '<HR>'
    j = 0
    if len(events.list) > 0 :
	print '<DL>'
	for i in events.list : ### UGLY I know :)
	    #href=""
	    href = 'dated.py?type=single&number='+`j`
	    if 'query' in keys:
		href = href+'&query=on'
		for l in filter.keys() :
		    for k in filter[l] :
		    	if type(k) <> type(''):
			    href=href+'&q'+l+'='+`k`
			else :
			    href=href+'&q'+l+'='+k        
	    print '<DT><A HREF="'+href+'">'
	    print ' <IMG SRC='+i.getGif()+'>'+'</A>  '
	    print '<STRONG>'+i.getValue('title')+'</STRONG>'
	    print '<DD>'+i.headline()
	    j = j+1
	print '</DL><HR>'
    else :
	print """
	<STRONG>Nix is los in dem Bereich</STRONG><BR>
	Ich wei&szlig;, Du willst jetzt eine Veranstaltung
	<A HREF=manager.py?function=try&dowhat=new>eintragen</A> !
	<HR>
	"""
	
elif listType == 'single' :
    print '<HR>'
    num = string.atoi(form['number'][0])
    events.list[num].printHTML() 
    print '<HR>'
    
print """
<IMG SRC=/happenings/gifs/questionS.gif ALT=(?)>
<A HREF=help/list.html>Hilfe</A> <IMG SRC=gifs/questionS.gif ALT=(?)>
zu den Listen.<HR>
<A HREF="happenings.html">Veranstaltungskalender</A> - 
<A HREF="http://www.cg.tuwien.ac.at/otherinfos.html">Andere Infos</A> - 
<A HREF="http://www.cg.tuwien.ac.at/">Homepage</A>
<HR>
	
""" 
print 'Diese Seite wurde am '+Date.DateFormatEuropeanNumeric%Date.today()
print """
automatisch von einem python script erzeugt.
<A HREF="/~scheich/">Harald Scheirich</A>,
Institut f&uuml;r Computergraphik, <A HREF=/>Abteilung f&uuml;r
Visualisierung und Animation</A>
</BODY>
</HTML>
"""

