"""Sigl Products View Blueprint. Simple Grocery List (Sigl) | sigl.app Copyright (c) 2022 Asymworks, LLC. All Rights Reserved. """ from flask import Blueprint, flash, redirect, render_template, request, url_for from sigl.database import db from sigl.domain.service import ( list_stores, product_addLocation, product_by_id, product_create, product_delete, product_removeLocation, product_update, products_all, ) from sigl.exc import Error, NotFoundError __all__ = ('bp', ) #: Lists Blueprint bp = Blueprint('products', __name__) @bp.route('/products') def home(): """All Products View.""" products = products_all(db.session) groups = dict() for product in products: cat = 'No Category' if product.category: cat = product.category if cat not in groups: groups[cat] = [product] else: groups[cat].append(product) return render_template('products/home.html.j2', products=products, groups=groups) @bp.route('/products/new', methods=('GET', 'POST')) def create(): """Create a new Product.""" if request.method == 'POST': product_name = request.form['name'].strip() product_category = request.form['category'].strip() product_notes = request.form['notes'].strip() if not product_name: flash('Error: Product Name is required') return render_template('products/create.html.j2') product = product_create( db.session, product_name, category=product_category, notes=product_notes, ) return redirect(url_for('products.detail', id=product.id)) return render_template('products/create.html.j2') @bp.route('/products/', methods=('GET', 'POST')) def detail(id): """Product Detail/Editing View.""" try: product = product_by_id(db.session, id) if not product: raise NotFoundError(f'Product {id} not found') except Error as e: flash(str(e), 'error') return redirect(url_for('products.home')) if request.method == 'POST': try: name = request.form['name'].strip() category = request.form['category'].strip() notes = request.form['notes'].strip() product_update( db.session, id, name, category, notes, ) return redirect(url_for('products.home')) except Error as e: flash(str(e), 'error') return render_template( 'products/detail.html.j2', product=product, stores=list_stores(db.session, None), ) @bp.route('/products//delete', methods=('POST', )) def delete(id): """Delete a Product.""" try: product_delete(db.session, id) except Error as e: flash(str(e), 'error') return redirect(url_for('products.home')) @bp.route('/products//addLocation', methods=('POST', )) def addLocation(id): """Add a Location to a Product.""" store = request.form['store'].strip() aisle = request.form['aisle'].strip() bin = request.form['bin'].strip() try: product_addLocation(db.session, id, store, aisle=aisle, bin=bin) except Error as e: flash(str(e), 'error') return redirect(url_for('products.detail', id=id)) @bp.route('/products//removeLocation', methods=('POST', )) def removeLocation(id): """Remove a Location from a Product.""" store = request.form['store'].strip() print(request.form) try: product_removeLocation(db.session, id, store) except Error as e: flash(str(e), 'error') return redirect(url_for('products.detail', id=id))