Add Item View
This commit is contained in:
@@ -8,8 +8,56 @@ from typing import List, Optional, Union
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from sigl.exc import NotFoundError
|
||||
from .models import ListEntry, ShoppingList
|
||||
from sigl.exc import DomainError, NotFoundError
|
||||
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]:
|
||||
@@ -82,3 +130,13 @@ def list_update(
|
||||
session.commit()
|
||||
|
||||
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()
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<div class="flex justify-between items-center py-2">
|
||||
<div class="text-lg font-bold text-gray-800">{{ list.name }}</div>
|
||||
<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">
|
||||
<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>
|
||||
|
||||
@@ -6,14 +6,15 @@ Copyright (c) 2022 Asymworks, LLC. All Rights Reserved.
|
||||
|
||||
from flask import (
|
||||
Blueprint,
|
||||
abort, flash, redirect, render_template, request, url_for
|
||||
flash, redirect, render_template, request, url_for
|
||||
)
|
||||
|
||||
from sigl.exc import Error
|
||||
from sigl.database import db
|
||||
from sigl.domain.service import (
|
||||
lists_all, list_by_id, list_create, list_delete,
|
||||
list_stores,
|
||||
list_addItem, list_stores,
|
||||
products_all,
|
||||
)
|
||||
|
||||
__all__ = ('bp', )
|
||||
@@ -73,10 +74,10 @@ def detail(id):
|
||||
aisle = 'Unknown'
|
||||
bin = None
|
||||
locs = e.product.locations
|
||||
for l in locs:
|
||||
if l.store.lower() == sortStore.lower():
|
||||
aisle = l.aisle
|
||||
bin = l.bin
|
||||
for loc in locs:
|
||||
if loc.store.lower() == sortStore.lower():
|
||||
aisle = loc.aisle
|
||||
bin = loc.bin
|
||||
|
||||
if aisle not in groups:
|
||||
groups[aisle] = [{'entry': e, 'bin': bin}]
|
||||
@@ -90,7 +91,6 @@ def detail(id):
|
||||
else:
|
||||
groups[category].append({'entry': e})
|
||||
|
||||
flash('An error occurred during the processing of this request', 'error')
|
||||
return render_template(
|
||||
'lists/detail.html.j2',
|
||||
list=list_by_id(db.session, id),
|
||||
@@ -114,3 +114,52 @@ def delete(id):
|
||||
flash(str(e), 'error')
|
||||
|
||||
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,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user