def recv_add_liquor_inventory(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) msg = "" status = "No values were modified\n" #Check if values have data if ( ('mfg' in results) and ('liquor' in results) and ('amt' in results)): #Get manufacturer mfg = results['mfg'][0] #Get liquor name liquor = results['liquor'][0] #Get amount amt = results['amt'][0] #Check if bottle type information is there if not (db._check_bottle_type_exists(mfg,liquor)): message = "Ooops Please add manufacturer and liquor information to bottle types first" else: #check if amount unit is correct if not (amt.endswith('ml') or amt.endswith('oz') or amt.endswith('gallon') or amt.endswith('liter')): message = "Ooops unit amount is not correct. " msg = "Valid units: 'ml','oz','gallon','liter'" else: #Add to inventory db.add_to_inventory(mfg,liquor, amt) dynamic_web.save_database('/../bin/drinkz_database') message = "Added to inventory successfully" status = "Updated inventory\n" else: message = "Ooops at least one of the fields was empty" #Generate results in html format content_type = 'text/html' data= """ <html> <head> <title>Updated inventory</title> <style type='text/css'> h1 {color:red;} body{ font-size:14px; } </style> </head> <body> """ data = data + "<h1>" + message + "</h1>" tmp = dynamic_web.generate_inventory_table() data = data + msg + "<p>" + status + "</p>" + tmp data = data + "<p><a href='./add_liquor_inventory.html'>add another liquor to inventory</a></p>" data = data + "<p><a href='./'>return to index</a></p>" data = data + """ </body> <html> """ start_response('200 OK', list(html_headers)) return [data]
def addinv(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) try: mfg = results['mfg'][0] liquor = results['liquor'][0] amount = results['amount'][0] mfg = mfg.strip() liquor = liquor.strip() amount = amount.strip() print mfg,liquor,amount try: db.add_to_inventory(mfg,liquor,amount) addin = "<p>Succesfully added.</p>" except db.LiquorMissing: addin= "<p> You must first add this bottle type " +mfg + " " + liquor + " .</p>" except (AssertionError, KeyError, IndexError) : addin = """\ <p> Incorrect amount format or incomplete. Please try again.</p> """ vars = dict(form=addin) template = env.get_template("add.html") data = template.render(vars) content_type = 'text/html' start_response('200 OK', list(html_headers)) return [str(data)]
def test_get_liquor_amount_1(): db._reset_db() db.add_bottle_type('Johnnie Walker', 'Black Label', 'blended scotch') db.add_to_inventory('Johnnie Walker', 'Black Label', '1000 ml') amount = db.get_liquor_amount('Johnnie Walker', 'Black Label') assert amount == 1000.0, amount
def rpc_add_inventory(self,mfg,liquor,amount): try: db.add_to_inventory(mfg,liquor,amount) data = "Added "+amount+" for "+liquor+" made by "+mfg except db.LiquorMissing: data = "Failed to find this type!" return data
def test_get_mixable_recipes(): db._reset_db() db.add_bottle_type('Johnnie Walker', 'Black Label', 'blended scotch') db.add_to_inventory('Johnnie Walker', 'Black Label', '1000 ml') db.add_bottle_type('Uncle Herman\'s', 'moonshine', 'blended scotch') db.add_to_inventory('Uncle Herman\'s', 'moonshine', '5 liter') db.add_bottle_type('Gray Goose', 'vodka', 'unflavored vodka') db.add_to_inventory('Gray Goose', 'vodka', '1 liter') db.add_bottle_type('Rossi', 'extra dry vermouth', 'vermouth') db.add_to_inventory('Rossi', 'extra dry vermouth', '24 oz') db.add_bottle_type('Meijer', 'pulp free orange juice', 'orange juice') db.add_to_inventory('Meijer', 'pulp free orange juice', '24 oz') r = recipes.Recipe('scotch on the rocks', [('blended scotch', '4 oz')]) db.add_recipe(r) r = recipes.Recipe('vodka gimlit', [('unflavored vodka', '4 oz'), ('lime juice', '1 oz')]) db.add_recipe(r) r = recipes.Recipe('vomit inducing martini', [('orange juice', '6 oz'), ('vermouth', '1.5 oz')]) db.add_recipe(r) mixable_recs = db.get_mixable_recipes() for rec in mixable_recs: print rec.name
def recv3(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) liquorMfg = results['liquorMfg'][0] liquorName = results['liquorName'][0] liquorAmount = results['liquorAmount'][0] bottleTypeExists = db._check_bottle_type_exists(liquorMfg, liquorName) if(bottleTypeExists == True): db.add_to_inventory(liquorMfg, liquorName, liquorAmount) content_type = 'text/html' data = """ <!DOCTYPE HTML> <html> <head> <title>Liquor Amount Added</title> <style type='text/css'> h1 {text-decoration:underline; text-align:center; color:red;} body { font-size:14px; } </style> </head> <body>""" if(bottleTypeExists == True): data += "<h1>Liquor Successfully Added to Inventory!</h1><br/>" else: data += "<h1>Error! Liquor bottle type could not be found. Liquor not added.</h1>" data += "<a href='./'>Return to index</a></body></html>" start_response('200 OK', list(html_headers)) return [data]
def rpc_add_to_inventory(self,mfg,liquor,amount): returnVal = False try: db.add_to_inventory(mfg, liquor, amount) returnVal = True; except Exception: returnVal = False return returnVal
def load_inventory(fp): new_reader = data_reader(fp) #1 n = 0 #2 for mfg, name, amt in new_reader: #3 n += 1 #3,a db.add_to_inventory(mfg, name, amt) #3,b return n #4
def rpc_get_liquor_inventory(self): db.add_bottle_type('Johnnie Walker', 'Black Label', 'blended scotch') db.add_to_inventory('Johnnie Walker', 'Black Label', '1000 ml') inv = [] for mfg,lqr in db.get_liquor_inventory(): inv.append((mfg,lqr)) return inv
def test_add_to_inventory_2(): db._reset_db() try: db.add_to_inventory('Johnnie Walker', 'Black Label', '1000 ml') assert False, 'the above command should have failed!' except db.LiquorMissing: # this is the correct result: catch exception. pass
def test_get_liquor_inventory(): db._reset_db() db.add_bottle_type('Johnnie Walker', 'Black Label', 'blended scotch') db.add_to_inventory('Johnnie Walker', 'Black Label', '1000 ml') x = [] for mfg, liquor in db.get_liquor_inventory(): x.append((mfg, liquor)) assert x == [('Johnnie Walker', 'Black Label')], x
def test_json_get_liquor_inventory(): db._reset_db() db.add_bottle_type('Johnnie Walker', 'black label', 'blended scotch') db.add_to_inventory('Johnnie Walker', 'black label', '500 ml') db.add_bottle_type('Gray Goose', 'vodka', 'unflavored vodka') db.add_to_inventory('Gray Goose', 'vodka', '1 liter') results = call_remote(method='get_liquor_inventory', params=[], id=1) assert ['Johnnie Walker', 'black label'] in results['result'], results['result']
def initialize_db(): db._reset_db() db.add_bottle_type('Johnnie Walker', 'black label', 'blended scotch') db.add_to_inventory('Johnnie Walker', 'black label', '500 ml') db.add_bottle_type('Uncle Herman\'s', 'moonshine', 'blended scotch') db.add_to_inventory('Uncle Herman\'s', 'moonshine', '5 liter') r = recipes.Recipe('scotch on the rocks', [('blended scotch','4 oz')]) db.add_recipe(r) r = recipes.Recipe('vodka martini', [('unflavored vodka', '7 oz'),('vermouth', '1.5 oz')]) db.add_recipe(r) r = recipes.Recipe('kunamatata', [('orange juice', '6 oz'),('vermouth', '1.5 oz')]) db.add_recipe(r)
def test_json_liquor_inventory(): db._reset_db() db.add_bottle_type('Johnnie Walker', 'black label', 'blended scotch') db.add_to_inventory('Johnnie Walker', 'black label', '500 ml') db.add_bottle_type('Uncle Herman\'s', 'moonshine', 'blended scotch') db.add_to_inventory('Uncle Herman\'s', 'moonshine', '5 liter') db.add_bottle_type('Gray Goose', 'vodka', 'unflavored vodka') db.add_to_inventory('Gray Goose', 'vodka', '1 liter') db.add_bottle_type('Rossi', 'extra dry vermouth', 'vermouth') db.add_to_inventory('Rossi', 'extra dry vermouth', '24 oz') liquor_inventory = call_remote(method='get_liquor_inventory', params = [], id='1') rpc_request = simplejson.loads(liquor_inventory) result = rpc_request['result'] print result assert ['Johnnie Walker', 'black label'] in result assert ['Rossi', 'extra dry vermouth'] in result assert ["Uncle Herman's", 'moonshine'] in result assert ['Gray Goose', 'vodka'] in result
def test_getinventory(): db._reset_db() #Reset database db.add_bottle_type('Johnnie Walker', 'black label', 'blended scotch') db.add_to_inventory('Johnnie Walker', 'black label', '500 ml') db.add_bottle_type('Uncle Herman\'s', 'moonshine', 'blended scotch') db.add_to_inventory('Uncle Herman\'s', 'moonshine', '5 liter') db.add_bottle_type('Gray Goose', 'vodka', 'unflavored vodka') db.add_to_inventory('Gray Goose', 'vodka', '1 liter') db.add_bottle_type('Rossi', 'extra dry vermouth', 'vermouth') db.add_to_inventory('Rossi', 'extra dry vermouth', '24 oz') text = call_remote(jsonrpc='1.0', method='get_liquor_inventory', params = [], id='1') rpc_request = simplejson.loads(text) print rpc_request result = rpc_request['result'] assert result[0][0] == 'Johnnie Walker' assert result[0][1] == 'black label' assert result[2][1] == 'moonshine' assert result[3][0] == 'Gray Goose' assert result[1][1] == 'extra dry vermouth'
def inventory_add(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) mfg = results['mfg'][0] liquor = results['liquor'][0] amount = results['amount'][0] db.add_to_inventory(mfg, liquor, amount) headers = list(html_headers) headers.append(('Location', '/inventory')) start_response('302 Found', headers) return ["Redirect to /inventory..."]
def add_items(): #Reset database db._reset_db() db.add_bottle_type('Johnnie Walker', 'black label', 'blended scotch') db.add_to_inventory('Johnnie Walker', 'black label', '500 ml') db.add_bottle_type('Uncle Herman\'s', 'moonshine', 'blended scotch') db.add_to_inventory('Uncle Herman\'s', 'moonshine', '5 liter') db.add_bottle_type('Gray Goose', 'vodka', 'unflavored vodka') db.add_to_inventory('Gray Goose', 'vodka', '1 liter') db.add_bottle_type('Rossi', 'extra dry vermouth', 'vermouth') db.add_to_inventory('Rossi', 'extra dry vermouth', '24 oz') #Add recipes r = recipes.Recipe('scotch on the rocks', [('blended scotch','4 oz')]) db.add_recipe(r) r = recipes.Recipe('vodka martini', [('unflavored vodka', '6 oz'),('vermouth', '1.5 oz')]) db.add_recipe(r) r = recipes.Recipe('vomit inducing martini', [('orange juice','6 oz'),('vermouth','1.5 oz')]) db.add_recipe(r) r = recipes.Recipe('whiskey bath', [('blended scotch', '2 liter')]) db.add_recipe(r)
def initialize_db(): db._reset_db() db.add_bottle_type("Johnnie Walker", "black label", "blended scotch") db.add_to_inventory("Johnnie Walker", "black label", "500 ml") db.add_bottle_type("Three Olives", "Cake", "flavored vodka") db.add_bottle_type("Uncle Herman's", "moonshine", "blended scotch") db.add_to_inventory("Uncle Herman's", "moonshine", "5 liter") r = recipes.Recipe("scotch on the rocks", [("blended scotch", "4 oz")]) db.add_recipe(r) r = recipes.Recipe("vodka martini", [("unflavored vodka", "7 oz"), ("vermouth", "1.5 oz")]) db.add_recipe(r) r = recipes.Recipe("vomit inducing martini", [("orange juice", "6 oz"), ("vermouth", "1.5 oz")]) db.add_recipe(r)
def recvInventory(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) mfg = results['man'][0] name = results['name'][0] amount = results['amount'][0] db.add_to_inventory(mfg, name, amount) content_type = 'text/html' data = "%s <a href='/'>return to index</a>" % "Your item has been added" start_response('200 OK', list(html_headers)) return [data]
def rpc_addinventory(self,mfg,liquor,amount): try: mfg = mfg.strip() liquor = liquor.strip() amount = amount.strip() print mfg,liquor,amount try: db.add_to_inventory(mfg,liquor,amount) addin = "Succesfully added." except db.LiquorMissing: addin= " You must first add this bottle type " +mfg + " " + liquor + " ." except (AssertionError, KeyError, IndexError) : addin = """\ Incorrect amount format or incomplete. Please try again. """ return addin
def test_rpc_get_liquor_inventory(): db._reset_db() mfg1 = 'Uncle Herman\'s' mfg2 = 'Gray Goose' liquor1 = 'moonshine' liquor2 = 'vodka' type1 = 'blended scotch' type2 = 'unflavored vodka' amount1 = '1 liter' amount2 = '2 gallon' db.add_bottle_type(mfg1,liquor1,type1) db.add_bottle_type(mfg2,liquor2,type2) db.add_bottle_type(mfg1,liquor2,type2) db.add_bottle_type(mfg2,liquor1,type1) db.add_to_inventory(mfg1, liquor1, amount1) db.add_to_inventory(mfg2, liquor2, amount2) db.add_to_inventory(mfg1, liquor2, amount2) db.add_to_inventory(mfg2, liquor1, amount1) method = 'get_liquor_inventory' params = [] id = 1 environ = {} environ['PATH_INFO'] = '/rpc' environ['REQUEST_METHOD'] = ('POST') dic = dict(method=method, params=params, id=id) s_input = simplejson.dumps(dic) environ['wsgi.input'] = StringIO(s_input) # looking for a socket? suuuuure, here's "one" environ['CONTENT_LENGTH'] = len(s_input) d = {} def my_start_response(s, h, return_in=d): d['status'] = s d['headers'] = h app_obj = app.SimpleApp() results = app_obj(environ, my_start_response) text = "".join(results) status, headers = d['status'], d['headers'] assert text.find("[\"Uncle Herman's\", \"vodka\"]") != -1, text assert text.find("[\"Uncle Herman's\", \"moonshine\"]") != -1, text assert text.find("[\"Gray Goose\", \"vodka\"]") != -1, text assert text.find("[\"Gray Goose\", \"moonshine\"]") != -1, text assert ('Content-Type', 'application/json') in headers assert status == '200 OK'
def inventory_recv(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) mfg = results['mfg'][0] lqr = results['lqr'][0] typ = results['typ'][0] try: db.add_to_inventory(mfg,lqr,typ) data1 = " *** Inventory Item ( %s , %s , %s ) added <br><br>" % (mfg, lqr, typ) except: data1 = " MISSING LIQUOR TYPE - try again <br><br> " content_type = 'text/html' data2 = make_html.inventory() data = data1 + data2 start_response('200 OK', list(html_headers)) return [data]
def rpc_enter_liquor_inventory_result(self, environ, start_response): # this sets up jinja2 to load templates from the 'templates' directory loader = jinja2.FileSystemLoader('./templates') env = jinja2.Environment(loader=loader) formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) liquor_inventory = results['Input'][0] list0 = liquor_inventory.split() db.add_to_inventory(list0[0], list0[1], list0[2]+' '+list0[3]) filename = 'form_result.html' vars = dict(input_type = 'liquor inventory', direction = direction) template = env.get_template(filename) start_response('200 OK', list(html_headers)) return str(template.render(vars))
def test_rpc_get_liquor_inventory(): db._reset_db() mfg1 = "Uncle Herman's" mfg2 = "Gray Goose" liquor1 = "moonshine" liquor2 = "vodka" type1 = "blended scotch" type2 = "unflavored vodka" amount1 = "1 liter" amount2 = "2 gallon" db.add_bottle_type(mfg1, liquor1, type1) db.add_bottle_type(mfg2, liquor2, type2) db.add_bottle_type(mfg1, liquor2, type2) db.add_bottle_type(mfg2, liquor1, type1) db.add_to_inventory(mfg1, liquor1, amount1) db.add_to_inventory(mfg2, liquor2, amount2) db.add_to_inventory(mfg1, liquor2, amount2) db.add_to_inventory(mfg2, liquor1, amount1) method = "get_liquor_inventory" params = [] id = 1 environ = {} environ["PATH_INFO"] = "/rpc" environ["REQUEST_METHOD"] = "POST" dic = dict(method=method, params=params, id=id) s_input = simplejson.dumps(dic) environ["wsgi.input"] = StringIO(s_input) # looking for a socket? suuuuure, here's "one" environ["CONTENT_LENGTH"] = len(s_input) d = {} def my_start_response(s, h, return_in=d): d["status"] = s d["headers"] = h app_obj = app.SimpleApp() results = app_obj(environ, my_start_response) text = "".join(results) status, headers = d["status"], d["headers"] assert text.find('["Uncle Herman\'s", "vodka"]') != -1, text assert text.find('["Uncle Herman\'s", "moonshine"]') != -1, text assert text.find('["Gray Goose", "vodka"]') != -1, text assert text.find('["Gray Goose", "moonshine"]') != -1, text assert ("Content-Type", "application/json") in headers assert status == "200 OK"
def addInventory(self, environ, start_response): formdata = environ['QUERY_STRING'] results = urlparse.parse_qs(formdata) mfg = results['mfg'][0] liquor = results['liquor'][0] amt = results['amt'][0] try: db.add_to_inventory(mfg, liquor, amt) data = inventoryList() except Exception: data = inventoryList() +"""<script> alert("That liquor is not an added type."); </script>""" content_type = 'text/html' start_response('200 OK', list(html_headers)) return [data]
def addInventory(self, environ, start_response): formdata = environ["QUERY_STRING"] results = urlparse.parse_qs(formdata) mfg = results["mfg"][0] liquor = results["liquor"][0] amt = results["amt"][0] try: db.add_to_inventory(mfg, liquor, amt) data = inventoryList() except Exception: data = ( inventoryList() + """<script> alert("That liquor is not an added type."); </script>""" ) content_type = "text/html" start_response("200 OK", list(html_headers)) return [data]
def do_add_inventory(self, environ): error = None if environ.get('CONTENT_LENGTH'): length = int(environ['CONTENT_LENGTH']) body = environ['wsgi.input'].read(length) d = urlparse.parse_qs(body) if d.has_key('cancel'): # nothing submitted, nothing to do return error try: mfg = d['mfg'][0] liquor = d['liquor'][0] # this will cause a ValueError if qty is not a number qty = float(d['qty'][0]) # but we don't care about that other than to get the error amount = '%s %s' % (d['qty'][0], d['unit'][0]) db.add_to_inventory(mfg, liquor, amount) except (KeyError, ValueError): error = "Error processing form." except db.LiquorMissing, e: error = e.message
def initialize_db(): db._reset_db() db.add_bottle_type('Johnnie Walker', 'black label', 'blended scotch') db.add_to_inventory('Johnnie Walker', 'black label', '500 ml') db.add_bottle_type('Three Olives', 'Vodka', 'flavored vodka') db.add_bottle_type('Uncle Herman\'s', 'moonshine', 'blended scotch') db.add_to_inventory('Uncle Herman\'s', 'moonshine', '5 liter') r = recipes.Recipe('scotch on the rocks', [('blended scotch','4 oz')]) db.add_recipe(r) r = recipes.Recipe('vodka martini', [('unflavored vodka', '7 oz'),('vermouth', '1.5 oz')]) db.add_recipe(r) r = recipes.Recipe('vomit inducing martini', [('orange juice', '6 oz'), ('vermouth', '1.5 oz')]) db.add_recipe(r) db._c.execute('INSERT INTO users (firstname,lastname,username,pass) VALUES ("test1_first","test1_last","test1_username","guest")') db.dump_db('db.txt')
def load_inventory(fp): """ Loads in data of the form manufacturer/liquor name/amount from a CSV file. Takes a file pointer. Adds data to database. Returns number of records loaded. Note that a LiquorMissing exception is raised if bottle_types_db does not contain the manufacturer and liquor name already. """ new_reader = data_reader(fp) n = 0 for (mfg, name, amount) in new_reader: n += 1 db.add_to_inventory(mfg, name, amount) return n
def load_inventory(fp): """ Loads in data of the form manufacturer/liquor name/amount from a CSV file. Takes a file pointer. Adds data to database. Returns number of records loaded. Note that a LiquorMissing exception is raised if bottle_types_db does not contain the manufacturer and liquor name already. """ try: n = 0 for j in csvreader(fp): (mfg, name, amount) = j db.add_to_inventory(mfg, name, amount) n += 1 return n except: print 'Unexpected Error'