#!/usr/bin/env python import os import cgi, string, sys, fcntl import MySQLdb from time import * from random import * # send an email to self/others when enrolments received: import smtplib import mimetypes # send out a confirmation email/with pdf attachment: from email.Utils import formatdate from email.Encoders import encode_base64 from email.MIMEBase import MIMEBase from email.MIMEImage import MIMEImage from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText #from settings import * #view cgi errors through browser in develment stage: import cgitb; cgitb.enable() #or send errors to file #import cgitb; cgitb.enable(display=0,logdir="./tmp") #start printing to screen (form) print "Content-Type: text/html\n\n" #this is the code for the Whitianga conference, Nov 08 def generate_form(timestamp): now=timestamp; print "\n" print "\n" print "\tRegistration form\n" print "\n" print "\n" print "\t

Meeting Registration Form

\n" print "\t\t
\n" form = cgi.FieldStorage() if not (form.has_key("id")): id=int(random()*10000000) ''' generate a random enrolment number for each enrollee -- persists for duration of enrolment session ''' print "\t\n" print "\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \
Personal details

Note: If any of the fields below do not apply to you, please leave the dash (-), otherwise the form will crash, and you will have to make another subsequent attempt.

(* denotes required field)
Title:
(*) Family name:
(*) First name/s:
(*) Affiliation:
City:
(*) Email address:
Number of accompanying persons, not including yourself:
The recommended arrival date is the afternoon/evening of Tuesday 18th November.
Arrival Date:
The recommended departure date is the afternoon of Friday 21st November.
Departure date
If you are a student, who is your supervisor?
If you do not attend the meeting with family members, please provide, if possible, name of person with whom you prefer to share your room
If you require van transportation from Auckland to Whitianga prior to the conference, how many people?
If you require van transportation from Whitianga to Auckland after to the conference, how many people?
What kind of postgraduate student are you?
Would you like us to provide your accomodation?
Please tell us if you have any special dietary requirements:
Please describe any special medical conditions which we should be aware of:
Please add anything else which you would like to mention:
" print "\t" print "\t" print "\t" print "\t\n" print "\t\n" print "\t
\n" print "\n" print "\n" return def cgiFieldStorageToDict( X ): """Get a plain dictionary, rather than the '.value' system used by the cgi module.""" formdict = {} for key in X.keys(): formdict[ key ] = X[ key ].value del formdict["action"] return formdict def db_write(form): formdict=cgiFieldStorageToDict(form) cols=formdict.keys() vals=formdict.values() statement= "INSERT INTO registration (%s) VALUES(%s)" % ( ",".join(cols), ",".join(["%s"]*len(vals)) ) try : conn = MySQLdb.connect (host="130.216.33.87", read_default_file = "/home/hcoh001/mysql/.aitken_my.cnf") except MySQLdb.Error, e: print "Error %d: %s" % (e.args[0], e.args[1]) sys.exit (1) try : cursor = conn.cursor () cursor.execute (statement, tuple(vals)); except MySQLdb.Error, e: print "Error %d: %s" % (e.args[0], e.args[1]) sys.exit (1) def makeAttachment(email,fullname, email_body,timestamp) : time_secs= "%d" % ( timestamp) filename="/tmp/email%s" % (time_secs) outfile = open(filename,"w") fcntl.flock(outfile.fileno(),fcntl.LOCK_EX); dstamp="Registration Date: %s \n" % asctime(localtime(timestamp)) outfile.write(dstamp) addressee="Enrollment Name: %s \n" % fullname outfile.write(addressee) s='Email: %s\n\n' % email outfile.write(s) outfile.write(email_body) fcntl.flock(outfile.fileno(),fcntl.LOCK_UN) outfile.close() return email_body def emailing(form,timestamp): addressee = '%s %s %s' % (form['title'].value, form['first_name'].value, form['surname'].value) f=open('text_body','r') contents=f.read() message=contents % (addressee, form['affiliation'].value, form['city'].value+' ', form['email'].value, form['accompanying'].value, form['arrival'].value, form['departure'].value, form['roommate'].value,form['supervisor'].value, form['tauckwhit'].value, form['twhitauck'].value, form['pgrad'].value, form['accomodation'].value, form['diet'].value, form['med'].value+' ', form['other'].value) f.close() email_body =makeAttachment(form['email'].value,addressee,message,timestamp) msg = MIMEMultipart() msg['From'] = 'h.cohl@math.auckland.ac.nz' msg['To'] = form['email'].value msg['Date']=formatdate(localtime=True) msg['Subject'] = 'Registration: NZMASP 2008 '+form['first_name'].value+' '+form['surname'].value body=MIMEText(email_body) msg.attach(body) server = smtplib.SMTP("mail.math.auckland.ac.nz") server.sendmail("hcoh001@math.auckland.ac.nz", ["hcoh001@math.auckland.ac.nz", form['email'].value], msg.as_string()) server.quit() g=open('text.html','r') screen_out=g.read() g.close() screen_message=screen_out % (addressee, form['affiliation'].value, form['city'].value+' ', form['email'].value, form['accompanying'].value, form['arrival'].value, form['departure'].value, form['roommate'].value,form['supervisor'].value, form['tauckwhit'].value, form['twhitauck'].value, form['pgrad'].value, form['accomodation'].value, form['diet'].value, form['med'].value, form['other'].value) print screen_message # Define main function. def main(): form = cgi.FieldStorage() timestamp = time() if not (form.has_key("action")) : generate_form(localtime(timestamp)) else : formdict=cgiFieldStorageToDict(form) if not (form.has_key("surname") and form.has_key("first_name") and form.has_key("affiliation") and form.has_key("email")): print "Some required personal contact details are missing.
\ Please provide your\