#!/usr/bin/env python2 # (C) 2003 Ben Collins-Sussman under the GNU General Public License. # See the file COPYING for details. import MySQLdb, cgi # enable debugging: displays detailed exceptions in web browser. import cgitb; cgitb.enable() # temporary: eventually put 'effrecipes.py' into a standard PYTHONPATH # location, like /usr/lib/python2.3/site-packages/ import sys sys.path.append('/home/sussman/projects/effrecipes/objects') import effrecipes # ----------------------------------------------------------------- def print_header(page_title): print "Content-type: text/html\n\n"; print "Effrecipes: add ingredient\n" print "

", page_title, "

\n\n" def print_footer(): print "" def print_form(recipe, conn): # fetch the lists of all known units and ingredients all_units = effrecipes.unit_list(None, conn) all_ingredients = effrecipes.ingredient_list(None, conn) # fetch the current list of IngredientQuantity objects attached to recipe current_ingredients = recipe.ingredientlist(conn) # the form action invokes this same page again, so users can # repeatedly add ingredients. print '
\n' print '' print 'Numerical quantity: ' print 'Unit: ' print 'Ingredient: \n' print '' print '
\n' # show all attached IngredientQuantity objects in a table, with option # to delete any one of them. print '

Current ingredients attached to this recipe:

' print "\n" for record in current_ingredients: unit = effrecipes.unit_lookup(record.UnitId, conn) ingredient = effrecipes.ingredient_lookup(record.IngId, conn) print '' print '' print '' print '' print '' print '
' print "
Amount = ' + str(record.Amount) + '' + unit.Name + '' + ingredient.Name + '
' print '' print '

" #------------------------------------------------------------------------ # Main logic conn = MySQLdb.connect(user='effuser', passwd='effuser', db='effrecipes') form = cgi.FieldStorage() # fetch the recipe object to which we're attaching ingredients val = form["Id"].value recipe = effrecipes.recipe_lookup(int(val), conn) # Initial handshake output print_header("Adding Ingredients to
'" + recipe.Name + "'") # Accepting new data: # There MIGHT be new ingredientquantity data coming in here; # if so, validate it and create a new record. if (form.has_key("Amount")): amount = int(form["Amount"].value) unitid = int(form["UnitId"].value) ingid = int(form["IngId"].value) new_ingquantity = effrecipes.IngredientQuantity(int(val), amount, unitid, ingid) new_ingquantity.save(conn) # Now show the real input form print_form(recipe, conn) print_footer() conn.close()