Add Item View

This commit is contained in:
2022-07-14 06:35:36 -07:00
parent b43b254a47
commit 3064a36b09
3 changed files with 121 additions and 14 deletions

View File

@@ -8,8 +8,56 @@ from typing import List, Optional, Union
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from sigl.exc import NotFoundError from sigl.exc import DomainError, NotFoundError
from .models import ListEntry, ShoppingList from .models import ListEntry, Product, ShoppingList
def list_addItem(
session: Session,
id: int,
*,
productId: Optional[int],
productName: Optional[str],
productCategory: Optional[str],
quantity: Optional[str],
notes: Optional[str],
) -> ListEntry:
"""Add a Product to a Shopping List.
If the `productId` parameter is provided, the method will look up the
product by ID and add it to the list. If the `product` parameter is not
provided, a new `Product` will be created with the provided `productName`
and `productCategory` values.
"""
sList = list_by_id(session, id)
if not sList:
raise NotFoundError(f'List {id} does not exist')
product = None
if not productId:
if not productName:
raise DomainError('Product Name cannot be empty')
product = Product(name=productName, category=productCategory)
session.add(product)
else:
product = product_by_id(session, productId)
if not product:
raise NotFoundError(f'Product {productId} does not exist')
entry = ListEntry(
shoppingList=sList,
product=product,
quantity=quantity,
notes=notes,
)
session.add(entry)
session.commit()
return entry
def lists_all(session: Session) -> List[ShoppingList]: def lists_all(session: Session) -> List[ShoppingList]:
@@ -67,8 +115,8 @@ def list_stores(session: Session, id: int) -> List[str]:
def list_update( def list_update(
session: Session, session: Session,
id: int, id: int,
name: Union[str,None], name: Union[str, None],
notes: Union[str,None], notes: Union[str, None],
) -> ShoppingList: ) -> ShoppingList:
"""Update the Name and/or Notes of a Shopping List.""" """Update the Name and/or Notes of a Shopping List."""
sList = list_by_id(session, id) sList = list_by_id(session, id)
@@ -82,3 +130,13 @@ def list_update(
session.commit() session.commit()
return sList return sList
def products_all(session: Session) -> List[Product]:
"""Return all Products."""
return session.query(Product).all()
def product_by_id(session: Session, id: int) -> Optional[Product]:
"""Load a specific Product."""
return session.query(Product).filter(Product.id == id).one_or_none()

View File

@@ -4,7 +4,7 @@
<div class="flex justify-between items-center py-2"> <div class="flex justify-between items-center py-2">
<div class="text-lg font-bold text-gray-800">{{ list.name }}</div> <div class="text-lg font-bold text-gray-800">{{ list.name }}</div>
<div class="flex justify-start items-start pr-1"> <div class="flex justify-start items-start pr-1">
<a href="#" class="px-2 py-1 text-sm text-white bg-green-600 hover:bg-green-700 border rounded flex justify-between items-center"> <a href="{{ url_for('lists.addItem', id=list.id) }}" class="px-2 py-1 text-sm text-white bg-green-600 hover:bg-green-700 border rounded flex justify-between items-center">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-1" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"> <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-1" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M12 9v3m0 0v3m0-3h3m-3 0H9m12 0a9 9 0 11-18 0 9 9 0 0118 0z" /> <path stroke-linecap="round" stroke-linejoin="round" d="M12 9v3m0 0v3m0-3h3m-3 0H9m12 0a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg> </svg>

View File

@@ -6,14 +6,15 @@ Copyright (c) 2022 Asymworks, LLC. All Rights Reserved.
from flask import ( from flask import (
Blueprint, Blueprint,
abort, flash, redirect, render_template, request, url_for flash, redirect, render_template, request, url_for
) )
from sigl.exc import Error from sigl.exc import Error
from sigl.database import db from sigl.database import db
from sigl.domain.service import ( from sigl.domain.service import (
lists_all, list_by_id, list_create, list_delete, lists_all, list_by_id, list_create, list_delete,
list_stores, list_addItem, list_stores,
products_all,
) )
__all__ = ('bp', ) __all__ = ('bp', )
@@ -73,10 +74,10 @@ def detail(id):
aisle = 'Unknown' aisle = 'Unknown'
bin = None bin = None
locs = e.product.locations locs = e.product.locations
for l in locs: for loc in locs:
if l.store.lower() == sortStore.lower(): if loc.store.lower() == sortStore.lower():
aisle = l.aisle aisle = loc.aisle
bin = l.bin bin = loc.bin
if aisle not in groups: if aisle not in groups:
groups[aisle] = [{'entry': e, 'bin': bin}] groups[aisle] = [{'entry': e, 'bin': bin}]
@@ -90,7 +91,6 @@ def detail(id):
else: else:
groups[category].append({'entry': e}) groups[category].append({'entry': e})
flash('An error occurred during the processing of this request', 'error')
return render_template( return render_template(
'lists/detail.html.j2', 'lists/detail.html.j2',
list=list_by_id(db.session, id), list=list_by_id(db.session, id),
@@ -114,3 +114,52 @@ def delete(id):
flash(str(e), 'error') flash(str(e), 'error')
return redirect(url_for('lists.home')) return redirect(url_for('lists.home'))
@bp.route('/lists/<int:id>/addItem', methods=('GET', 'POST'))
def addItem(id):
"""Add an Item to a Shopping List."""
try:
sList = list_by_id(db.session, id)
products = products_all(db.session)
if request.method == 'POST':
if 'product' not in request.form:
flash(
'An internal error occurred. Please reload the page and try again',
'error'
)
return render_template(
'/lists/addItem.html.j2',
list=sList,
products=products,
)
productId = request.form['product']
productName = request.form.get('productName', None)
productCategory = request.form.get('productCategory', None)
quantity = request.form.get('quantity', None)
notes = request.form.get('notes', None)
if productId == 'new' or productId == '':
productId = None
list_addItem(
db.session,
id,
productId=productId,
productName=productName,
productCategory=productCategory,
quantity=quantity,
notes=notes,
)
return redirect(url_for('lists.detail', id=id))
except Error as e:
flash(str(e), 'error')
return render_template(
'/lists/addItem.html.j2',
list=sList,
products=products,
)