Add database support for borrowing and borrow queues

This commit is contained in:
Oystein Kristoffer Tveit 2023-05-05 22:54:45 +02:00
parent 4e868c3324
commit c445cb2dbd
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
4 changed files with 81 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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