def print_headers(catid=0,catname=''): print '''Content-Type: text/html\n\n''' if catid != 0: catname = db.get_category(catid).get_name() elif catname == '': catname = '' print '''<html><head> <title>Open Produce Category '%s' Info</title>''' % catname print ''' <style type="text/css"> table thead { background-color:#ddd; color:#333; font-weight: bold; cursor: default; }\n tr:nth-child(even) { background-color: #E0E0E0; }; </style> ''' print '''</head><body>'''
def print_categories(item): print '<table id="categories">' print '''<thead> <th>Category</th> <th>Remove</th> </thead>''' for category_item in item.get_category_items(): category = db.get_category(category_item.get_cat_id()) print '''<tr id="%d_category_tr"><td><a href="%s">%s</a></td><td><button type="button" onClick="removeCategory(%d)"> remove </button> </td>''' % (category_item.get_id(),db.get_category_info_page_link(category.get_id()),category.get_name(),category_item.get_id()) print '''</table>''' print '<select id="new_category">' for cat in db.get_categories(): print '''<option value="%d"> %s </option>''' % (cat.get_id(), cat) print '</select>' print '''<button type="button" onClick="addCategory()"> Add Category </button>''' print '<br />'*2
def main(): form = cgi.FieldStorage() if 'categoryid' in form: print_headers(int(form.getvalue('categoryid'))) elif 'categoryname' in form: print_headers(catname=form.getvalue('categoryname')) else: print_headers() print '''<form name="category" action="category_info.py" method="get">''' print '''Input a category name''' print '''<input type="text" name="categoryname" size="4" /> <input type="submit" value="Display" />''' print '''</form>''' category = None if "categoryid" in form: categoryid = int(form.getvalue("categoryid")) category = db.get_category(categoryid) elif "categoryname" in form: try: category = db.get_category_byname(form.getvalue("categoryname")) except Exception: print '''Error: category '%s' not found''' % form.getvalue("categoryname") if category != None: print '''<b>Info for category %s:</b><br />''' % category print '''<b>Items in this category </b><br/><table> <thead> <th>ItemID</th> <th>Name</th> </thead>''' for item in db.get_items(show_categories=[category.get_name()], hide_categoryless=True): print '''<tr><td><a href="%s">%d</a></td><td>%s</td></tr>''' % (db.get_item_info_page_link(item.get_id()),item.get_id(), item.get_name()) print '''</table>'''
db.update_distributor_item(d_i, ditemid, wholesale_price, case_size, case_unitid) item = db.get_item(itemid) each_cost = d_i.get_each_cost() op_price = item.get_price() tax = item.get_tax_value() if (op_price - tax) != 0: margin = (1.0 - each_cost / (op_price - tax)) * 100 else: margin = 100 item_price = db.get_price(item.get_price_id()) cost = item_price.get_unit_cost() if "categories" in form: cat_ids_list = form.getvalue("categories") cat_ids = filter(lambda x: x.isdigit(), cat_ids_list.split(",")) # only get valid digits for c_id in cat_ids: cat = db.get_category(c_id) db.add_category_item(item, cat) print "%d,%d,%d,%.2f,%.2f,%.2f,%d,%.2f,%s,%s" % ( itemid, item.get_price_id(), dist.get_id(), d_i.get_wholesale_price(), d_i.get_case_size(), each_cost, margin, cost, db.get_unit(item_price.get_sale_unit_id()), db.get_item_info_page_link(itemid), ) else:
if 'cat' in form: catname = form.getvalue('cat').strip() if db.is_category_byname(catname): raise Exception('category already exists') else: db.add_category(catname) cat = db.get_category_byname(catname) print '%d,%s' % (cat.get_id(), cat.get_name()) else: raise Exception('no name given to add') elif action == 'remove': catid = form.getvalue('catid') if not db.is_category(catid): raise Exception ('category not in database') else: cat = db.get_category(catid) db.remove_category(cat) print '' elif action == 'query': cat_list = [cat.get_name() for cat in db.get_categories()] print ','.join(cat_list) elif action == 'query-name': cat_list = [str(cat.get_id())+','+cat.get_name() for cat in db.get_categories()] print '\n'.join(cat_list) elif action == 'query-cat-id': if 'catname' in form: catname = form.getvalue('catname').strip() cat = db.get_category_byname(catname) print '%d,%s' % (cat.get_id(), cat.get_name()) else: raise Exception ('invalid action')