Skip to content

Commit

Permalink
Move custom lists pydantic model into models package
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathangreen committed Nov 22, 2024
1 parent 1d1b4eb commit 99289a5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 64 deletions.
22 changes: 4 additions & 18 deletions src/palace/manager/api/admin/controller/custom_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
20 changes: 20 additions & 0 deletions src/palace/manager/api/admin/model/custom_lists.py
Original file line number Diff line number Diff line change
@@ -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
62 changes: 16 additions & 46 deletions tests/manager/api/admin/controller/test_custom_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
)
Expand Down Expand Up @@ -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()
)
Expand All @@ -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()
)
Expand Down Expand Up @@ -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()
)
Expand All @@ -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()
)
Expand All @@ -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,
Expand All @@ -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()
)
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 99289a5

Please sign in to comment.