From f89cf8ecd43083b16e767ae0cc1b2c354455f5cc Mon Sep 17 00:00:00 2001 From: Donkie Date: Fri, 14 Jul 2023 14:19:57 +0200 Subject: [PATCH] Added archived field to spools Resolves #13 --- migrations/README.md | 3 ++- ...7-92186a5f7b0f_add_spool_archived_field.py | 24 +++++++++++++++++++ spoolman/api/v1/models.py | 2 ++ spoolman/api/v1/spool.py | 8 +++++++ spoolman/database/models.py | 1 + spoolman/database/spool.py | 5 ++++ 6 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 migrations/versions/2023_07_14_1217-92186a5f7b0f_add_spool_archived_field.py diff --git a/migrations/README.md b/migrations/README.md index c29cffe..0ae2b6c 100644 --- a/migrations/README.md +++ b/migrations/README.md @@ -2,5 +2,6 @@ Creating a new version: ```bash -alembic revision -m "some title" --autogenerate +python -m spoolman.main +pdm run alembic revision -m "some title" --autogenerate ``` diff --git a/migrations/versions/2023_07_14_1217-92186a5f7b0f_add_spool_archived_field.py b/migrations/versions/2023_07_14_1217-92186a5f7b0f_add_spool_archived_field.py new file mode 100644 index 0000000..ffc9daa --- /dev/null +++ b/migrations/versions/2023_07_14_1217-92186a5f7b0f_add_spool_archived_field.py @@ -0,0 +1,24 @@ +"""add spool archived field. + +Revision ID: 92186a5f7b0f +Revises: db385b808a20 +Create Date: 2023-07-14 12:17:13.162618 +""" +import sqlalchemy as sa +from alembic import op + +# revision identifiers, used by Alembic. +revision = "92186a5f7b0f" +down_revision = "db385b808a20" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + """Perform the upgrade.""" + op.add_column("spool", sa.Column("archived", sa.Boolean(), nullable=True)) + + +def downgrade() -> None: + """Perform the downgrade.""" + op.drop_column("spool", "archived") diff --git a/spoolman/api/v1/models.py b/spoolman/api/v1/models.py index 2ca9cc5..a27f8f0 100644 --- a/spoolman/api/v1/models.py +++ b/spoolman/api/v1/models.py @@ -150,6 +150,7 @@ class Spool(BaseModel): description="Free text comment about this specific spool.", example="", ) + archived: bool = Field(description="Whether this spool is archived and should not be used anymore.") @staticmethod def from_db(item: models.Spool) -> "Spool": @@ -185,6 +186,7 @@ class Spool(BaseModel): location=item.location, lot_nr=item.lot_nr, comment=item.comment, + archived=item.archived if item.archived is not None else False, ) diff --git a/spoolman/api/v1/spool.py b/spoolman/api/v1/spool.py index 4a3cabb..db7b91c 100644 --- a/spoolman/api/v1/spool.py +++ b/spoolman/api/v1/spool.py @@ -49,6 +49,7 @@ class SpoolParameters(BaseModel): description="Free text comment about this specific spool.", example="", ) + archived: bool = Field(default=False, description="Whether this spool is archived and should not be used anymore.") class SpoolUpdateParameters(SpoolParameters): @@ -104,6 +105,11 @@ async def find( title="Lot/Batch Number", description="Partial case-insensitive search term for the spool lot number.", ), + allow_archived: bool = Query( + default=False, + title="Allow Archived", + description="Whether to include archived spools in the search results.", + ), ) -> list[Spool]: db_items = await spool.find( db=db, @@ -114,6 +120,7 @@ async def find( vendor_id=vendor_id, location=location, lot_nr=lot_nr, + allow_archived=allow_archived, ) return [Spool.from_db(db_item) for db_item in db_items] @@ -168,6 +175,7 @@ async def create( # noqa: ANN201 location=body.location, lot_nr=body.lot_nr, comment=body.comment, + archived=body.archived, ) return Spool.from_db(db_item) except ItemCreateError: diff --git a/spoolman/database/models.py b/spoolman/database/models.py index cac8340..c6c5bdb 100644 --- a/spoolman/database/models.py +++ b/spoolman/database/models.py @@ -57,3 +57,4 @@ class Spool(Base): location: Mapped[Optional[str]] = mapped_column(String(64)) lot_nr: Mapped[Optional[str]] = mapped_column(String(64)) comment: Mapped[Optional[str]] = mapped_column(String(1024)) + archived: Mapped[Optional[bool]] = mapped_column() diff --git a/spoolman/database/spool.py b/spoolman/database/spool.py index e591bd6..17c5eb5 100644 --- a/spoolman/database/spool.py +++ b/spoolman/database/spool.py @@ -30,6 +30,7 @@ async def create( location: Optional[str] = None, lot_nr: Optional[str] = None, comment: Optional[str] = None, + archived: bool = False, ) -> models.Spool: """Add a new spool to the database. Leave weight empty to assume full spool.""" filament_item = await filament.get_by_id(db, filament_id) @@ -55,6 +56,7 @@ async def create( location=location, lot_nr=lot_nr, comment=comment, + archived=archived, ) db.add(db_item) await db.flush() @@ -83,6 +85,7 @@ async def find( vendor_id: Optional[int] = None, location: Optional[str] = None, lot_nr: Optional[str] = None, + allow_archived: bool = False, ) -> list[models.Spool]: """Find a list of spool objects by search criteria.""" stmt = ( @@ -105,6 +108,8 @@ async def find( stmt = stmt.where(models.Spool.location.ilike(f"%{location}%")) if lot_nr is not None: stmt = stmt.where(models.Spool.lot_nr.ilike(f"%{lot_nr}%")) + if not allow_archived: + stmt = stmt.where(models.Spool.archived != True) # noqa: E712 rows = await db.execute(stmt) return list(rows.scalars().all())