From c46daee498ec9d187b96b801baaf7fb940cb741a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20K=C3=B6rber?= Date: Fri, 16 Sep 2022 08:19:41 +0200 Subject: [PATCH] Restructure --- db.sqlite | 0 python_flask/packager/__init__.py | 68 +++++++++++-------- .../packager/components/CategoryList.py | 35 ++++++++++ python_flask/packager/components/ItemList.py | 33 +++++++++ .../components/PackageListItemManager.py | 21 ++++++ python_flask/packager/components/__init__.py | 4 ++ python_flask/packager/models.py | 19 +++++- python_flask/packager/views.py | 49 ++++++++++++- 8 files changed, 195 insertions(+), 34 deletions(-) delete mode 100644 db.sqlite create mode 100644 python_flask/packager/components/CategoryList.py create mode 100644 python_flask/packager/components/ItemList.py create mode 100644 python_flask/packager/components/PackageListItemManager.py diff --git a/db.sqlite b/db.sqlite deleted file mode 100644 index e69de29..0000000 diff --git a/python_flask/packager/__init__.py b/python_flask/packager/__init__.py index a59ec44..11cad38 100644 --- a/python_flask/packager/__init__.py +++ b/python_flask/packager/__init__.py @@ -1,5 +1,6 @@ import uuid import sqlalchemy +import csv from flask import Flask from .helpers import * @@ -14,42 +15,51 @@ db = SQLAlchemy(app) from packager.models import * import packager.views - db.create_all() + try: - db.session.add( - PackageList( - id="ab2f16c2-d5f5-460b-b149-0fc9eec12887", - name="EDC", - description="What you always carry", - ) - ) - db.session.add( - PackageList( - id="9f3a72cd-7e30-4263-bd52-92fb7bed1242", - name="Camping", - description="For outdoors", - ) + categories = ( + {"id": uuid.uuid4(), "name": "Sleeping"}, + {"id": uuid.uuid4(), "name": "Shelter"}, + {"id": uuid.uuid4(), "name": "Fire"}, + {"id": uuid.uuid4(), "name": "Cooking"}, + {"id": uuid.uuid4(), "name": "Water"}, + {"id": uuid.uuid4(), "name": "Protection"}, + {"id": uuid.uuid4(), "name": "Tools"}, + {"id": uuid.uuid4(), "name": "Insulation"}, + {"id": uuid.uuid4(), "name": "Electronics"}, + {"id": uuid.uuid4(), "name": "Carry"}, + {"id": uuid.uuid4(), "name": "Medic"}, + {"id": uuid.uuid4(), "name": "Hygiene"}, ) - db.session.add( - PackageListItem( - id="4c08f0d5-583e-4882-8bea-2b2faab61fff", - name="Taschenmesser", - description="", - packagelist_id="ab2f16c2-d5f5-460b-b149-0fc9eec12887", + for category in categories: + db.session.add( + PackageListItemCategory( + id=str(category['id']), + name=category['name'], + description="", + ) ) - ) - db.session.add( - PackageListItem( - id="f7fe1c35-23c8-4e57-bec0-56212cff940a", - name="Geldbeutel", - description="", - packagelist_id="ab2f16c2-d5f5-460b-b149-0fc9eec12887", - ) - ) + with open("./items.csv") as csvfile: + reader = csv.reader(csvfile, delimiter=',') + for row in reader: + print(row) + (name, category, weight) = row + db.session.add( + PackageListItem( + id=str(uuid.uuid4()), + name=name, + description="", + weight=weight, + category_id=str([c['id'] for c in categories if c['name'] == category][0]) + ) + ) + + + print("db init done") db.session.commit() except sqlalchemy.exc.IntegrityError: pass diff --git a/python_flask/packager/components/CategoryList.py b/python_flask/packager/components/CategoryList.py new file mode 100644 index 0000000..230e2cd --- /dev/null +++ b/python_flask/packager/components/CategoryList.py @@ -0,0 +1,35 @@ +import dominate +import dominate.tags as t +from dominate.util import raw + +from ..helpers import * + +def CategoryList(categories): + with t.div(id="packagelist-table") as doc: + t.h1("Categories", _class=cls("text-2xl", "mb-5")) + with t.table( + id="packagelist-table", + _class=cls( + "table", + "table-auto", + "border-collapse", + "border-spacing-0", + "border", + "w-full", + ), + ): + with t.tbody() as b: + for category in categories: + with t.tr(_class=cls("h-10", "hover:bg-purple-200")) as doc: + with t.td(_class=cls("border", "p-0", "m-0")): + t.a( + category.name, + id="select-category", + # data_hx_post=f"/list/{pkglist.id}/edit", + href=f"/category/{category.id}", + # data_hx_target="closest tr", + # data_hx_swap="outerHTML", + _class=cls("block", "p-2", "m-2"), + ) + + return doc diff --git a/python_flask/packager/components/ItemList.py b/python_flask/packager/components/ItemList.py new file mode 100644 index 0000000..b90af4e --- /dev/null +++ b/python_flask/packager/components/ItemList.py @@ -0,0 +1,33 @@ +import dominate +import dominate.tags as t +from dominate.util import raw + +from ..helpers import * + +def ItemList(items): + with t.div(id="packagelist-table") as doc: + t.h1("Items", _class=cls("text-2xl", "mb-5")) + with t.table( + id="packagelist-table", + _class=cls( + "table", + "table-auto", + "border-collapse", + "border-spacing-0", + "border", + "w-full", + ), + ): + with t.thead(_class=cls("bg-gray-200")): + t.tr( + t.th("Name", _class=cls("border", "p-2")), + t.th("Weight", _class=cls("border", "p-2")), + _class="h-10", + ) + with t.tbody() as b: + for item in items: + with t.tr(_class=cls("h-10", "even:bg-gray-100", "hover:bg-purple-200")) as doc: + t.td(item.name, _class=cls("border", "px-2")), + t.td(str(item.weight), _class=cls("border", "px-2")), + + return doc diff --git a/python_flask/packager/components/PackageListItemManager.py b/python_flask/packager/components/PackageListItemManager.py new file mode 100644 index 0000000..85cd0b2 --- /dev/null +++ b/python_flask/packager/components/PackageListItemManager.py @@ -0,0 +1,21 @@ +import dominate +import dominate.tags as t +from dominate.util import raw + +from . import CategoryList, ItemList + +from ..helpers import * + + +class PackageListItemManager: + def __init__( + self, categories, items, name=None, description=None, error=False, errormsg=None + ): + assert not (error and not errormsg) + with t.div(id="pkglist-item-manager", _class=cls("p-8", "grid", "grid-cols-4", "gap-3")) as doc: + with t.div(_class=cls("col-span-1")): + CategoryList(categories), + with t.div(_class=cls("col-span-3")): + ItemList(items), + + self.doc = doc diff --git a/python_flask/packager/components/__init__.py b/python_flask/packager/components/__init__.py index 6780327..c993cd8 100644 --- a/python_flask/packager/components/__init__.py +++ b/python_flask/packager/components/__init__.py @@ -5,5 +5,9 @@ from .PackageListTable import ( PackageListTableRowNormal, PackageListTableRow, ) + +from .ItemList import ItemList +from .CategoryList import CategoryList from .PackageListManager import PackageListManager +from .PackageListItemManager import PackageListItemManager from .Home import Home diff --git a/python_flask/packager/models.py b/python_flask/packager/models.py index 94f82e1..80e2c7c 100644 --- a/python_flask/packager/models.py +++ b/python_flask/packager/models.py @@ -6,18 +6,31 @@ class PackageList(db.Model): id = db.Column(db.String(36), primary_key=True) name = db.Column(db.Text, unique=True, nullable=False) description = db.Column(db.Text) - items = db.relationship("PackageListItem", backref="packagelist", lazy=True) + # items = db.relationship("PackageListItem", backref="packagelist", lazy=True) edit = False error = False errormsg = None +class PackageListItemCategory(db.Model): + __tablename__ = "packagelistitemcategory" + id = db.Column(db.String(36), primary_key=True) + name = db.Column(db.Text, unique=True, nullable=False) + description = db.Column(db.Text) + items = db.relationship("PackageListItem", backref="category", lazy=True) + + class PackageListItem(db.Model): __tablename__ = "packagelistitem" id = db.Column(db.String(36), primary_key=True) name = db.Column(db.Text, unique=True, nullable=False) description = db.Column(db.Text) - packagelist_id = db.Column( - db.String(36), db.ForeignKey("packagelist.id"), nullable=False + weight = db.Column(db.Integer) + # packagelist_id = db.Column( + # db.String(36), db.ForeignKey("packagelist.id"), nullable=False + # ) + category_id = db.Column( + db.String(36), db.ForeignKey("packagelistitemcategory.id"), nullable=False ) + diff --git a/python_flask/packager/views.py b/python_flask/packager/views.py index d48203b..c77a885 100644 --- a/python_flask/packager/views.py +++ b/python_flask/packager/views.py @@ -12,6 +12,7 @@ from dominate.util import raw from .components import ( PackageListManager, + PackageListItemManager, NewPackageList, Home, PackageListTableRowEdit, @@ -26,6 +27,16 @@ def get_packagelists(): return PackageList.query.all() +def get_categories(): + return PackageListItemCategory.query.all() + + +def get_all_items(): + return PackageListItem.query.all() + +def get_items(category): + return PackageListItem.query.filter_by(category_id=str(category.id)) + def get_packagelist_by_id(id): return PackageList.query.filter_by(id=str(id)).first() @@ -52,7 +63,8 @@ def delete_packagelist(id): @app.route("/") def root(): - packagelists = get_packagelists() + categories = get_categories() + items = get_all_items() error = False if not is_htmx(): edit = request.args.get("edit") @@ -74,7 +86,40 @@ def root(): match[0].errormsg = f"Invalid name" return make_response( - Home(PackageListManager(packagelists), app.root_path).doc.render(), 200 + Home(PackageListItemManager(categories, items), app.root_path).doc.render(), 200 + ) + + +@app.route("/category/") +def category(id): + categories = get_categories() + print(id) + for c in categories: + print(f"{c.id} | {c.name}") + active_category = [c for c in categories if str(c.id) == str(id)][0] + items = get_items(active_category) + error = False + if not is_htmx(): + edit = request.args.get("edit") + if edit is not None: + match = [p for p in packagelists if p.id == edit] + if match: + match[0].edit = True + error = request.args.get("error") + if error and bool(int(error)): + match[0].error = True + errormsg = request.args.get("msg") + if errormsg: + match[0].errormsg = errormsg + else: + name = request.args.get("name") + if name: + match[0].errormsg = f"Invalid name: {name}" + else: + match[0].errormsg = f"Invalid name" + + return make_response( + Home(PackageListItemManager(categories, items), app.root_path).doc.render(), 200 )