From 73f0bddf0b52bc79ef29b5eaf20f2af8d305528b Mon Sep 17 00:00:00 2001 From: Leonidas Spyropoulos Date: Tue, 8 Nov 2022 13:14:42 +0000 Subject: [PATCH] fix: handle default requests when using pages The default page shows the pending requests which were working OK if one used the Filters button. This fixes the case when someone submits by using the pager (Next, Last etc). Closes: #405 Signed-off-by: Leonidas Spyropoulos --- aurweb/routers/requests.py | 10 +++++++-- test/test_requests.py | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/aurweb/routers/requests.py b/aurweb/routers/requests.py index ca5fae73..d1f1b830 100644 --- a/aurweb/routers/requests.py +++ b/aurweb/routers/requests.py @@ -18,6 +18,13 @@ from aurweb.requests.util import get_pkgreq_by_id from aurweb.scripts import notify from aurweb.templates import make_context, render_template +FILTER_PARAMS = { + "filter_pending", + "filter_closed", + "filter_accepted", + "filter_rejected", +} + router = APIRouter() @@ -36,7 +43,7 @@ async def requests( context["q"] = dict(request.query_params) - if len(dict(request.query_params)) == 0: + if not dict(request.query_params).keys() & FILTER_PARAMS: filter_pending = True O, PP = util.sanitize_params(O, PP) @@ -89,7 +96,6 @@ async def requests( .offset(O) .all() ) - return render_template(request, "requests.html", context) diff --git a/test/test_requests.py b/test/test_requests.py index 344b9edc..7dfcf5e5 100644 --- a/test/test_requests.py +++ b/test/test_requests.py @@ -734,6 +734,52 @@ def test_requests( rows = root.xpath('//table[@class="results"]/tbody/tr') assert len(rows) == defaults.PP + # Request page 2 of the requests page. + with client as request: + resp = request.get("/requests", params={"O": 50}, cookies=cookies) # Page 2 + assert resp.status_code == int(HTTPStatus.OK) + + assert "‹ Previous" in resp.text + assert "« First" in resp.text + + root = parse_root(resp.text) + rows = root.xpath('//table[@class="results"]/tbody/tr') + assert len(rows) == 5 # There are five records left on the second page. + + +def test_requests_with_filters( + client: TestClient, + tu_user: User, + packages: list[Package], + requests: list[PackageRequest], +): + cookies = {"AURSID": tu_user.login(Request(), "testPassword")} + with client as request: + resp = request.get( + "/requests", + params={ + # Pass in url query parameters O, SeB and SB to exercise + # their paths inside of the pager_nav used in this request. + "O": 0, # Page 1 + "SeB": "nd", + "SB": "n", + "filter_pending": True, + "filter_closed": True, + "filter_accepted": True, + "filter_rejected": True, + }, + cookies=cookies, + ) + assert resp.status_code == int(HTTPStatus.OK) + + assert "Next ›" in resp.text + assert "Last »" in resp.text + + root = parse_root(resp.text) + # We have 55 requests, our defaults.PP is 50, so expect we have 50 rows. + rows = root.xpath('//table[@class="results"]/tbody/tr') + assert len(rows) == defaults.PP + # Request page 2 of the requests page. with client as request: resp = request.get(