#!/usr/local/bin/python

import cgi
import os
import posix
import string

print "Content-Type: text/html"
print

print "<html><head>"
print "<Title>To Do List v0.1</Title>"
print "</head><body>"

def itemwrite(name,item):
	"Write an item to disk"
	f = open(name, 'w')
	f.write("Title:"+item['Title'] +'\n')
	f.write("Dept:"+ item['Dept'] +'\n')
	f.write("Time:"+ item['Time'] +'\n')
	f.close()

def packitem(title, time, dept):
	"Pack args into an item dictionary"
	items={'Title':title, 'Dept':dept, 'Time' : time}
	return items

def itemread(name):
	"Read an item from disk"
	f = open(name, 'r')
	stuff=f.readlines()
	items={'Title':'No title for this item', 'Dept':'No dept chosen', 'Time': '0'}
	for j in stuff:
		if string.find(j, ':') != -1:
			items[string.split(j,':')[0]] = string.split(j,':')[1]
	f.close()
	return items

def itemwrite2(name, title, time, dept):
	"Write an item to disk"
	itemwrite(name, packitem(title, time, dept))

def cmpfunc (a,b):
	"check whether the integers in strings a and b are greater of smaller than each other"
	if string.atoi(a) == string.atoi(b):
		return 0
	elif string.atoi(a) > string.atoi(b):
		return -1
	else:
		return 1

########## sort out details of previous submit

form=cgi.FieldStorage()
#for i in form.keys():
#	print i + ":" +form[i].value + "<br>"

#deal with new entry
if form.has_key('CONTENTS'):
	files=posix.listdir("todo")
	files2=posix.listdir("done")
	biggest=0
	for i in files + files2:
		if int(i) > biggest:
			biggest=int(i)
	biggest = biggest+1
	if form.has_key('DONENEW'):
		newfile="done/"+str(biggest)
	else:
		newfile="todo/"+str(biggest)
	if form.has_key('TIMENEW'):
		newtime=form['TIMENEW'].value
	else:
		newtime='0'
	if form.has_key('DEPTNEW'):
		newdept=form['DEPTNEW'].value
	else:
		newdept='No dept chosen'
	item=packitem(form['CONTENTS'].value, newtime,newdept)
	itemwrite(newfile, item)

#deal with updates
for i in form.keys():
	if string.find(i, 'TIME') != -1 and string.find(i, 'NEW')==-1:
		chfile=i[4:]
		item=itemread("todo/"+chfile)
		if item['Time'] != form[i].value:
			item['Time']=form[i].value
			itemwrite("todo/"+chfile, item)
for i in form.keys():
	if string.find(i, 'DONE') != -1 and string.find(i, 'NEW')==-1: 
		donefile=i[4:]
		os.rename("todo/"+donefile, "done/"+donefile)
	

########### print page 
FONTSIZE="-1"
files=posix.listdir("todo")
files.sort(cmpfunc)
print "<table width=100% border=0><tr><td><font size=7>Dan's Todo list<font>"
print "<td align=right><a href=todo.cgi>Reload and destroy changes</a><br><font size=-3>(don't press the reload button above - it'll screw things up)</font></table>"
print "<form name=todo method=get action=todo.cgi>"
print "<input type=submit value=submit>"
print "<table border=1>"
print "<tr><td width=90%>Item<td>Dept.<td>Time(hours)<td>done"

f=open("deptlist", 'r')
deptlist=f.readlines()
f.close()

bgcolor="#cccccc"
print "<tr>"
print "<td bgcolor="+bgcolor+"><input name=CONTENTS size=50>"
print "<td bgcolor="+bgcolor+"><select name=DEPTNEW>"
for i in deptlist:
	print "<OPTION>"+i
print "</select>"
print "<td bgcolor="+bgcolor+"><input name=TIMENEW size=6>"
print "<td bgcolor="+bgcolor+"><input type=checkbox name=DONENEW>"

count=1
for i in files:
	count =count+1
	str="todo/"+i
	f = open(str, 'r')
	items=itemread(str)
	if count % 2 == 1:
		bgcolor="#cccccc"
	else:
		bgcolor="#ffffff"
	print "<tr>"
	print "<td bgcolor="+bgcolor+"><font size="+FONTSIZE+">"+items['Title']
	print "<td bgcolor="+bgcolor+"><font size="+FONTSIZE+">"+items['Dept']
	print "<td bgcolor="+bgcolor+"><font size="+FONTSIZE+"><input name=TIME"+i+" size=6 value="+items['Time']+">"
	print "<td bgcolor="+bgcolor+"><input type=checkbox name=DONE"+i+">"

print "</table></form>"

print "</body></html>"
