#!/usr/pd/bin/python import cgi import os import string labname="lab2" mailfields=["email1","email2"]; datafields=["group","name1","name2"]; datafields.extend(mailfields) extensions=["txt","ps","pdf"] handin_dir="TEST_HANDIN" report_mail="alten@math.chalmers.se" def open_dump_pickle(object,fname): import cPickle a=open(fname,'w') b=cPickle.dump(object,a) a.close() def print_header(): print "Content-type: text/html" print print " " print ""+labname +" submission confirmation" print """""" print "" print "" print def print_error(errorstring): print errorstring print "
Press the back button to try again" print "" def doit(): print_header() form=cgi.FieldStorage() data={} try: filedata=form["fildata"] except (KeyError,TypeError): print "Oooops, something wrong no 1, contact "+report_mail raise SystemExit for i in datafields: try: data[i]=form[i] except (KeyError,TypeError): print "Oooops, something wrong type %s, contact +"report_mail % i raise SystemExit # check for group number try: no=string.atoi(data["group"].value) except ValueError: print_error(data["group"].value + " is not a valid group number") raise SystemExit # check mail adresses for i in mailfields: adr=data[i].value if adr.strip()!="" and adr.count("@")!=1: print_error(adr + " is not one single valid e-mail adress") raise SystemExit # check extension types q=filedata.filename.split("."); if len(q)==1 or extensions.count(q[-1])!=1: mystr=filedata.filename + " is not a valid filename, since it has the wrong extension" mystr=mystr+ "
Valid extensions are:" for i in extensions: mystr=mystr+"
%s"%i print_error(mystr) raise SystemExit subno=1; while (subno<1000): recname=os.path.join(handin_dir,"record_gr_"+repr(no)+"_no_"+repr(subno)+".txt") if not(os.access(recname,os.F_OK)): break subno=subno+1 # check filedata length if len(filedata.value)==0: print_error("The file "+filedata.filename+ " seems to be empty or nonexistant.") raise SystemExit # write file data fname=os.path.join(handin_dir,"gr_"+repr(no)+"_no_"+repr(subno)+"_"+filedata.filename); try: out=open(fname,"w") except : print "Oooops, something wrong with file, contact "+report_mail raise SystemExit out.write(filedata.value); out.close() try: out=open(recname,"w") except : print "Oooops, something wrong with file, contact "+report_mail raise SystemExit for i in datafields: if data[i].value!="": out.write("%s : %s\n" % (i,data[i].value)); out.write("file : %s\n"% ("gr_"+repr(no)+"_no_"+repr(subno)+"_"+filedata.filename)) out.close print "

Done

" print "You have submitted the lab " +labname + "
" if subno==1: print "This was the first time you turned it in
" elif subno==2: print "This was the second time you turned it in
" elif subno==3: print "This was the third time you turned it in
" else: print "This was the %i:th time you turned it in
" %subno print "" if __name__=="__main__": doit()