From c445cb2dbd5aa7654ab23058881dac344eee89fa Mon Sep 17 00:00:00 2001 From: h7x4 Date: Fri, 5 May 2023 22:54:45 +0200 Subject: [PATCH] Add database support for borrowing and borrow queues --- worblehat/models/BookcaseItem.py | 4 ++ worblehat/models/BookcaseItemBorrowing.py | 38 +++++++++++++++++++ .../models/BookcaseItemBorrowingQueue.py | 37 ++++++++++++++++++ worblehat/models/__init__.py | 2 + 4 files changed, 81 insertions(+) create mode 100644 worblehat/models/BookcaseItemBorrowing.py create mode 100644 worblehat/models/BookcaseItemBorrowingQueue.py diff --git a/worblehat/models/BookcaseItem.py b/worblehat/models/BookcaseItem.py index 0d36e5a..2ed1a3b 100644 --- a/worblehat/models/BookcaseItem.py +++ b/worblehat/models/BookcaseItem.py @@ -24,6 +24,8 @@ from .xref_tables import ( ) if TYPE_CHECKING: from .Author import Author + from .BookcaseItemBorrowing import BookcaseItemBorrowing + from .BookcaseItemBorrowingQueue import BookcaseItemBorrowingQueue from .BookcaseShelf import BookcaseShelf from .Category import Category from .Language import Language @@ -41,6 +43,8 @@ class BookcaseItem(Base, UidMixin, UniqueNameMixin): media_type: Mapped[MediaType] = relationship(back_populates='items') shelf: Mapped[BookcaseShelf] = relationship(back_populates='items') language: Mapped[Language] = relationship() + borrowings: Mapped[BookcaseItemBorrowing] = relationship(back_populates='item') + borrowing_queue: Mapped[BookcaseItemBorrowingQueue] = relationship(back_populates='item') categories: Mapped[set[Category]] = relationship( secondary = Item_Category.__table__, diff --git a/worblehat/models/BookcaseItemBorrowing.py b/worblehat/models/BookcaseItemBorrowing.py new file mode 100644 index 0000000..487a896 --- /dev/null +++ b/worblehat/models/BookcaseItemBorrowing.py @@ -0,0 +1,38 @@ +from __future__ import annotations +from typing import TYPE_CHECKING +from datetime import datetime, timedelta + +from sqlalchemy import ( + Boolean, + ForeignKey, + String, + DateTime, +) +from sqlalchemy.orm import ( + Mapped, + mapped_column, + relationship, +) + +from .Base import Base +from .mixins import UidMixin +if TYPE_CHECKING: + from .BookcaseItem import BookcaseItem + +class BookcaseItemBorrowing(Base, UidMixin): + username: Mapped[str] = mapped_column(String) + start_time: Mapped[datetime] = mapped_column(DateTime, default=datetime.now()) + end_time: Mapped[datetime] = mapped_column(DateTime, default=datetime.now() + timedelta(days=30)) + delivered: Mapped[bool] = mapped_column(Boolean, default=False) + + fk_bookcase_item_uid: Mapped[int] = mapped_column(ForeignKey('BookcaseItem.uid'), index=True) + + item: Mapped[BookcaseItem] = relationship(back_populates='borrowings') + + def __init__( + self, + username: str, + item: BookcaseItem, + ): + self.username = username + self.item = item \ No newline at end of file diff --git a/worblehat/models/BookcaseItemBorrowingQueue.py b/worblehat/models/BookcaseItemBorrowingQueue.py new file mode 100644 index 0000000..9b6234d --- /dev/null +++ b/worblehat/models/BookcaseItemBorrowingQueue.py @@ -0,0 +1,37 @@ +from __future__ import annotations +from typing import TYPE_CHECKING +from datetime import datetime + +from sqlalchemy import ( + ForeignKey, + String, + DateTime, + Boolean, +) +from sqlalchemy.orm import ( + Mapped, + mapped_column, + relationship, +) + +from .Base import Base +from .mixins import UidMixin +if TYPE_CHECKING: + from .BookcaseItem import BookcaseItem + +class BookcaseItemBorrowingQueue(Base, UidMixin): + username: Mapped[str] = mapped_column(String) + entered_queue_time = mapped_column(DateTime, default=datetime.now()) + should_notify_user = mapped_column(Boolean, default=False) + + fk_bookcase_item_uid: Mapped[int] = mapped_column(ForeignKey('BookcaseItem.uid'), index=True) + + item: Mapped[BookcaseItem] = relationship(back_populates='borrowing_queue') + + def __init__( + self, + username: str, + item: BookcaseItem, + ): + self.username = username + self.item = item \ No newline at end of file diff --git a/worblehat/models/__init__.py b/worblehat/models/__init__.py index f3f74f8..d417c08 100644 --- a/worblehat/models/__init__.py +++ b/worblehat/models/__init__.py @@ -2,6 +2,8 @@ from .Author import Author from .Base import Base from .Bookcase import Bookcase from .BookcaseItem import BookcaseItem +from .BookcaseItemBorrowing import BookcaseItemBorrowing +from .BookcaseItemBorrowingQueue import BookcaseItemBorrowingQueue from .BookcaseShelf import BookcaseShelf from .Category import Category from .Language import Language