#!/usr/bin/python #---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ # Wedding Registry module # # 12-28-96 ct7 Original version #---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ import sys sys.stderr = sys.stdout print "Content-type: text/html\n" import registry import template import cgi import string,time import sys,traceback PASSWORDS = [ 'swing','jazz','polka' ] #---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ def parse_form( f ): # Get the Item ID try: item_id = string.atoi(f['item_id'].value) except KeyError: raise ValueError, "Missing item_id" except TypeError: raise ValueError, "Bad item_id (%s)" % f['item_id'] # Get the Item quantity (default = 1) try: quantity = string.atoi(f['qty'].value) except KeyError: quantity = 1 except TypeError: raise ValueError, "Bad quantity" # Get the user's comment (default = "") try: comment = f['comment'].value comment = comment[:200] except KeyError: comment = '' except ValueError: comment = '' # Get the action (default = password) try: action = f['action'].value except KeyError: action = 'password' # Get the user's password (default = "") try: password = f['password'].value except KeyError: password = '' else: password = string.strip( password ) password = string.lower( password ) # Check the password if this is a 'check' step # Bad password will shift to the 'again' step if action == 'check': if not password in PASSWORDS: action = 'again' return ( item_id, quantity, comment, action ) #---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ def display_password( item_id ): print """\ Sadly, not all denizens of the web are completely trustworthy.

Because of this and in order to prevent malicious tampering with the registry, we need to ask for the password that you were given previously. If you don't have a password, or have forgotten the password, please get in touch with one of our neutral registry contacts.

""" display_password_form( item_id ) #---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ def display_password_again( item_id ): print """\ The password you entered was not correct.

If you mis-typed it, please enter it below, otherwise if you don't have a password, or have forgotten the password, please get in contact with one of our neutral registry contacts.

""" display_password_form( item_id ) #---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ def display_password_form( item_id ): print ( '

\n' '\n' '\n' '\n' '\n' '\n' '
\n' '' '\n' '
Password:
' '' '
\n' '
\n') % item_id #---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ def display_item( item_id ): i = registry.Item() i.load( item_id ) print """\ To register your gift, just enter the quantity in the box below, and press the register button. If you accidently selected the wrong gift, click here and you will be returned to the Registry.

""" print ( '

\n' '\n' '\n' '\n' '\n' '\n' '
\n' '\n' '' '\n' '\n' '\n' '\n' '' '\n' '
Section: %s
Description:%s
%s
Vendor: %s
Pattern: %s
Color: %s
Quantity: 
' '' '
\n' '
\n') % ( item_id, i.sect_name, i.item_type, i.comment, i.vendor, i.pattern, i.color ) return #---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ def verify_item( item_id, quantity ): i = registry.Item() i.load( item_id ) print """\ Here's your chance to make sure that everything is correct. To register your gift, press the register button. If you accidently entered the wrong amount, click here and you will be returned to the Registry.

""" print ( '

\n' '\n' '\n' '\n' '\n' '\n' '\n' '
\n' '\n' '' '\n' '\n' '\n' '\n' '' '\n' '
Section: %s
Description:%s
%s
Vendor: %s
Pattern: %s
Color: %s
Quantity: %s
' '' '
\n' '
\n') % ( item_id, quantity, i.sect_name, i.item_type, i.comment, i.vendor, i.pattern, i.color, quantity ) return #---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ def list_item( item_id, quantity ): try: l = registry.Listing() l.new() l.list_item = item_id l.list_qty = quantity l.list_comment = "" l.list_time = int(time.time()) l.save() i = registry.Item() i.load( item_id ) i.qty_have = i.qty_have + quantity i.save() print ( 'Thank you for registering your gift.' ) except: print '
'
		traceback.print_exc()
		print '
' print '
' print l.__dict__ print '
' print i.__dict__ print '
' print ( 'An error occurred while saving your listing. ' 'Please return to the main ' 'register page ' 'and try again.' ) #---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ def handle_form( f ): # Parse the CGI arguments try: item_id, quantity, comment, action = parse_form( f ) except ValueError, msg: print ( 'This program has been called in error. ' 'Please check your parameters and try again.

' '%s' ) % msg return # Act on the item_id and action if action == 'password': display_password( item_id ) elif action == 'again': display_password_again( item_id ) elif action == 'check': display_item( item_id ) elif action == 'verify': verify_item( item_id, quantity ) elif action == 'list': list_item( item_id, quantity ) else: print ( 'This program has been called in error. ' 'Please check your parameters and try again.' ) return #---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ f = cgi.FieldStorage() # cgi.print_form( f ) # cgi.print_environ() template.header("Register a Gift") handle_form( f ) template.footer()