From 4d6f5ed8dca366b70f38d1aed4826c5e85a7816b Mon Sep 17 00:00:00 2001 From: Torgrim Limstrand Date: Fri, 13 Dec 2024 11:57:23 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor=20past=20events?= =?UTF-8?q?=20filtering=20and=20pagination?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit will: - Refactor the `get_past_events` endpoint to use a fixed query limit - Now uses localtime instead of utctime in `get_past_events` endpoint - Refactor the 'get_past_events' endpoint to use the same mongodb-query logic as the rest of the functions for consistency - Update pagination parameters in the test suite to reflect the new fixed limit and validate functionality. - Ensure tests verify filtering and pagination logic with updated query constraints. --- app/api/events.py | 22 ++++++++++++++-------- tests/test_endpoints/test_event.py | 4 ++-- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/app/api/events.py b/app/api/events.py index d954b06..0b4a930 100644 --- a/app/api/events.py +++ b/app/api/events.py @@ -94,17 +94,23 @@ def get_upcoming_events(request: Request, token: AccessTokenPayload = Depends(op @router.get('/past-events/count') -def get_past_events_count(request: Request, token: AccessTokenPayload = Depends(optional_authentication)): +def get_past_events_count(request: Request, + token: AccessTokenPayload = Depends(optional_authentication)): """Get count of past events""" db = get_database(request) # Get current UTC datetime - current_datetime = datetime.now(timezone.utc) + current_datetime = datetime.now() # Apply search filter according to role - search_filter = {"date": {"$lt": current_datetime}} - if token and token.role != Role.admin: - search_filter["public"] = True + if token and token.role == Role.admin: + search_filter = { + 'date' : {"$lt": current_datetime} + } + else : + search_filter = { + "$and": [{'date' : {"$lt": current_datetime}}, {"public" : {"$eq": True}}] + } # Count the number of documents that match the filter count = db.events.count_documents(search_filter) @@ -112,8 +118,7 @@ def get_past_events_count(request: Request, token: AccessTokenPayload = Depends( @router.get('/past-events') def get_past_events(request: Request, token: AccessTokenPayload = Depends(optional_authentication), - skip: int = Query(0, ge=0), - limit: int = Query(10,ge=1,le=50)): + skip: int = Query(0, ge=0)): """ Get last events that have passed """ # TODO: Expand endpoint to accept custom amount? db = get_database(request) @@ -129,12 +134,13 @@ def get_past_events(request: Request, token: AccessTokenPayload = Depends(option if token and token.role == Role.admin: search_filter = {'date': {"$lt": date}} + fixed_limit = 10 # Get the last events pipeline = [ {"$match": search_filter}, {"$sort": {"date": -1}}, {"$skip": skip}, - {"$limit": limit}, + {"$limit": fixed_limit}, ] res = db.events.aggregate(pipeline) diff --git a/tests/test_endpoints/test_event.py b/tests/test_endpoints/test_event.py index e2ebd4e..7fb5b2f 100644 --- a/tests/test_endpoints/test_event.py +++ b/tests/test_endpoints/test_event.py @@ -147,13 +147,13 @@ def test_get_past_events_with_pagination(client): total_past_events = response.json()["count"] # Set pagination parameters - limit = 5 + limit = 10 total_pages = (total_past_events + limit - 1) // limit # Iterate through pages for page in range(total_pages): skip = page * limit - response = client.get(f'/api/event/past-events?skip={skip}&limit={limit}') + response = client.get(f'/api/event/past-events?skip={skip}') assert response.status_code == 200 past_events = response.json()