Restructure

This commit is contained in:
2022-09-16 08:19:41 +02:00
parent 8bb8fb2171
commit c46daee498
8 changed files with 195 additions and 34 deletions

View File

View File

@@ -1,5 +1,6 @@
import uuid import uuid
import sqlalchemy import sqlalchemy
import csv
from flask import Flask from flask import Flask
from .helpers import * from .helpers import *
@@ -14,42 +15,51 @@ db = SQLAlchemy(app)
from packager.models import * from packager.models import *
import packager.views import packager.views
db.create_all() db.create_all()
try: try:
db.session.add( categories = (
PackageList( {"id": uuid.uuid4(), "name": "Sleeping"},
id="ab2f16c2-d5f5-460b-b149-0fc9eec12887", {"id": uuid.uuid4(), "name": "Shelter"},
name="EDC", {"id": uuid.uuid4(), "name": "Fire"},
description="What you always carry", {"id": uuid.uuid4(), "name": "Cooking"},
) {"id": uuid.uuid4(), "name": "Water"},
) {"id": uuid.uuid4(), "name": "Protection"},
db.session.add( {"id": uuid.uuid4(), "name": "Tools"},
PackageList( {"id": uuid.uuid4(), "name": "Insulation"},
id="9f3a72cd-7e30-4263-bd52-92fb7bed1242", {"id": uuid.uuid4(), "name": "Electronics"},
name="Camping", {"id": uuid.uuid4(), "name": "Carry"},
description="For outdoors", {"id": uuid.uuid4(), "name": "Medic"},
) {"id": uuid.uuid4(), "name": "Hygiene"},
) )
db.session.add( for category in categories:
PackageListItem( db.session.add(
id="4c08f0d5-583e-4882-8bea-2b2faab61fff", PackageListItemCategory(
name="Taschenmesser", id=str(category['id']),
description="", name=category['name'],
packagelist_id="ab2f16c2-d5f5-460b-b149-0fc9eec12887", description="",
)
) )
)
db.session.add( with open("./items.csv") as csvfile:
PackageListItem( reader = csv.reader(csvfile, delimiter=',')
id="f7fe1c35-23c8-4e57-bec0-56212cff940a", for row in reader:
name="Geldbeutel", print(row)
description="", (name, category, weight) = row
packagelist_id="ab2f16c2-d5f5-460b-b149-0fc9eec12887",
)
)
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() db.session.commit()
except sqlalchemy.exc.IntegrityError: except sqlalchemy.exc.IntegrityError:
pass pass

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -5,5 +5,9 @@ from .PackageListTable import (
PackageListTableRowNormal, PackageListTableRowNormal,
PackageListTableRow, PackageListTableRow,
) )
from .ItemList import ItemList
from .CategoryList import CategoryList
from .PackageListManager import PackageListManager from .PackageListManager import PackageListManager
from .PackageListItemManager import PackageListItemManager
from .Home import Home from .Home import Home

View File

@@ -6,18 +6,31 @@ class PackageList(db.Model):
id = db.Column(db.String(36), primary_key=True) id = db.Column(db.String(36), primary_key=True)
name = db.Column(db.Text, unique=True, nullable=False) name = db.Column(db.Text, unique=True, nullable=False)
description = db.Column(db.Text) description = db.Column(db.Text)
items = db.relationship("PackageListItem", backref="packagelist", lazy=True) # items = db.relationship("PackageListItem", backref="packagelist", lazy=True)
edit = False edit = False
error = False error = False
errormsg = None 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): class PackageListItem(db.Model):
__tablename__ = "packagelistitem" __tablename__ = "packagelistitem"
id = db.Column(db.String(36), primary_key=True) id = db.Column(db.String(36), primary_key=True)
name = db.Column(db.Text, unique=True, nullable=False) name = db.Column(db.Text, unique=True, nullable=False)
description = db.Column(db.Text) description = db.Column(db.Text)
packagelist_id = db.Column( weight = db.Column(db.Integer)
db.String(36), db.ForeignKey("packagelist.id"), nullable=False # 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
) )

View File

@@ -12,6 +12,7 @@ from dominate.util import raw
from .components import ( from .components import (
PackageListManager, PackageListManager,
PackageListItemManager,
NewPackageList, NewPackageList,
Home, Home,
PackageListTableRowEdit, PackageListTableRowEdit,
@@ -26,6 +27,16 @@ def get_packagelists():
return PackageList.query.all() 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): def get_packagelist_by_id(id):
return PackageList.query.filter_by(id=str(id)).first() return PackageList.query.filter_by(id=str(id)).first()
@@ -52,7 +63,8 @@ def delete_packagelist(id):
@app.route("/") @app.route("/")
def root(): def root():
packagelists = get_packagelists() categories = get_categories()
items = get_all_items()
error = False error = False
if not is_htmx(): if not is_htmx():
edit = request.args.get("edit") edit = request.args.get("edit")
@@ -74,7 +86,40 @@ def root():
match[0].errormsg = f"Invalid name" match[0].errormsg = f"Invalid name"
return make_response( 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/<uuid:id>")
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
) )