From 2d313a9e75dfe5ba58e934afc9140fd7f8c634bd Mon Sep 17 00:00:00 2001 From: "J.P. Krauss" Date: Thu, 14 Jul 2022 09:56:03 -0700 Subject: [PATCH] Added Views --- sigl/domain/service.py | 44 ++++++++++-- sigl/templates/lists/detail.html.j2 | 96 +++++++++++++++++---------- sigl/templates/lists/editItem.html.j2 | 29 ++++++++ sigl/templates/lists/update.html.j2 | 44 ++++++++++++ sigl/views/lists.py | 59 +++++++++++++++- 5 files changed, 229 insertions(+), 43 deletions(-) create mode 100644 sigl/templates/lists/editItem.html.j2 create mode 100644 sigl/templates/lists/update.html.j2 diff --git a/sigl/domain/service.py b/sigl/domain/service.py index 7e165fa..20907ae 100644 --- a/sigl/domain/service.py +++ b/sigl/domain/service.py @@ -89,6 +89,34 @@ def list_delete(session: Session, id: int): session.commit() +def list_deleteItem(session: Session, listId: int, entryId: int): + """Delete an Entry from a Shopping List.""" + entry = list_entry_by_id(session, listId, entryId) + + session.delete(entry) + session.commit() + + +def list_editItem( + session: Session, + listId: int, + entryId: int, + *, + quantity: Optional[str], + notes: Optional[str], +) -> ListEntry: + """Edit an Entry on a Shopping List.""" + entry = list_entry_by_id(session, listId, entryId) + entry.quantity = quantity + entry.notes = notes + entry.set_modified_at() + + session.add(entry) + session.commit() + + return entry + + def list_stores(session: Session, id: int) -> List[str]: """Get a list of all Stores for the List. @@ -132,18 +160,13 @@ def list_update( return sList -def list_entry_by_id(session: Session, id: int) -> Optional[ListEntry]: +def list_entry_by_id(session: Session, listId: int, entryId: int) -> Optional[ListEntry]: """Load a specific Shopping List Entry.""" - return session.query(ListEntry).filter(ListEntry.id == id).one_or_none() - - -def list_entry_set_crossedOff(session: Session, listId: int, entryId: int, crossedOff: bool) -> ListEntry: - """Set the Crossed-Off state of a List Entry.""" sList = list_by_id(session, listId) if not sList: raise NotFoundError(f'List {listId} not found') - entry = list_entry_by_id(session, entryId) + entry = session.query(ListEntry).filter(ListEntry.id == entryId).one_or_none() if not entry: raise NotFoundError(f'List Entry {entryId} not found') @@ -153,6 +176,13 @@ def list_entry_set_crossedOff(session: Session, listId: int, entryId: int, cross status_code=422, ) + return entry + + +def list_entry_set_crossedOff(session: Session, listId: int, entryId: int, crossedOff: bool) -> ListEntry: + """Set the Crossed-Off state of a List Entry.""" + entry = list_entry_by_id(session, listId, entryId) + entry.crossedOff = crossedOff session.add(entry) session.commit() diff --git a/sigl/templates/lists/detail.html.j2 b/sigl/templates/lists/detail.html.j2 index f35b30f..0f29b9e 100644 --- a/sigl/templates/lists/detail.html.j2 +++ b/sigl/templates/lists/detail.html.j2 @@ -1,31 +1,41 @@ {% extends 'base.html.j2' %} {% block title %}{{ list.name }} | Sigl{% endblock %} {% block header %} -
-
{{ list.name }}
-
- - - - - Add Item - - - - - - - +
+
+
{{ list.name }}
+
+ + + + + Add Item + +{% if list.notes %} + +{% endif %} + + + + + +
+{% if list.notes %} +
{{ list.notes}}
+{% endif %}
{% endblock %} {% block main %}
-
+
+
+
+ + +
+
+ +
+
+ +{% endblock %} \ No newline at end of file diff --git a/sigl/templates/lists/update.html.j2 b/sigl/templates/lists/update.html.j2 new file mode 100644 index 0000000..e8f43c1 --- /dev/null +++ b/sigl/templates/lists/update.html.j2 @@ -0,0 +1,44 @@ +{% extends 'base.html.j2' %} +{% block title %}{{ list.name }} | Sigl{% endblock %} +{% block header %} +
+
Edit {{ list.name }}
+
+ + Cancel + +
+
+{% endblock %} +{% block main %} +
+
+
+ + +
+
+ + +
+
+ +
+
+
+
+ +{% endblock %} \ No newline at end of file diff --git a/sigl/views/lists.py b/sigl/views/lists.py index 8550a25..7caf3a4 100644 --- a/sigl/views/lists.py +++ b/sigl/views/lists.py @@ -12,8 +12,9 @@ from flask import ( from sigl.exc import DomainError, Error, NotFoundError from sigl.database import db from sigl.domain.service import ( - lists_all, list_by_id, list_create, list_delete, - list_addItem, list_stores, list_entry_set_crossedOff, + list_entry_by_id, lists_all, list_by_id, list_create, list_delete, + list_update, list_addItem, list_editItem, list_stores, + list_entry_set_crossedOff, products_all, ) @@ -108,6 +109,31 @@ def detail(id): return redirect(url_for('lists.home')) +@bp.route('/lists//update', methods=('GET', 'POST')) +def update(id): + """Update a Shopping List.""" + try: + sList = list_by_id(db.session, id) + + if request.method == 'POST': + list_update( + db.session, + id, + name=request.form.get('name', sList.name), + notes=request.form.get('notes', sList.notes), + ) + return redirect(url_for('lists.detail', id=id)) + + return render_template( + 'lists/update.html.j2', + list=sList, + ) + + except Error as e: + flash(str(e), 'error') + return redirect(url_for('lists.detail', id=id)) + + @bp.route('/lists//delete', methods=('POST', )) def delete(id): """Delete a Shopping List.""" @@ -204,3 +230,32 @@ def addItem(id): list=sList, products=products, ) + + +@bp.route('/lists//editItem/', methods=('GET', 'POST')) +def editItem(listId, entryId): + """Edit an Item on a Shopping List.""" + try: + entry = list_entry_by_id(db.session, listId, entryId) + if request.method == 'POST': + quantity = request.form.get('quantity', None) + notes = request.form.get('notes', None) + + list_editItem( + db.session, + listId, + entryId, + quantity=quantity, + notes=notes, + ) + + return redirect(url_for('lists.detail', id=listId)) + + except Error as e: + flash(str(e), 'error') + + return render_template( + '/lists/editItem.html.j2', + list=entry.shoppingList, + entry=entry, + )