Skip to content

Commit

Permalink
Add type hints to sqlalchemy Patron model (PP-1728) (#2086)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathangreen authored Sep 26, 2024
1 parent 70a8026 commit 344383b
Show file tree
Hide file tree
Showing 18 changed files with 205 additions and 139 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ module = [
"palace.manager.sqlalchemy.model.collection",
"palace.manager.sqlalchemy.model.integration",
"palace.manager.sqlalchemy.model.library",
"palace.manager.sqlalchemy.model.patron",
"palace.manager.util.authentication_for_opds",
"palace.manager.util.base64",
"palace.manager.util.cache",
Expand Down
4 changes: 3 additions & 1 deletion src/palace/manager/api/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
)
from palace.manager.sqlalchemy.model.identifier import Identifier
from palace.manager.sqlalchemy.model.patron import Annotation
from palace.manager.sqlalchemy.util import get_one_or_create
from palace.manager.util.datetime_helpers import utc_now


Expand Down Expand Up @@ -210,8 +211,9 @@ def parse(cls, _db, data, patron):
# per target.
extra_kwargs["target"] = target

annotation, ignore = Annotation.get_one_or_create(
annotation, ignore = get_one_or_create(
_db,
Annotation,
patron=patron,
identifier=identifier,
motivation=motivation,
Expand Down
4 changes: 2 additions & 2 deletions src/palace/manager/feed/acquisition.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from __future__ import annotations

import logging
from collections.abc import Callable, Generator
from collections.abc import Callable, Generator, Iterable
from typing import TYPE_CHECKING, Any

from dependency_injector.wiring import Provide, inject
Expand Down Expand Up @@ -56,7 +56,7 @@ def __init__(
self,
title: str,
url: str,
works: list[Work],
works: Iterable[Work],
annotator: CirculationManagerAnnotator,
facets: FacetsWithEntryPoint | None = None,
pagination: Pagination | None = None,
Expand Down
4 changes: 2 additions & 2 deletions src/palace/manager/sqlalchemy/model/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def protocol(self, new_protocol: str) -> None:
STANDARD_DEFAULT_LOAN_PERIOD = 21

def default_loan_period(
self, library: Library, medium: str = EditionConstants.BOOK_MEDIUM
self, library: Library | None, medium: str = EditionConstants.BOOK_MEDIUM
) -> int:
"""Until we hear otherwise from the license provider, we assume
that someone who borrows a non-open-access item from this
Expand All @@ -336,7 +336,7 @@ def loan_period_key(cls, medium: str = EditionConstants.BOOK_MEDIUM) -> str:

def default_loan_period_setting(
self,
library: Library,
library: Library | None,
medium: str = EditionConstants.BOOK_MEDIUM,
) -> int | None:
"""Until we hear otherwise from the license provider, we assume
Expand Down
2 changes: 1 addition & 1 deletion src/palace/manager/sqlalchemy/model/edition.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class Edition(Base, EditionConstants):

# An Edition may be the presentation edition for many LicensePools.
is_presentation_for: Mapped[list[LicensePool]] = relationship(
"LicensePool", backref="presentation_edition"
"LicensePool", back_populates="presentation_edition"
)

title = Column(Unicode, index=True)
Expand Down
6 changes: 6 additions & 0 deletions src/palace/manager/sqlalchemy/model/licensing.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
if TYPE_CHECKING:
from palace.manager.sqlalchemy.model.collection import Collection
from palace.manager.sqlalchemy.model.datasource import DataSource
from palace.manager.sqlalchemy.model.edition import Edition
from palace.manager.sqlalchemy.model.identifier import Identifier
from palace.manager.sqlalchemy.model.resource import Resource
from palace.manager.sqlalchemy.model.work import Work


class PolicyException(BasePalaceException):
Expand Down Expand Up @@ -199,6 +201,7 @@ class LicensePool(Base):
# A LicensePool may be associated with a Work. (If it's not, no one
# can check it out.)
work_id = Column(Integer, ForeignKey("works.id"), index=True)
work: Mapped[Work] = relationship("Work", back_populates="license_pools")

# Each LicensePool is associated with one DataSource and one
# Identifier.
Expand All @@ -224,6 +227,9 @@ class LicensePool(Base):
# Each LicensePool has an Edition which contains the metadata used
# to describe this book.
presentation_edition_id = Column(Integer, ForeignKey("editions.id"), index=True)
presentation_edition: Mapped[Edition] = relationship(
"Edition", back_populates="is_presentation_for"
)

# If the source provides information about individual licenses, the
# LicensePool may have many Licenses.
Expand Down
Loading

0 comments on commit 344383b

Please sign in to comment.