Skip to content

Commit

Permalink
Move custom lists pydantic model into models package (#2187)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathangreen authored Nov 22, 2024
1 parent 7b3a977 commit 8d10cab
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 65 deletions.
27 changes: 8 additions & 19 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,10 @@
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,
CustomListSharePostResponse,
)
from palace.manager.api.admin.problem_details import (
ADMIN_NOT_AUTHORIZED,
AUTO_UPDATE_CUSTOM_LIST_CANNOT_HAVE_ENTRIES,
Expand Down Expand Up @@ -37,28 +41,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 +87,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 +348,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 Expand Up @@ -456,7 +445,7 @@ def share_locally_POST(

self._db.commit()
self.log.info(f"Done sharing customlist {customlist.name}")
return self.CustomListSharePostResponse(
return CustomListSharePostResponse(
successes=len(successes), failures=len(failures)
).model_dump()

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 @@ -133,9 +133,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 @@ -163,9 +161,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 @@ -187,9 +183,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 @@ -219,9 +213,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 @@ -236,9 +228,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 @@ -258,9 +248,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 @@ -280,9 +268,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 @@ -308,9 +294,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 @@ -344,9 +328,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 @@ -379,9 +361,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 @@ -411,9 +391,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 @@ -646,9 +624,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 @@ -684,9 +660,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 @@ -710,9 +684,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 @@ -742,9 +714,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 8d10cab

Please sign in to comment.