def item_by_category_and_name(category_name, item_name): # Get one item in database by name item = models.get_item_by_name(item_name) # Get all categories in database categories = models.get_all_categories() # Render the page return render_template('item.html', categories=categories, item=item)
def home(): # Get all categories categories = models.get_all_categories() # Get latest items latest_items = models.get_latest_items() # Render the home page return render_template('home.html', categories=categories, latest_items=latest_items)
def items_by_category(category_name): # Get all items in database by category name items = models.get_items_by_category(category_name).all() # Get all categories in database categories = models.get_all_categories() # Get item quantity item_quantity = len(items) # Render the page return render_template('item_list.html', categories=categories, category_name=category_name, items=items, item_quantity=item_quantity)
def edit(item_name): # If its a GET render the page if request.method == 'GET': # Get item by name in database item = models.get_item_by_name(item_name) # Get all categories in database categories = models.get_all_categories() # Render the page return render_template('edit.html', categories=categories, item=item) # If its a POST update a item elif request.method == 'POST': # Get item name from form name = request.form['name'] # Get item description from form description = request.form['description'] # Get item category_name from form category_name = request.form['category_name'] # Set by default the necessity of update a item in database to false update = False new_item_name = item_name # Get item in database item = models.get_item_by_name(item_name) # Name is different from the form if item.name != name and name: item.name = name # Necessity of update a item is true update = True # Category Name is different from the form if item.category.name != category_name and category_name: # Get the new category in database category = models.get_category_by_name(category_name) # Update item category item.category = category item.category_id = category.id # Necessity of update a item is true update = True # Description is different from the form if item.description != description and description: item.description = description # Necessity of update a item is true update = True # If Necessity of update a item is true if update: # Update the item in database new_item_name = models.update_item(item) # Render the page return redirect(url_for('edit', item_name=new_item_name))
def delete(item_name): # If its a GET render the page if request.method == 'GET': # Get the item by name in database item = models.get_item_by_name(item_name) # Get all categories in database categories = models.get_all_categories() # Render the page return render_template('delete.html', categories=categories, item=item) # If its a POST delete the item in database elif request.method == 'POST': # Get the item by name in database item = models.get_item_by_name(item_name) # Remove the item by name in database models.remove_item_by_id(item.id) return redirect(url_for('home'))