Skip to content

Commit

Permalink
Addresses PR feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbernstein committed Nov 5, 2024
1 parent 3d35d1a commit d7b132d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 43 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ module = [
"palace.manager.api.controller.loan",
"palace.manager.api.controller.marc",
"palace.manager.api.controller.odl_notification",
"palace.manager.api.controller.patron_activity_history",
"palace.manager.api.discovery.*",
"palace.manager.api.enki",
"palace.manager.api.lcp.hash",
Expand Down
15 changes: 7 additions & 8 deletions src/palace/manager/api/controller/patron_activity_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
import flask
from flask import Response

from palace.manager.api.controller.circulation_manager import (
CirculationManagerController,
)
from palace.manager.sqlalchemy.model.patron import Patron


class PatronActivityHistoryController(CirculationManagerController):
class PatronActivityHistoryController:

def erase(self):
"""Erases the patron's activity by resetting the UUID that links the patron to past activity"""
patron = flask.request.patron
def reset_statistics_uuid(self) -> Response:
"""Resets the patron's the statistics UUID that links the patron to past activity thus effectively erasing the
link between activity history and patron."""
patron: Patron = flask.request.patron # type: ignore [attr-defined]
patron.uuid = uuid.uuid4()
return Response("Erased", 200)
return Response("UUID reset", 200)
7 changes: 3 additions & 4 deletions src/palace/manager/api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,12 @@ def patron_profile():
return app.manager.profiles.protocol()


@library_dir_route("/patrons/me/erase_activity_history", methods=["PUT"])
@library_dir_route("/patrons/me/reset_statistics_uuid", methods=["PUT"])
@has_library
@allows_patron_web
@requires_auth
@returns_problem_detail
def erase_activity_history():
return app.manager.patron_activity_history.erase()
def reset_statistics_uuid():
return app.manager.patron_activity_history.reset_statistics_uuid()


@library_dir_route("/patrons/me/devices", methods=["GET"])
Expand Down
13 changes: 5 additions & 8 deletions src/palace/manager/api/s3_analytics_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import json
import random
import string
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING

from palace.manager.core.config import CannotLoadConfiguration
from palace.manager.core.local_analytics_provider import LocalAnalyticsProvider
Expand All @@ -29,8 +29,8 @@ def _create_event_object(
license_pool: LicensePool | None,
event_type: str,
time: datetime.datetime,
old_value: Any = None,
new_value: Any = None,
old_value: int | None = None,
new_value: int | None = None,
neighborhood: str | None = None,
user_agent: str | None = None,
patron: Patron | None = None,
Expand Down Expand Up @@ -145,8 +145,8 @@ def collect_event(
license_pool: LicensePool | None,
event_type: str,
time: datetime.datetime,
old_value: Any = None,
new_value: Any = None,
old_value: int | None = None,
new_value: int | None = None,
user_agent: str | None = None,
patron: Patron | None = None,
**kwargs,
Expand Down Expand Up @@ -178,9 +178,6 @@ def collect_event(
:type patron: Patron
"""

if not library:
raise ValueError("Either library or license_pool must be provided.")

event = self._create_event_object(
library,
license_pool,
Expand Down
49 changes: 26 additions & 23 deletions tests/manager/api/controller/test_patron_activity_history.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,48 @@
from __future__ import annotations

import flask
import pytest

from palace.manager.api.controller.patron_activity_history import (
PatronActivityHistoryController,
)
from palace.manager.sqlalchemy.model.patron import Patron
from tests.fixtures.api_controller import ControllerFixture
from tests.fixtures.database import DatabaseTransactionFixture
from tests.fixtures.services import ServicesFixture
from tests.fixtures.flask import FlaskAppFixture


class PatronActivityHistoryFixture(ControllerFixture):
class PatronActivityHistoryControllerFixture:
def __init__(
self, db: DatabaseTransactionFixture, services_fixture: ServicesFixture
self,
db: DatabaseTransactionFixture,
):
super().__init__(db, services_fixture)
self.default_patron = db.patron()
self.auth = dict(Authorization=self.valid_auth)
self.controller = PatronActivityHistoryController()
self.db = db


@pytest.fixture(scope="function")
def patron_activity_history_fixture(
db: DatabaseTransactionFixture, services_fixture: ServicesFixture
):
return PatronActivityHistoryFixture(db, services_fixture)
@pytest.fixture
def controller_fixture(
db: DatabaseTransactionFixture,
) -> PatronActivityHistoryControllerFixture:
return PatronActivityHistoryControllerFixture(db)


class TestPatronActivityHistoryController:
"""Test that a client can interact with the Patron Activity History."""

def test_erase(self, patron_activity_history_fixture: PatronActivityHistoryFixture):
with patron_activity_history_fixture.request_context_with_library(
"/", method="PUT", headers=patron_activity_history_fixture.auth
):
patron = (
patron_activity_history_fixture.controller.authenticated_patron_from_request()
)
def test_reset_statistics_uuid(
self,
controller_fixture: PatronActivityHistoryControllerFixture,
flask_app_fixture: FlaskAppFixture,
):

with flask_app_fixture.test_request_context("/", method="PUT"):
patron = controller_fixture.db.patron()
flask.request.patron = patron # type: ignore[attr-defined]
assert isinstance(patron, Patron)
uuid1 = patron.uuid
assert uuid1
response = (
patron_activity_history_fixture.manager.patron_activity_history.erase()
)
response = controller_fixture.controller.reset_statistics_uuid()
uuid2 = patron.uuid
assert uuid1 != uuid2
assert 200 == response.status_code
assert response.status_code == 200

0 comments on commit d7b132d

Please sign in to comment.