From 99289a5da9df84088ecf852e3b4ff0e314c0169e Mon Sep 17 00:00:00 2001 From: Jonathan Green Date: Fri, 22 Nov 2024 13:21:03 -0400 Subject: [PATCH] Move custom lists pydantic model into models package --- .../api/admin/controller/custom_lists.py | 22 ++----- .../manager/api/admin/model/custom_lists.py | 20 ++++++ .../api/admin/controller/test_custom_lists.py | 62 +++++-------------- 3 files changed, 40 insertions(+), 64 deletions(-) create mode 100644 src/palace/manager/api/admin/model/custom_lists.py diff --git a/src/palace/manager/api/admin/controller/custom_lists.py b/src/palace/manager/api/admin/controller/custom_lists.py index 4237432a9..fa66ba654 100644 --- a/src/palace/manager/api/admin/controller/custom_lists.py +++ b/src/palace/manager/api/admin/controller/custom_lists.py @@ -9,6 +9,7 @@ from flask_babel import lazy_gettext as _ from palace.manager.api.admin.controller.base import AdminPermissionsControllerMixin +from palace.manager.api.admin.model.custom_lists import CustomListPostRequest from palace.manager.api.admin.problem_details import ( ADMIN_NOT_AUTHORIZED, AUTO_UPDATE_CUSTOM_LIST_CANNOT_HAVE_ENTRIES, @@ -37,28 +38,13 @@ from palace.manager.sqlalchemy.model.licensing import LicensePool from palace.manager.sqlalchemy.model.work import Work from palace.manager.sqlalchemy.util import create, get_one -from palace.manager.util.flask_util import CustomBaseModel, parse_multi_dict +from palace.manager.util.flask_util import parse_multi_dict from palace.manager.util.problem_detail import ProblemDetail, ProblemDetailException class CustomListsController( CirculationManagerController, AdminPermissionsControllerMixin ): - class CustomListSharePostResponse(CustomBaseModel): - successes: int = 0 - failures: int = 0 - - class CustomListPostRequest(CustomBaseModel): - name: str - id: int | None = None - entries: list[dict] = [] - collections: list[int] = [] - deletedEntries: list[dict] = [] - # For auto updating lists - auto_update: bool = False - auto_update_query: dict | None = None - auto_update_facets: dict | None = None - def _list_as_json(self, list: CustomList, is_owner=True) -> dict: """Transform a CustomList object into a response ready dict""" collections = [] @@ -98,7 +84,7 @@ def custom_lists(self) -> dict | ProblemDetail | Response | None: return dict(custom_lists=custom_lists) if flask.request.method == "POST": - list_ = self.CustomListPostRequest.model_validate( + list_ = CustomListPostRequest.model_validate( parse_multi_dict(flask.request.form) ) return self._create_or_update_list( @@ -359,7 +345,7 @@ def custom_list(self, list_id: int) -> Response | dict | ProblemDetail | None: ) elif flask.request.method == "POST": - list_ = self.CustomListPostRequest.model_validate( + list_ = CustomListPostRequest.model_validate( parse_multi_dict(flask.request.form) ) return self._create_or_update_list( diff --git a/src/palace/manager/api/admin/model/custom_lists.py b/src/palace/manager/api/admin/model/custom_lists.py new file mode 100644 index 000000000..a96230e2a --- /dev/null +++ b/src/palace/manager/api/admin/model/custom_lists.py @@ -0,0 +1,20 @@ +from pydantic import NonNegativeInt + +from palace.manager.util.flask_util import CustomBaseModel + + +class CustomListSharePostResponse(CustomBaseModel): + successes: int = 0 + failures: int = 0 + + +class CustomListPostRequest(CustomBaseModel): + name: str + id: NonNegativeInt | None = None + entries: list[dict] = [] + collections: list[int] = [] + deletedEntries: list[dict] = [] + # For auto updating lists + auto_update: bool = False + auto_update_query: dict | None = None + auto_update_facets: dict | None = None diff --git a/tests/manager/api/admin/controller/test_custom_lists.py b/tests/manager/api/admin/controller/test_custom_lists.py index e446b34ca..2888da6ad 100644 --- a/tests/manager/api/admin/controller/test_custom_lists.py +++ b/tests/manager/api/admin/controller/test_custom_lists.py @@ -8,8 +8,8 @@ from attr import define from werkzeug.datastructures import ImmutableMultiDict -from palace.manager.api.admin.controller.custom_lists import CustomListsController from palace.manager.api.admin.exceptions import AdminNotAuthorized +from palace.manager.api.admin.model.custom_lists import CustomListPostRequest from palace.manager.api.admin.problem_details import ( AUTO_UPDATE_CUSTOM_LIST_CANNOT_HAVE_ENTRIES, CANNOT_CHANGE_LIBRARY_FOR_CUSTOM_LIST, @@ -132,9 +132,7 @@ def test_custom_lists_post_errors( ("name", "name"), ] ) - add_request_context( - flask.request, CustomListsController.CustomListPostRequest, form=form - ) + add_request_context(flask.request, CustomListPostRequest, form=form) response = ( admin_librarian_fixture.manager.admin_custom_lists_controller.custom_lists() ) @@ -162,9 +160,7 @@ def test_custom_lists_post_errors( ("name", list.name), ] ) - add_request_context( - flask.request, CustomListsController.CustomListPostRequest, form=form - ) + add_request_context(flask.request, CustomListPostRequest, form=form) response = ( admin_librarian_fixture.manager.admin_custom_lists_controller.custom_lists() ) @@ -186,9 +182,7 @@ def test_custom_lists_post_errors( ("name", list.name), ] ) - add_request_context( - flask.request, CustomListsController.CustomListPostRequest, form=form - ) + add_request_context(flask.request, CustomListPostRequest, form=form) response = ( admin_librarian_fixture.manager.admin_custom_lists_controller.custom_lists() ) @@ -218,9 +212,7 @@ def test_custom_lists_post_errors( ("name", l1.name), ] ) - add_request_context( - flask.request, CustomListsController.CustomListPostRequest, form=form - ) + add_request_context(flask.request, CustomListPostRequest, form=form) response = ( admin_librarian_fixture.manager.admin_custom_lists_controller.custom_lists() ) @@ -235,9 +227,7 @@ def test_custom_lists_post_errors( ("collections", json.dumps([12345])), ] ) - add_request_context( - flask.request, CustomListsController.CustomListPostRequest, form=form - ) + add_request_context(flask.request, CustomListPostRequest, form=form) response = ( admin_librarian_fixture.manager.admin_custom_lists_controller.custom_lists() ) @@ -257,9 +247,7 @@ def test_custom_lists_post_errors( ("collections", json.dumps([])), ] ) - add_request_context( - flask.request, CustomListsController.CustomListPostRequest, form=form - ) + add_request_context(flask.request, CustomListPostRequest, form=form) pytest.raises( AdminNotAuthorized, admin_librarian_fixture.manager.admin_custom_lists_controller.custom_lists, @@ -279,9 +267,7 @@ def test_custom_lists_post_collection_with_wrong_library( ("collections", json.dumps([collection.id])), ] ) - add_request_context( - flask.request, CustomListsController.CustomListPostRequest, form=form - ) + add_request_context(flask.request, CustomListPostRequest, form=form) response = ( admin_librarian_fixture.manager.admin_custom_lists_controller.custom_lists() ) @@ -307,9 +293,7 @@ def test_custom_lists_create(self, admin_librarian_fixture: AdminLibrarianFixtur ("collections", json.dumps([collection.id])), ] ) - add_request_context( - flask.request, CustomListsController.CustomListPostRequest, form=form - ) + add_request_context(flask.request, CustomListPostRequest, form=form) response = ( admin_librarian_fixture.manager.admin_custom_lists_controller.custom_lists() @@ -343,9 +327,7 @@ def test_custom_lists_create(self, admin_librarian_fixture: AdminLibrarianFixtur ("auto_update", "True"), ] ) - add_request_context( - flask.request, CustomListsController.CustomListPostRequest, form=form - ) + add_request_context(flask.request, CustomListPostRequest, form=form) response = ( admin_librarian_fixture.manager.admin_custom_lists_controller.custom_lists() @@ -378,9 +360,7 @@ def test_custom_lists_create(self, admin_librarian_fixture: AdminLibrarianFixtur ("auto_update_facets", json.dumps({})), ] ) - add_request_context( - flask.request, CustomListsController.CustomListPostRequest, form=form - ) + add_request_context(flask.request, CustomListPostRequest, form=form) response = ( admin_librarian_fixture.manager.admin_custom_lists_controller.custom_lists() @@ -410,9 +390,7 @@ def test_custom_lists_create(self, admin_librarian_fixture: AdminLibrarianFixtur ("auto_update_facets", json.dumps({})), ] ) - add_request_context( - flask.request, CustomListsController.CustomListPostRequest, form=form - ) + add_request_context(flask.request, CustomListPostRequest, form=form) response = ( admin_librarian_fixture.manager.admin_custom_lists_controller.custom_lists() @@ -640,9 +618,7 @@ def test_custom_list_edit(self, admin_librarian_fixture: AdminLibrarianFixture): ("collections", json.dumps([c.id for c in new_collections])), ] ) - add_request_context( - flask.request, CustomListsController.CustomListPostRequest, form=form - ) + add_request_context(flask.request, CustomListPostRequest, form=form) assert isinstance(list.id, int) response = admin_librarian_fixture.manager.admin_custom_lists_controller.custom_list( @@ -678,9 +654,7 @@ def test_custom_list_edit(self, admin_librarian_fixture: AdminLibrarianFixture): ("auto_update_facets", json.dumps(update_facets)), ] ) - add_request_context( - flask.request, CustomListsController.CustomListPostRequest, form=form - ) + add_request_context(flask.request, CustomListPostRequest, form=form) response = admin_librarian_fixture.manager.admin_custom_lists_controller.custom_list( list.id @@ -704,9 +678,7 @@ def test_custom_list_edit(self, admin_librarian_fixture: AdminLibrarianFixture): ("collections", json.dumps([c.id for c in new_collections])), ] ) - add_request_context( - flask.request, CustomListsController.CustomListPostRequest, form=form - ) + add_request_context(flask.request, CustomListPostRequest, form=form) pytest.raises( AdminNotAuthorized, @@ -736,9 +708,7 @@ def test_custom_list_auto_update_cases( ("auto_update_query", None), ] ) - add_request_context( - flask.request, CustomListsController.CustomListPostRequest, form=form - ) + add_request_context(flask.request, CustomListPostRequest, form=form) response = admin_librarian_fixture.manager.admin_custom_lists_controller.custom_list( list.id