-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
794fe8b
commit 36462fe
Showing
5 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import logging | ||
from http import HTTPStatus | ||
|
||
import flask | ||
from flask import Response | ||
|
||
from api.controller.circulation_manager import CirculationManagerController | ||
from core.model.admin import Admin | ||
from core.model.asynctask import AsyncTaskType, queue_task | ||
from core.util.problem_detail import ProblemDetail, ProblemDetailException | ||
|
||
|
||
class ReportController(CirculationManagerController): | ||
def generate_inventory_report(self) -> Response | ProblemDetail: | ||
log = logging.getLogger(self.__class__.__name__) | ||
admin: Admin = getattr(flask.request, "admin") | ||
try: | ||
email = admin.email | ||
data = dict(admin_email=email, admin_id=admin.id) | ||
task, is_new = queue_task( | ||
self._db, task_type=AsyncTaskType.INVENTORY_REPORT, data=data | ||
) | ||
self._db.commit() | ||
|
||
msg = ( | ||
f"An inventory report request was {'already' if not is_new else ''} received at {task.created}. " | ||
f"When processing is complete, the report will be sent to {email}." | ||
) | ||
http_status = HTTPStatus.ACCEPTED if is_new else HTTPStatus.CONFLICT | ||
|
||
return Response(dict(message=msg), http_status) | ||
except ProblemDetailException as e: | ||
self._db.rollback() | ||
return e.problem_detail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from http import HTTPStatus | ||
|
||
import pytest | ||
|
||
from core.model import create | ||
from core.model.admin import Admin, AdminRole | ||
from tests.fixtures.api_admin import AdminControllerFixture | ||
from tests.fixtures.api_controller import ControllerFixture | ||
|
||
|
||
class ReportControllerFixture(AdminControllerFixture): | ||
def __init__(self, controller_fixture: ControllerFixture): | ||
super().__init__(controller_fixture) | ||
|
||
|
||
@pytest.fixture | ||
def report_fixture( | ||
controller_fixture: ControllerFixture, | ||
) -> ReportControllerFixture: | ||
return ReportControllerFixture(controller_fixture) | ||
|
||
|
||
class TestReportController: | ||
def test_generate_inventory_report(self, report_fixture: ReportControllerFixture): | ||
ctrl = report_fixture.manager.admin_report_controller | ||
db = report_fixture.ctrl.db | ||
|
||
system_admin, _ = create(db.session, Admin, email="[email protected]") | ||
system_admin.add_role(AdminRole.SYSTEM_ADMIN) | ||
default = db.default_library() | ||
library1 = db.library() | ||
with report_fixture.request_context_with_admin( | ||
f"/", | ||
admin=system_admin, | ||
) as ctx: | ||
response = ctrl.generate_inventory_report() | ||
assert response.status_code == HTTPStatus.ACCEPTED | ||
assert response.response["message"].__contains__("[email protected]") | ||
assert not response.response["message"].__contains__("already") | ||
|
||
# check that when generating a duplicate request a 409 is returned. | ||
with report_fixture.request_context_with_admin( | ||
f"/", | ||
admin=system_admin, | ||
) as ctx: | ||
response = ctrl.generate_inventory_report() | ||
assert response.status_code == HTTPStatus.CONFLICT | ||
assert response.response["message"].__contains__("[email protected]") | ||
assert response.response["message"].__contains__("already") |