Skip to content

Commit

Permalink
Change due_date and expiry_date fields in Hold/Checkout to Date
Browse files Browse the repository at this point in the history
Also:
- Run `ruff` linting and formatting across all files
- Add `pre-commit` and configure to run `ruff` before commit
  • Loading branch information
subhashb committed Jun 17, 2024
1 parent f3b7b1a commit c255912
Show file tree
Hide file tree
Showing 16 changed files with 213 additions and 73 deletions.
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.4.9
hooks:
# Run the linter.
- id: ruff
args: [ "check", "--select", "I", "--fix" ]
# Run the formatter.
- id: ruff-format
119 changes: 118 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pytest-cov = "^5.0.0"

[tool.poetry.group.dev.dependencies]
ruff = "^0.4.7"
pre-commit = "^3.7.1"

[build-system]
requires = ["poetry-core"]
Expand All @@ -41,3 +42,7 @@ addopts = [
"--cov-report=term-missing",
"--cov-branch"
]

[tool.ruff.lint.isort]
known-first-party = ["lending"]
known-third-party = ["protean"]
26 changes: 12 additions & 14 deletions src/lending/__init__.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
from lending.patron import (
Patron,
PatronType,
Hold,
HoldType,
HoldStatus,
Checkout,
CheckoutStatus,
HoldPlaced
)

from lending.book import (
Book,
BookStatus,
BookType,
)

from lending.holding_service import place_hold
from lending.daily_sheet_service import DailySheetService
from lending.checkout_service import checkout
from lending.daily_sheet_service import DailySheetService
from lending.holding_service import place_hold
from lending.patron import (
Checkout,
CheckoutStatus,
Hold,
HoldPlaced,
HoldStatus,
HoldType,
Patron,
PatronType,
)

__all__ = [
"Patron",
Expand Down
2 changes: 1 addition & 1 deletion src/lending/book.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from protean import handle
from protean.fields import String

from lending.domain import lending
from lending import HoldPlaced
from lending.domain import lending


class BookStatus(Enum):
Expand Down
6 changes: 1 addition & 5 deletions src/lending/checkout_service.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from datetime import datetime, timedelta

from protean import invariant
from protean.exceptions import ValidationError
from protean.fields import Identifier

from lending import Book, BookType, Checkout, HoldStatus, Patron, PatronType
from lending.domain import lending
from lending import Patron, Book, Checkout, HoldStatus, BookType, PatronType


@lending.domain_service(part_of=[Patron, Book])
Expand Down Expand Up @@ -34,8 +32,6 @@ def __call__(self):
Checkout(
book_id=self.book.id,
branch_id=self.branch_id,
checkout_date=datetime.now(),
due_date=datetime.now() + timedelta(days=60),
)
)

Expand Down
8 changes: 4 additions & 4 deletions src/lending/daily_sheet_service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime, timezone
from datetime import date

from lending import Patron, Book, HoldStatus
from lending import Book, HoldStatus, Patron
from lending.domain import lending


Expand All @@ -18,12 +18,12 @@ def _expire_holds(self):
for hold in patron.holds:
if (
hold.status == HoldStatus.ACTIVE.value
and hold.expiry_date < datetime.now()
and hold.expiry_date < date.today()
):
patron.expire_hold(hold.id)

def _overdue_checkouts(self):
for patron in self.patrons:
for checkout in patron.checkouts:
if checkout.due_date < datetime.now(timezone.utc):
if checkout.due_date < date.today():
checkout.overdue()
5 changes: 4 additions & 1 deletion src/lending/domain.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ provider = "memory"
provider = "inline"

[caches.default]
provider = "memory"
provider = "memory"

[custom]
CHECKOUT_PERIOD = 60 # Days
19 changes: 9 additions & 10 deletions src/lending/holding_service.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
from datetime import datetime, timedelta
from datetime import date, datetime, timedelta

from protean import invariant
from protean.fields import Identifier
from protean.exceptions import ValidationError
from protean.fields import Identifier

from lending import (
Book,
BookStatus,
BookType,
Hold,
HoldPlaced,
HoldStatus,
HoldType,
HoldPlaced,
Patron,
PatronType,
Book,
BookType,
BookStatus,
)
from lending.domain import lending

Expand Down Expand Up @@ -72,8 +72,7 @@ def patron_cannot_not_have_more_than_two_overdue_checkouts_in_branch(self):
overdue_checkouts_in_branch = [
checkout
for checkout in self.patron.checkouts
if checkout.due_date < datetime.now()
and checkout.branch_id == self.branch_id
if checkout.due_date < date.today() and checkout.branch_id == self.branch_id
]
if len(overdue_checkouts_in_branch) > 2:
raise ValidationError(
Expand All @@ -91,7 +90,7 @@ def __call__(self):
hold_type=self.hold_type.value,
status=HoldStatus.ACTIVE.value,
request_date=datetime.now(),
expiry_date=datetime.now() + timedelta(days=7),
expiry_date=date.today() + timedelta(days=7),
)
self.patron.add_holds(hold)

Expand All @@ -104,6 +103,6 @@ def __call__(self):
branch_id=hold.branch_id,
hold_type=hold.hold_type,
request_date=hold.request_date,
expiry_date=hold.expiry_date
expiry_date=hold.expiry_date,
)
)
Loading

0 comments on commit c255912

Please sign in to comment.