Skip to content

Commit

Permalink
Per-library work suppression is idempotent. (PP-1378) (#1905)
Browse files Browse the repository at this point in the history
* Failing test for this issue.

* Un/suppression is idempotent.
  • Loading branch information
tdilauro authored Jun 14, 2024
1 parent 1b2ce5f commit 3682ee1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/palace/manager/api/admin/controller/work_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,18 @@ def suppress(
# Something went wrong.
return work

work.suppressed_for.append(library)
self.log.info(
f"Suppressed {identifier_type}/{identifier} (work id: {work.id}) for library {library.short_name}."
)
if library in work.suppressed_for:
# If the library is already suppressed, we don't need to do anything.
message = f"Already suppressed {identifier_type}/{identifier} (work id: {work.id}) for library {library.short_name}."
else:
# Otherwise, add the library to the suppressed list.
work.suppressed_for.append(library)
message = f"Suppressed {identifier_type}/{identifier} (work id: {work.id}) for library {library.short_name}."

return Response("", 200)
self.log.info(message)
return Response(
json.dumps({"message": message}), 200, mimetype="application/json"
)

def unsuppress(
self, identifier_type: str, identifier: str
Expand All @@ -416,12 +422,18 @@ def unsuppress(
# Something went wrong.
return work

work.suppressed_for.remove(library)
self.log.info(
f"Unsuppressed {identifier_type}/{identifier} (work id: {work.id}) for library {library.short_name}."
)
if library not in work.suppressed_for:
# If the library is not suppressed, we don't need to do anything.
message = f"Already unsuppressed {identifier_type}/{identifier} (work id: {work.id}) for library {library.short_name}."
else:
# Otherwise, remove the library from the suppressed list.
work.suppressed_for.remove(library)
message = f"Unsuppressed {identifier_type}/{identifier} (work id: {work.id}) for library {library.short_name}."

return Response("", 200)
self.log.info(message)
return Response(
json.dumps({"message": message}), 200, mimetype="application/json"
)

def refresh_metadata(self, identifier_type, identifier, provider=None):
"""Refresh the metadata for a book from the content server"""
Expand Down
15 changes: 15 additions & 0 deletions tests/manager/api/admin/controller/test_work_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,14 @@ def test_suppress(self, work_fixture: WorkFixture):
assert 200 == response.status_code
assert library in work.suppressed_for

# We should not fail if we suppress the already-suppressed work again.
with work_fixture.request_context_with_library_and_admin("/"):
response = work_fixture.manager.admin_work_controller.suppress(
lp.identifier.type, lp.identifier.identifier
)
assert 200 == response.status_code
assert library in work.suppressed_for

# test non-existent id
with work_fixture.request_context_with_library_and_admin("/"):
response = work_fixture.manager.admin_work_controller.suppress(
Expand Down Expand Up @@ -781,7 +789,14 @@ def test_unsuppress(self, work_fixture: WorkFixture):
response = work_fixture.manager.admin_work_controller.unsuppress(
lp.identifier.type, lp.identifier.identifier
)
assert 200 == response.status_code
assert library not in work.suppressed_for

# We should not fail if we unsuppress the already-unsuppressed work again.
with work_fixture.request_context_with_library_and_admin("/"):
response = work_fixture.manager.admin_work_controller.unsuppress(
lp.identifier.type, lp.identifier.identifier
)
assert 200 == response.status_code
assert library not in work.suppressed_for

Expand Down

0 comments on commit 3682ee1

Please sign in to comment.