class VendorTests(CRUDTest): """Test cases for the Vendors class""" def __init__(self): """Constructor""" CRUDTest.__init__(self) self.vendors = None self.last_id = None self.vendors = None def set_up(self, mysql_x, user=None, passwd=None): """Setup the test cases""" self.mygarage = self.begin(mysql_x, "Vendors", user, passwd) self.vendors = Vendors(self.mygarage) def create(self): """Run Create test case""" print("\nCRUD: Create test case") vendor_data = { "name": "ACME Bolt Company", "url": "www.acme.org", "sources": "looney toons" } self.vendors.create(vendor_data) self.last_id = self.vendors.get_last_docid() print("\tLast insert id = {0}".format(self.last_id)) def read_all(self): """Run Read(all) test case""" print("\nCRUD: Read (all) test case") docs = self.vendors.read() self.show_docs(docs, 5) def read_one(self): """Run Read(record) test case""" print("\nCRUD: Read (doc) test case") docs = self.vendors.read(self.last_id) self.show_docs(docs, 1) def update(self): """Run Update test case""" print("\nCRUD: Update test case") vendor_data = { "_id": self.last_id, "name": "ACME Nut Company", "url": "www.weesayso.co", } self.vendors.update(vendor_data) def delete(self): """Run Delete test case""" print("\nCRUD: Delete test case") self.vendors.delete(self.last_id) docs = self.vendors.read(self.last_id) if not docs: print("\tNot found (deleted).")
class ToolTests(CRUDTest): """Test cases for the Tools class""" def __init__(self): """Constructor""" CRUDTest.__init__(self) self.tool = None self.last_id = None self.vendor_id = None self.vendors = None self.tools = None def set_up(self, mysql_x, user=None, passwd=None): """Setup the test cases""" self.mygarage = self.begin(mysql_x, "Tools", user, passwd) self.tools = Tools(self.mygarage) self.vendors = Vendors(self.mygarage) self.vendor_id = self.vendors.read()[0].get('_id', None) def create(self): """Run Create test case""" print("\nCRUD: Create test case") tool_data = { "vendorid": self.vendor_id, "size": "Left-handed Philips", "type": "Screwdriver", "category": "Handtool", "description": "#3 X 6-in" } self.tools.create(tool_data) self.last_id = self.tools.get_last_docid() print("\tLast insert id = {0}".format(self.last_id)) def read_all(self): """Run Read(all) test case""" print("\nCRUD: Read (all) test case") docs = self.tools.read() self.show_docs(docs, 5) def read_one(self): """Run Read(record) test case""" print("\nCRUD: Read (doc) test case") docs = self.tools.read(self.last_id) self.show_docs(docs, 1) def update(self): """Run Update test case""" print("\nCRUD: Update test case") tool_data = { "_id": self.last_id, "size": "Right-handed Philips", } self.tools.update(tool_data) def delete(self): """Run Delete test case""" print("\nCRUD: Delete test case") self.tools.delete(self.last_id) docs = self.tools.read(self.last_id) if not docs: print("\tNot found (deleted).")
def set_up(self, mysql_x, user=None, passwd=None): """Setup the test cases""" self.mygarage = self.begin(mysql_x, "Workbenches", user, passwd) self.workbenches = Workbenches(self.mygarage) self.vendors = Vendors(self.mygarage) self.vendor_id = self.vendors.read()[0].get('_id', None)
class WorkbenchTests(CRUDTest): """Test cases for the Wrokbenches class""" def __init__(self): """Constructor""" CRUDTest.__init__(self) self.workbench = None self.last_id = None self.vendor_id = None self.vendors = None self.workbenches = None def set_up(self, mysql_x, user=None, passwd=None): """Setup the test cases""" self.mygarage = self.begin(mysql_x, "Workbenches", user, passwd) self.workbenches = Workbenches(self.mygarage) self.vendors = Vendors(self.mygarage) self.vendor_id = self.vendors.read()[0].get('_id', None) def create(self): """Run Create test case""" print("\nCRUD: Create test case") workbench_data = { "vendorid": self.vendor_id, "description": "Loft", "shelves": [ { "depth": 24, "description": "Bottom Left", "height": 10, "width": 12 }, { "depth": 24, "description": "Bottom Right", "height": 10, "width": 12 }, ], "width": 11, "depth": 11, "height": 11, "location": "Ceiling", } self.workbenches.create(workbench_data) self.last_id = self.workbenches.get_last_docid() print("\tLast insert id = {0}".format(self.last_id)) def read_all(self): """Run Read(all) test case""" print("\nCRUD: Read (all) test case") docs = self.workbenches.read() self.show_docs(docs, 5) def read_one(self): """Run Read(record) test case""" print("\nCRUD: Read (doc) test case") docs = self.workbenches.read(self.last_id) self.show_docs(docs, 1) def update(self): """Run Update test case""" print("\nCRUD: Update test case") workbench_data = { "_id": self.last_id, "description": "Cloud Storage", "drawers": [{ "depth": 17, "description": "Top", "height": 4, "tool_ids": [ "00005cafa3eb00000000000007c5", "00005cafa3eb00000000000007c6", "00005cafa3eb00000000000007c7", ], "width": 21 }], "location": "3rd floor basement", } self.workbenches.update(workbench_data) def delete(self): """Run Delete test case""" print("\nCRUD: Delete test case") self.workbenches.delete(self.last_id) docs = self.workbenches.read(self.last_id) if not docs: print("\tNot found (deleted).")
def set_up(self, mysql_x, user=None, passwd=None): """Setup the test cases""" self.mygarage = self.begin(mysql_x, "Vendors", user, passwd) self.vendors = Vendors(self.mygarage)
class ToolchestTests(CRUDTest): """Test cases for the Toolchests class""" def __init__(self): """Constructor""" CRUDTest.__init__(self) self.toolchest = None self.last_id = None self.vendor_id = None self.vendors = None self.toolchests = None def set_up(self, mysql_x, user=None, passwd=None): """Setup the test cases""" self.mygarage = self.begin(mysql_x, "Toolchests", user, passwd) self.toolchests = Toolchests(self.mygarage) self.vendors = Vendors(self.mygarage) self.vendor_id = self.vendors.read()[0].get('_id', None) def create(self): """Run Create test case""" print("\nCRUD: Create test case") toolchest_data = { "vendorid": self.vendor_id, "description": "Kobalt 3000 Steel Rolling Tool Cabinet (Black)", "location": "Rear wall right of workbench", "drawers": [ { "depth": 17, "description": "Top", "height": 2, "tool_ids": [ "00005cb1065c000000000000014c", "00005cb1065c000000000000014d", "00005cb1065c000000000000015c", "00005cb1065c000000000000015d", "00005cb1065c000000000000015f", "00005cb1065c0000000000000160" ], "width": 21 }, ], "shelves": [ { "depth": 18, "description": "Top", "height": 5, "width": 45 }, ], "depth": 22, "width": 48, "height": 54, } self.toolchests.create(toolchest_data) self.last_id = self.toolchests.get_last_docid() print("\tLast insert id = {0}".format(self.last_id)) def read_all(self): """Run Read(all) test case""" print("\nCRUD: Read (all) test case") docs = self.toolchests.read() self.show_docs(docs, 5) def read_one(self): """Run Read(record) test case""" print("\nCRUD: Read (doc) test case") docs = self.toolchests.read(self.last_id) self.show_docs(docs, 1) def update(self): """Run Update test case""" print("\nCRUD: Update test case") toolchest_data = { "_id": self.last_id, "location": "Rear wall left of workbench", "drawers": [ { "depth": 17, "description": "Top", "height": 2, "tool_ids": ["00005cb1065c0000000000000160"], "width": 21 }, ], "shelves": [ { "depth": 18, "description": "Top", "height": 5, "width": 45 }, { "depth": 18, "description": "Bottom", "height": 5, "width": 45, "tool_ids": [ "00005cb1065c000000000000014d", "00005cb1065c000000000000015c", "00005cb1065c000000000000015d", "00005cb1065c000000000000015f", ] }, ], "depth": 22, "width": 48, "height": 54, } self.toolchests.update(toolchest_data) def delete(self): """Run Delete test case""" print("\nCRUD: Delete test case") self.toolchests.delete(self.last_id) docs = self.toolchests.read(self.last_id) if not docs: print("\tNot found (deleted).")
class LocationTests(CRUDTest): """Test cases for the Locations class""" def __init__(self): """Constructor""" CRUDTest.__init__(self) self.tool = None self.last_id = None self.vendor_id = None self.vendors = None self.locations = None def set_up(self, mysql_x, user=None, passwd=None): """Setup the test cases""" self.mygarage = self.begin(mysql_x, "Locations", user, passwd) self.locations = Locations(self.mygarage) self.vendors = Vendors(self.mygarage) self.vendor_id = self.vendors.read()[0].get('_id', None) def create(self): """Run Create test case""" print("\nCRUD: Create test case") location_data = { "type": "Shelf", "description": "Top", "height": 4, "width": 5, "depth": 12, "tool_ids": [ "00005cb8f74200000000000003bd", "00005cb8f74200000000000003bf", "00005cb8f74200000000000003c2", "00005cb8f74200000000000003c3", "00005cb8f74200000000000003c4", "00005cb8f74200000000000003c7", "00005cb8f74200000000000003c8", ] } self.locations.create(location_data) self.last_id = self.locations.get_last_docid() print("\tLast insert id = {0}".format(self.last_id)) def read_all(self): """Run Read(all) test case""" print("\nCRUD: Read (all) test case") docs = self.locations.read() self.show_docs(docs, 5) def read_one(self): """Run Read(record) test case""" print("\nCRUD: Read (doc) test case") docs = self.locations.read(self.last_id) self.show_docs(docs, 1) def update(self): """Run Update test case""" print("\nCRUD: Update test case") location_data = { "_id": self.last_id, "type": "Drawer", "description": "Bottom", "height": 2, "width": 22, "depth": 18, "tool_ids": [ "00005cb8f74200000000000003c7", "00005cb8f74200000000000003c8", ] } self.locations.update(location_data) def delete(self): """Run Delete test case""" print("\nCRUD: Delete test case") self.locations.delete(self.last_id) docs = self.locations.read(self.last_id) if not docs: print("\tNot found (deleted).")
def set_up(self, mysql_x, user=None, passwd=None): """Setup the test cases""" self.mygarage = self.begin(mysql_x, "Locations", user, passwd) self.locations = Locations(self.mygarage) self.vendors = Vendors(self.mygarage) self.vendor_id = self.vendors.read()[0].get('_id', None)
class ShelvingUnitTests(CRUDTest): """Test cases for the ShelvingUnits class""" def __init__(self): """Constructor""" CRUDTest.__init__(self) self.shelving_unit = None self.last_id = None self.vendor_id = None self.vendors = None self.shelving_units = None def set_up(self, mysql_x, user=None, passwd=None): """Setup the test cases""" self.mygarage = self.begin(mysql_x, "ShelvingUnits", user, passwd) self.shelving_units = ShelvingUnits(self.mygarage) self.vendors = Vendors(self.mygarage) self.vendor_id = self.vendors.read()[0].get('_id', None) def create(self): """Run Create test case""" print("\nCRUD: Create test case") shelving_unit_data = { "vendorid": self.vendor_id, "description": "Wire shelving #3", "location": "Right wall", "shelves": [ { "depth": 24, "description": "Top", "height": 18, "width": 48 }, { "depth": 24, "description": "Bottom", "height": 18, "width": 48 } ], "depth": 24, "width": 48, "height": 72, } self.shelving_units.create(shelving_unit_data) self.last_id = self.shelving_units.get_last_docid() print("\tLast insert id = {0}".format(self.last_id)) def read_all(self): """Run Read(all) test case""" print("\nCRUD: Read (all) test case") docs = self.shelving_units.read() self.show_docs(docs, 5) def read_one(self): """Run Read(record) test case""" print("\nCRUD: Read (doc) test case") docs = self.shelving_units.read(self.last_id) self.show_docs(docs, 1) def update(self): """Run Update test case""" print("\nCRUD: Update test case") shelving_unit_data = { "_id": self.last_id, "shelves": [ { "depth": 24, "description": "Top", "height": 18, "width": 48 }, { "depth": 24, "description": "Middle", "height": 18, "width": 48, "tool_ids": [ "00005cafa3eb00000000000007c5", "00005cafa3eb00000000000007c6", "00005cafa3eb00000000000007c7", ], }, { "depth": 24, "description": "Bottom", "height": 18, "width": 48 } ], } self.shelving_units.update(shelving_unit_data) def delete(self): """Run Delete test case""" print("\nCRUD: Delete test case") self.shelving_units.delete(self.last_id) docs = self.shelving_units.read(self.last_id) if not docs: print("\tNot found (deleted).")
def set_up(self, mysql_x, user=None, passwd=None): """Setup the test cases""" self.mygarage = self.begin(mysql_x, "ShelvingUnits", user, passwd) self.shelving_units = ShelvingUnits(self.mygarage) self.vendors = Vendors(self.mygarage) self.vendor_id = self.vendors.read()[0].get('_id', None)
class CabinetTests(CRUDTest): """Test cases for the Cabinets class""" def __init__(self): """Constructor""" CRUDTest.__init__(self) self.cabinet = None self.last_id = None self.vendor_id = None self.vendors = None self.cabinets = None def set_up(self, mysql_x, user=None, passwd=None): """Setup the test cases""" self.mygarage = self.begin(mysql_x, "Cabinets", user, passwd) self.cabinets = Cabinets(self.mygarage) self.vendors = Vendors(self.mygarage) self.vendor_id = self.vendors.read()[0].get('_id', None) def create(self): """Run Create test case""" print("\nCRUD: Create test case") cabinet_data = { "vendorid": self.vendor_id, "description": "Large freestanding cabinet", "shelves": [ { "depth": 20, "description": "Middle", "height": 18, "width": 48 }, ], "numdoors": 2, "width": 11, "depth": 11, "height": 11, "location": "Read wall next to compressor", } self.cabinets.create(cabinet_data) self.last_id = self.cabinets.get_last_docid() print("\tLast insert id = {0}".format(self.last_id)) def read_all(self): """Run Read(all) test case""" print("\nCRUD: Read (all) test case") docs = self.cabinets.read() self.show_docs(docs, 5) def read_one(self): """Run Read(record) test case""" print("\nCRUD: Read (doc) test case") docs = self.cabinets.read(self.last_id) self.show_docs(docs, 1) def update(self): """Run Update test case""" print("\nCRUD: Update test case") cabinet_data = { "_id": self.last_id, "description": "Cold Storage", "shelves": [{ "depth": 20, "description": "Top", "height": 18, "width": 48 }, { "depth": 20, "description": "Bottom", "height": 18, "width": 48, "tool_ids": [ "00005cafa3eb00000000000007c5", "00005cafa3eb00000000000007c6", "00005cafa3eb00000000000007c7", ], }], "location": "3rd floor basement", } self.cabinets.update(cabinet_data) def delete(self): """Run Delete test case""" print("\nCRUD: Delete test case") self.cabinets.delete(self.last_id) docs = self.cabinets.read(self.last_id) if not docs: print("\tNot found (deleted).")
def tool(tool_id=None): """Manage tool CRUD operations.""" collection = Tools(mygarage) form = ToolForm() # Get data from the form if present form_toolid = form.toolid.data # Tool type choices vendor_data = Vendors(mygarage) vendors = vendor_data.read() vendor_list = [] for item in vendors: vendor_list.append((item["_id"], item["name"])) form.vendor.choices = vendor_list form.location.choices = mygarage.get_locations() form_vendor = form.vendor.data form_desc = form.description.data form_location = form.location.data form_category = form.category.data form_toolsize = form.toolsize.data form_tooltype = form.tooltype.data # If the route with the variable is called, change the create button to update # then populate the form with the data from the row in the table. Otherwise, # remove the delete button because this will be a new data item. if tool_id: form.create_button.label.text = "Update" # Here, we get the data and populate the form data = collection.read(tool_id) if data == []: flash("Tool not found!") data_dict = dict(data[0]) form.toolid.data = data_dict.get("_id", None) form.vendorid.data = data_dict.get("vendorid", None) form.vendor.process_data(data_dict.get("vendorid", None)) form.description.data = data_dict.get("description", None) form.location.choices = mygarage.get_locations() form.location.process_data(data_dict.get("location", None)) form.category.process_data(data_dict.get("category", None)) form.toolsize.data = data_dict.get("size", None) form.tooltype.process_data(data_dict.get("type", None)) else: del form.del_button if request.method == 'POST': # First, determine if we must create, update, or delete when form posts. operation = "Create" if form.close_button.data: return redirect('/list/tools') if form.create_button.data: if form.create_button.label.text == "Update": operation = "Update" if form.del_button and form.del_button.data: operation = "Delete" if form.validate_on_submit(): # Get the data from the form here if operation == "Create": try: tool_data = { "vendorid": form_vendor, "description": form_desc, "type": form_tooltype, "size": form_toolsize, "location": form_location, "category": form_category, } collection.create(tool_data) flash("Added.") return redirect('/list/tools') except Exception as err: flash(err) elif operation == "Update": try: tool_data = { "_id": tool_id, "vendorid": form_vendor, "description": form_desc, "type": form_tooltype, "size": form_toolsize, "location": form_location, "category": form_category, } collection.update(tool_data) flash("Updated.") return redirect('/list/tools') except Exception as err: flash(err) else: try: mygarage.get_session().start_transaction() locations = Locations(mygarage) locations.remove_tool(form_toolid) organizers = Organizers(mygarage) organizers.remove_tool(form_toolid) collection.delete(form_toolid) mygarage.get_session().commit() flash("Deleted.") return redirect('/list/tools') except Exception as err: flash(err) else: flash_errors(form) return render_template("tool.html", form=form)
def storage_places(kind=None, storage_place_id=None): """Manage storage place CRUD operations.""" if kind == 'cabinets': collection = Cabinets(mygarage) collection_str = 'Cabinet' elif kind == 'shelving_units': collection = ShelvingUnits(mygarage) collection_str = 'Shelving Unit' elif kind == 'toolchests': collection = Toolchests(mygarage) collection_str = 'Toolchest' elif kind == 'workbenches': collection = Workbenches(mygarage) collection_str = 'Workbench' elif kind == 'tools': # Redirect! flash("Must redirect to tools.") elif kind == 'organizers': # Redirect! flash("Must redirect to organizers.") else: flash("Something is wrong. Wrong 'kind' specified for the detail.") form = StoragePlaceForm() # Get data from the form if present form_storageid = form.storageid.data vendor_data = Vendors(mygarage) vendors = vendor_data.read() vendor_list = [] for item in vendors: vendor_list.append((item["_id"], item["name"])) form.vendor.choices = vendor_list form_vendor = form.vendor.data form_desc = form.description.data # Only cabinets have doors if kind == 'cabinets': form_ndoors = form.numdoors.data else: del form.numdoors form_width = form.width.data form_depth = form.depth.data form_height = form.height.data form_location = form.location.data tool_locations = None # If the route with the variable is called, change the create button to update # then populate the form with the data from the row in the table. Otherwise, # remove the delete button because this will be a new data item. if storage_place_id: if kind == 'cabinets': form.caption.label = "Cabinet" elif kind == 'shelving_units': form.caption.label = "Shelving Unit" elif kind == 'toolchests': form.caption.label = "Toolchest" elif kind == 'workbenches': form.caption.label = "Workbench" form.create_button.label.text = "Update" # Here, we get the data and populate the form data = collection.read(storage_place_id) if data == []: flash("The {0} was not found!".format(collection_str)) data_dict = dict(data[0]) form.vendorid.data = data_dict.get("vendorid", None) form.vendor.process_data(data_dict.get("vendorid", None)) form.storageid.data = data_dict.get("_id", None) form.description.data = data_dict.get("description", None) if kind == 'cabinets': form.numdoors.data = data_dict.get("numdoors", None) form.width.data = data_dict.get("width", None) form.depth.data = data_dict.get("depth", None) form.height.data = data_dict.get("height", None) form.location.data = data_dict.get("location", None) places = data_dict.get("tool_locations") tool_locations = mygarage.build_storage_contents(places) else: del form.del_button del form.manage_tool_locations_button if request.method == 'POST': # First, determine if we must create, update, or delete when form posts. operation = "Create" if form.close_button.data: return redirect('/list/{0}'.format(kind)) if form.create_button.data: if form.create_button.label.text == "Update": operation = "Update" if form.del_button and form.del_button.data: operation = "Delete" if form.manage_tool_locations_button and form.manage_tool_locations_button.data: return redirect('/list/tool_locations/{0}/{1}'.format( kind, form.storageid.data)) if form.validate_on_submit(): # Get the data from the form here if operation == "Create": try: storage_place_data = { "vendorid": form_vendor, "description": form_desc, "width": form_width, "depth": form_depth, "height": form_height, "location": form_location, } if kind == 'cabinets': storage_place_data.update({"numdoors": form_ndoors}) collection.create(storage_place_data) flash("Added.") return redirect('/list/{0}'.format(kind)) except Exception as err: flash(err) elif operation == "Update": try: storage_place_data = { "_id": storage_place_id, "vendorid": form_vendor, "description": form_desc, "width": form_width, "depth": form_depth, "height": form_height, "location": form_location, } if kind == 'cabinets': storage_place_data.update({"numdoors": form_ndoors}) collection.update(storage_place_data) flash("Updated.") return redirect('/list/{0}'.format(kind)) except Exception as err: flash(err) else: try: mygarage.get_session().start_transaction() locations = Locations(mygarage) if places: for loc_id in places: locations.delete(loc_id) collection.delete(form_storageid) mygarage.get_session().commit() flash("Deleted.") return redirect('/list/{0}'.format(kind)) except Exception as err: flash(err) else: flash_errors(form) return render_template("storage_place.html", form=form, tool_locations=tool_locations, tool_locations_columns=TOOL_LOCATIONS_COLUMNS)
def simple_list(kind=None, storage_type=None, docid=None): """Display the simple list and landing page""" form = ListForm() if kind == 'cabinets': form.form_name.label = 'Cabinets' if request.method == 'POST': return redirect('storage_places/cabinets') columns = ( '<td style="width:500px"><b>Description</b></td>', '<td style="width:300px"><b>Location</b></td>', ) kind = 'cabinets' cabinet_data = Cabinets(mygarage) rows = make_list(cabinet_data.read(), ['_id', 'description', 'location']) del form.back_button return render_template("list.html", form=form, rows=rows, columns=columns, kind=kind, redirect='storage_places') elif kind == 'organizers': form.form_name.label = 'Organizers' if request.method == 'POST': return redirect('organizers') columns = ( '<td style="width:100px"><b>Type</b></td>', '<td style="width:300px"><b>Description</b></td>', '<td style="width:400px"><b>Location</b></td>', ) kind = 'organizers' organizer_data = Organizers(mygarage) rows = make_list(organizer_data.read(), ['_id', 'type', 'description', 'location']) del form.back_button return render_template("list.html", form=form, rows=rows, columns=columns, kind=kind, redirect='storage_places') elif kind == 'shelving_units': form.form_name.label = 'Shelving_units' if request.method == 'POST': return redirect('storage_places/shelving_units') columns = ( '<td style="width:300px"><b>Description</b></td>', '<td style="width:400px"><b>Location</b></td>', ) kind = 'shelving_units' workbench_data = ShelvingUnits(mygarage) rows = make_list(workbench_data.read(), ['_id', 'description', 'location']) del form.back_button return render_template("list.html", form=form, rows=rows, columns=columns, kind=kind, redirect='storage_places') elif kind == 'toolchests' or not kind: form.form_name.label = 'Toolchests' if request.method == 'POST': return redirect('storage_places/toolchests') columns = ( '<td style="width:300px"><b>Description</b></td>', '<td style="width:400px"><b>Location</b></td>', ) kind = 'toolchests' toolchest_data = Toolchests(mygarage) rows = make_list(toolchest_data.read(), ['_id', 'description', 'location']) del form.back_button return render_template("list.html", form=form, rows=rows, columns=columns, kind=kind, redirect='storage_places') elif kind == 'tools': form.form_name.label = 'Tools' if request.method == 'POST': return redirect('tools') columns = TOOL_COLUMNS kind = 'tools' workbench_data = Tools(mygarage) rows = make_list( workbench_data.read(), ['_id', 'category', 'type', 'size', 'description', 'location']) del form.back_button return render_template("list.html", form=form, rows=rows, columns=columns, kind=kind, redirect=None) elif kind == 'vendors': form.form_name.label = 'Vendors' if request.method == 'POST': return redirect('vendors') columns = ( '<td style="width:200px"><b>Name</b></td>', '<td style="width:400px"><b>URL</b></td>', '<td style="width:200px"><b>Sources</b></td>', ) kind = 'vendors' vendor_data = Vendors(mygarage) rows = make_list(vendor_data.read(), ['_id', 'name', 'url', 'sources']) del form.back_button return render_template("list.html", form=form, rows=rows, columns=columns, kind=kind, redirect=None) elif kind == 'workbenches': form.form_name.label = 'Workbenches' if request.method == 'POST': return redirect('storage_places/workbenches') columns = ( '<td style="width:300px"><b>Description</b></td>', '<td style="width:400px"><b>Location</b></td>', ) kind = 'workbenches' workbench_data = Workbenches(mygarage) rows = make_list(workbench_data.read(), ['_id', 'description', 'location']) del form.back_button return render_template("list.html", form=form, rows=rows, columns=columns, kind=kind, redirect='storage_places') elif kind == 'tool_locations': label_str = storage_type.rstrip('s').replace('_', ' ') form.form_name.label = 'Tool locations for {0}'.format(label_str) if request.method == 'POST': if form.submit.data: return redirect('/tool_locations/{0}/{1}'.format( storage_type, docid)) return redirect('/storage_places/{0}/{1}'.format( storage_type, docid)) columns = ( '<td style="width:100px"><b>Type</b></td>', '<td style="width:300px"><b>Description</b></td>', '<td style="width:100px"><b>Height</b></td>', '<td style="width:100px"><b>Width</b></td>', '<td style="width:100px"><b>Depth</b></td>', ) if storage_type == 'cabinets': collection = Cabinets(mygarage) elif storage_type == 'shelving_units': collection = ShelvingUnits(mygarage) elif storage_type == 'toolchests': collection = Toolchests(mygarage) else: collection = Workbenches(mygarage) locations = collection.get_tool_locations(docid) rows = [] for item in locations: loc_dict = dict(item) rows.append((loc_dict.get("_id"), loc_dict.get("type", None), loc_dict.get("description", None), loc_dict.get("height", None), loc_dict.get("width", None), loc_dict.get("depth", None))) return render_template("list.html", form=form, rows=rows, columns=columns, kind='tool_location/{0}/{1}'.format( storage_type, docid), redirect=None) else: flash("Something is wrong. No 'kind' specified for list. " "Got this: {0}".format(kind)) return None
def vendor(vendor_id=None): """Manage vendor CRUD operations.""" vendor_collection = Vendors(mygarage) form = VendorForm() # Get data from the form if present form_vendorid = form.vendorid.data form_name = form.name.data form_url = form.url.data form_sources = form.sources.data # If the route with the variable is called, change the create button to update # then populate the form with the data from the row in the table. Otherwise, # remove the delete button because this will be a new data item. if vendor_id: form.create_button.label.text = "Update" # Here, we get the data and populate the form data = vendor_collection.read(vendor_id) if data == []: flash("Vendor not found!") data_dict = dict(data[0]) form.vendorid.data = data_dict.get("_id", None) form.name.data = data_dict.get("name", None) form.url.data = data_dict.get("url", None) form.sources.data = data_dict.get("sources", None) else: del form.del_button if request.method == 'POST': # First, determine if we must create, update, or delete when form posts. operation = "Create" if form.close_button.data: return redirect('/list/vendors') if form.create_button.data: if form.create_button.label.text == "Update": operation = "Update" if form.del_button and form.del_button.data: operation = "Delete" if form.validate_on_submit(): # Get the data from the form here if operation == "Create": try: vendor_data = { "name": form_name, "url": form_url, "sources": form_sources, } res = vendor_collection.create(vendor_data) if res[0]: flash("Added.") else: flash("Cannot add vendor: {0}".format(res[1])) return redirect('/list/vendors') except Exception as err: flash(err) elif operation == "Update": try: vendor_data = { "_id": form.vendorid.data, "name": form_name, "url": form_url, "sources": form_sources, } res = vendor_collection.update(vendor_data) if res[0]: flash("Updated.") else: flash("Cannot update vendor: {0}".format(res[1])) return redirect('/list/vendors') except Exception as err: flash(err) else: try: if not mygarage.vendor_in_use(form_vendorid): res = vendor_collection.delete(form_vendorid) if res[0]: flash("Deleted.") else: flash("Cannot delete vendor: {0}".format(res[1])) else: flash("Vendor {0} in use. Cannot delete.".format( form_name)) return redirect('/list/vendors') except Exception as err: flash(err) else: flash_errors(form) return render_template("vendor.html", form=form)