From 9be00864a02417ae5b06bd902e178646482ae655 Mon Sep 17 00:00:00 2001 From: Torgrim Limstrand Date: Sun, 27 Oct 2024 09:38:04 +0100 Subject: [PATCH 1/5] add: database is now seeded with 18 old events from 2022 --- db/seeds/events.json | 257 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 256 insertions(+), 1 deletion(-) diff --git a/db/seeds/events.json b/db/seeds/events.json index ff4a7de..78dc31d 100644 --- a/db/seeds/events.json +++ b/db/seeds/events.json @@ -50,5 +50,260 @@ "bindingRegistration": true, "registrationOpeningDate": "2030-04-15 16:30:00", "registeredPenalties": [] + }, + { + "eid": "6819761d9f814b1897cda685c658b19c", + "title": "Teknologisk fremtid med CompTech", + "address": "Realfagbygget C112", + "date": "2022-05-15 10:00:00", + "duration": 2, + "price": 0, + "maxParticipants": 40, + "description": "CompTech holder en inspirerende presentasjon om teknologiske nyvinninger og hvordan de påvirker samfunnet vårt. Det blir en diskusjonsrunde med mingling etterpå.", + "picturePath": "db/eventImages/uuid4.png", + "participants": [], + "transportation": true, + "food": true, + "public": false, + "bindingRegistration": false, + "registeredPenalties": [] + }, + { + "eid": "ff7f44d7ae784bfb8c1a795bffb8b456", + "title": "Foredrag om bærekraft", + "address": "Universitetsaulaen", + "date": "2022-06-12 14:00:00", + "duration": 2, + "price": 0, + "maxParticipants": 100, + "description": "Et foredrag om hvordan teknologibransjen kan bidra til bærekraftige løsninger i fremtiden. Foredragsholdere fra flere kjente selskaper vil være tilstede.", + "picturePath": "db/eventImages/uuid5.png", + "participants": [], + "transportation": false, + "food": true, + "public": true, + "bindingRegistration": true, + "registeredPenalties": [] + }, + { + "eid": "76fa60301e724fa5acbc3d01ec106d1f", + "title": "Workshop om AI og ML", + "address": "Realfagbygget A101", + "date": "2022-07-14 15:00:00", + "duration": 3, + "price": 50, + "maxParticipants": 50, + "description": "Delta på en spennende workshop om AI og maskinlæring, med fokus på nybegynnere og viderekomne. Mat og drikke blir servert.", + "picturePath": "db/eventImages/uuid6.png", + "participants": [], + "transportation": false, + "food": true, + "public": true, + "bindingRegistration": false, + "registeredPenalties": [] + }, + { + "eid": "10108a19e6dd4863a500755678467095", + "title": "Networking med Startups", + "address": "Innovasjonsparken", + "date": "2022-08-10 18:00:00", + "duration": 2, + "price": 0, + "maxParticipants": 40, + "description": "Bli med på en spennende kveld med networking. Møt representanter fra ulike startups og lær om deres spennende prosjekter.", + "picturePath": "db/eventImages/uuid7.png", + "participants": [], + "transportation": true, + "food": true, + "public": true, + "bindingRegistration": true, + "registeredPenalties": [] + }, + { + "eid": "74d6ad1b442546e4b762935301ed252f", + "title": "Hackathon: Code for a Cause", + "address": "Informatikkbygget D200", + "date": "2022-09-15 09:00:00", + "duration": 8, + "price": 0, + "maxParticipants": 100, + "description": "Delta på hackathon for en god sak. Utvikle løsninger som kan hjelpe lokale organisasjoner med teknologibaserte utfordringer.", + "picturePath": "db/eventImages/uuid8.png", + "participants": [], + "transportation": false, + "food": true, + "public": false, + "bindingRegistration": true, + "registeredPenalties": [] + }, + { + "eid": "5bcea836b3cb489eb658b9e5ab0a93b7", + "title": "Introkurs i Python", + "address": "Realfagbygget B120", + "date": "2022-10-02 12:00:00", + "duration": 3, + "price": 0, + "maxParticipants": 25, + "description": "Et kurs for nybegynnere i Python-programmering. Lær grunnleggende konsepter og bygg dine første programmer.", + "picturePath": "db/eventImages/uuid9.png", + "participants": [], + "transportation": false, + "food": true, + "public": true, + "bindingRegistration": false, + "registeredPenalties": [] + }, + { + "eid": "59669e52716f4e82bf5365498b79bb8c", + "title": "Kveldsmingling med Accenture", + "address": "Realfagbygget A200", + "date": "2022-11-05 17:00:00", + "duration": 2, + "price": 0, + "maxParticipants": 30, + "description": "Accenture inviterer til mingling og nettverksbygging. Lær om deres prosjekter og karrieremuligheter i selskapet.", + "picturePath": "db/eventImages/uuid10.png", + "participants": [], + "transportation": true, + "food": false, + "public": true, + "bindingRegistration": true, + "registeredPenalties": [] + }, + { + "eid": "d01da25dc2894dcdbb098bb28752c6e5", + "title": "Workshop i prosjektledelse", + "address": "Informatikkbygget A110", + "date": "2022-12-10 14:00:00", + "duration": 2, + "price": 20, + "maxParticipants": 20, + "description": "Lær grunnleggende prosjektledelse og metodikk som Scrum og Kanban. Perfekt for deg som ønsker å lede egne prosjekter.", + "picturePath": "db/eventImages/uuid11.png", + "participants": [], + "transportation": false, + "food": false, + "public": true, + "bindingRegistration": false, + "registeredPenalties": [] + }, + { + "eid": "99fb1f7b428d4fd3a3856fa184f345ec", + "title": "Introduksjon til UI/UX design", + "address": "Realfagbygget C204", + "date": "2022-04-22 09:30:00", + "duration": 2, + "price": 0, + "maxParticipants": 35, + "description": "Lær om prinsipper for UI/UX design og hvordan du kan forbedre brukeropplevelsen i dine prosjekter.", + "picturePath": "db/eventImages/uuid12.png", + "participants": [], + "transportation": true, + "food": false, + "public": false, + "bindingRegistration": true, + "registeredPenalties": [] + }, + { + "eid": "a6cab28b4de540d9a47f2ee55807b102", + "title": "Workshop i DevOps", + "address": "Informatikkbygget E120", + "date": "2022-05-17 16:00:00", + "duration": 2, + "price": 0, + "maxParticipants": 40, + "description": "Lær DevOps-verktøy og metodikk, inkludert CI/CD og automatisering. En praktisk workshop for teknologientusiaster.", + "picturePath": "db/eventImages/uuid13.png", + "participants": [], + "transportation": true, + "food": true, + "public": true, + "bindingRegistration": true, + "registeredPenalties": [] + }, + { + "eid": "37c3970af14546028467ef6235de3dff", + "title": "Forelesning i IT-sikkerhet", + "address": "Realfagbygget B101", + "date": "2022-06-20 12:00:00", + "duration": 2, + "price": 0, + "maxParticipants": 45, + "description": "Bli med på en informativ forelesning om cybersikkerhet og hvilke trusler bedrifter står overfor.", + "picturePath": "db/eventImages/uuid14.png", + "participants": [], + "transportation": true, + "food": false, + "public": false, + "bindingRegistration": true, + "registeredPenalties": [] + }, + { + "eid": "a891362a04e345e79810aec1d451386b", + "title": "Kodekveld med Open Source", + "address": "Informatikkbygget A220", + "date": "2022-09-25 17:30:00", + "duration": 3, + "price": 0, + "maxParticipants": 20, + "description": "Utforsk verdenen av open-source-prosjekter. Lær hvordan du kan bidra og hva som skal til for å bli en open-source-utvikler.", + "picturePath": "db/eventImages/uuid15.png", + "participants": [], + "transportation": false, + "food": true, + "public": true, + "bindingRegistration": false, + "registeredPenalties": [] + }, + { + "eid": "a07930c3119746f586d029cb9d89ad67", + "title": "Foredrag: Karriereveier i tech", + "address": "Realfagbygget A003", + "date": "2022-03-23 15:00:00", + "duration": 2, + "price": 0, + "maxParticipants": 50, + "description": "Lær om muligheter og karriereveier i teknologibransjen. Ideelt for nyutdannede og studenter som vurderer ulike retninger.", + "picturePath": "db/eventImages/uuid16.png", + "participants": [], + "transportation": true, + "food": true, + "public": true, + "bindingRegistration": true, + "registeredPenalties": [] + }, + { + "eid": "322e7ad606fb4be2b79208aab6c81eb8", + "title": "Workshop i datastrukturer", + "address": "Informatikkbygget C103", + "date": "2022-11-08 13:00:00", + "duration": 2, + "price": 0, + "maxParticipants": 30, + "description": "Grunnleggende workshop i datastrukturer og algoritmer. Lær om lister, stakker, køer og trær, samt deres praktiske anvendelser.", + "picturePath": "db/eventImages/uuid17.png", + "participants": [], + "transportation": false, + "food": false, + "public": true, + "bindingRegistration": true, + "registeredPenalties": [] + }, + { + "eid": "09effc6304fe489c921cadc53e195efa", + "title": "Introduksjon til Kotlin", + "address": "Realfagbygget A200", + "date": "2022-10-05 10:00:00", + "duration": 2, + "price": 0, + "maxParticipants": 15, + "description": "Et introduksjonskurs i programmeringsspråket Kotlin, spesielt for Java-utviklere som ønsker å lære mer.", + "picturePath": "db/eventImages/uuid18.png", + "participants": [], + "transportation": true, + "food": true, + "public": true, + "bindingRegistration": true, + "registeredPenalties": [] } -] \ No newline at end of file +] From 9811b48ee1341d058bce87c9984bb8b783242140 Mon Sep 17 00:00:00 2001 From: Torgrim Limstrand Date: Sun, 27 Oct 2024 09:53:14 +0100 Subject: [PATCH 2/5] Add pagination to /past-events endpoint with skip and limit parameters --- app/api/events.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/api/events.py b/app/api/events.py index c5fa745..94de4bc 100644 --- a/app/api/events.py +++ b/app/api/events.py @@ -1,7 +1,7 @@ from datetime import datetime, timedelta import os import shutil -from fastapi import APIRouter, Response, Request, HTTPException, Depends, BackgroundTasks +from fastapi import APIRouter, Response, Request, HTTPException, Depends, BackgroundTasks, Query from fastapi.datastructures import UploadFile from fastapi.param_functions import File from pydantic import ValidationError @@ -94,7 +94,9 @@ def get_upcoming_events(request: Request, token: AccessTokenPayload = Depends(op @router.get('/past-events') -def get_past_events(request: Request, token: AccessTokenPayload = Depends(optional_authentication)): +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)): """ Get last 10 events that have passed """ # TODO: Expand endpoint to accept custom amount? db = get_database(request) @@ -114,7 +116,8 @@ def get_past_events(request: Request, token: AccessTokenPayload = Depends(option pipeline = [ {"$match": search_filter}, {"$sort": {"date": -1}}, - {"$limit": 10} + {"$skip": skip}, + {"$limit": limit}, ] res = db.events.aggregate(pipeline) From dbd584ef0017f5c3493a29381c3c3b0c253c1ef8 Mon Sep 17 00:00:00 2001 From: Torgrim Limstrand Date: Sun, 27 Oct 2024 18:25:15 +0100 Subject: [PATCH 3/5] Add endpoints for past events and their count MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🗃️ Add 15 new past events to seed data for testing ✨ Implement /past-events/count endpoint to get the count of past events ✨ Implement /past-events endpoint to retrieve past events with pagination ✅ Add tests for the two new API endpoints --- app/api/events.py | 23 ++- db/seeds/test_seeds/test_events.json | 255 +++++++++++++++++++++++++++ tests/test_endpoints/test_event.py | 41 ++++- 3 files changed, 308 insertions(+), 11 deletions(-) diff --git a/app/api/events.py b/app/api/events.py index 94de4bc..be48320 100644 --- a/app/api/events.py +++ b/app/api/events.py @@ -1,4 +1,4 @@ -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone import os import shutil from fastapi import APIRouter, Response, Request, HTTPException, Depends, BackgroundTasks, Query @@ -93,11 +93,28 @@ def get_upcoming_events(request: Request, token: AccessTokenPayload = Depends(op return [Event.model_validate(event) for event in upcoming_events] +@router.get('/past-events/count') +def get_past_events_count(request: Request, token: AccessTokenPayload = Depends(optional_authentication)): + """ Get count of past events """ + db = get_database(request) + + # Get current UTC time as an ISO-formatted string + date_str = datetime.now(timezone.utc).isoformat(timespec='seconds') + + # Apply search filter according to role + search_filter = {'date': {"$lt": date_str}} + if token and token.role != Role.admin: + search_filter["public"] = True + + # Count the number of documents that match the filter + count = db.events.count_documents(search_filter) + return {"count": count} + @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)): - """ Get last 10 events that have passed """ + """ Get last events that have passed """ # TODO: Expand endpoint to accept custom amount? db = get_database(request) @@ -112,7 +129,7 @@ def get_past_events(request: Request, token: AccessTokenPayload = Depends(option if token and token.role == Role.admin: search_filter = {'date': {"$lt": date}} - # Get the last 10 events + # Get the last events pipeline = [ {"$match": search_filter}, {"$sort": {"date": -1}}, diff --git a/db/seeds/test_seeds/test_events.json b/db/seeds/test_seeds/test_events.json index 7a265e2..391b70e 100644 --- a/db/seeds/test_seeds/test_events.json +++ b/db/seeds/test_seeds/test_events.json @@ -30,5 +30,260 @@ "public": true, "bindingRegistration": false, "registeredPenalties": [] + }, + { + "eid": "6819761d9f814b1897cda685c658b19c", + "title": "Teknologisk fremtid med CompTech", + "address": "Realfagbygget C112", + "date": "2022-05-15 10:00:00", + "duration": 2, + "price": 0, + "maxParticipants": 40, + "description": "CompTech holder en inspirerende presentasjon om teknologiske nyvinninger og hvordan de påvirker samfunnet vårt. Det blir en diskusjonsrunde med mingling etterpå.", + "picturePath": "db/eventImages/uuid4.png", + "participants": [], + "transportation": true, + "food": true, + "public": true, + "bindingRegistration": false, + "registeredPenalties": [] + }, + { + "eid": "ff7f44d7ae784bfb8c1a795bffb8b456", + "title": "Foredrag om bærekraft", + "address": "Universitetsaulaen", + "date": "2022-06-12 14:00:00", + "duration": 2, + "price": 0, + "maxParticipants": 100, + "description": "Et foredrag om hvordan teknologibransjen kan bidra til bærekraftige løsninger i fremtiden. Foredragsholdere fra flere kjente selskaper vil være tilstede.", + "picturePath": "db/eventImages/uuid5.png", + "participants": [], + "transportation": false, + "food": true, + "public": true, + "bindingRegistration": true, + "registeredPenalties": [] + }, + { + "eid": "76fa60301e724fa5acbc3d01ec106d1f", + "title": "Workshop om AI og ML", + "address": "Realfagbygget A101", + "date": "2022-07-14 15:00:00", + "duration": 3, + "price": 50, + "maxParticipants": 50, + "description": "Delta på en spennende workshop om AI og maskinlæring, med fokus på nybegynnere og viderekomne. Mat og drikke blir servert.", + "picturePath": "db/eventImages/uuid6.png", + "participants": [], + "transportation": false, + "food": true, + "public": true, + "bindingRegistration": false, + "registeredPenalties": [] + }, + { + "eid": "10108a19e6dd4863a500755678467095", + "title": "Networking med Startups", + "address": "Innovasjonsparken", + "date": "2022-08-10 18:00:00", + "duration": 2, + "price": 0, + "maxParticipants": 40, + "description": "Bli med på en spennende kveld med networking. Møt representanter fra ulike startups og lær om deres spennende prosjekter.", + "picturePath": "db/eventImages/uuid7.png", + "participants": [], + "transportation": true, + "food": true, + "public": true, + "bindingRegistration": true, + "registeredPenalties": [] + }, + { + "eid": "74d6ad1b442546e4b762935301ed252f", + "title": "Hackathon: Code for a Cause", + "address": "Informatikkbygget D200", + "date": "2022-09-15 09:00:00", + "duration": 8, + "price": 0, + "maxParticipants": 100, + "description": "Delta på hackathon for en god sak. Utvikle løsninger som kan hjelpe lokale organisasjoner med teknologibaserte utfordringer.", + "picturePath": "db/eventImages/uuid8.png", + "participants": [], + "transportation": false, + "food": true, + "public": true, + "bindingRegistration": true, + "registeredPenalties": [] + }, + { + "eid": "5bcea836b3cb489eb658b9e5ab0a93b7", + "title": "Introkurs i Python", + "address": "Realfagbygget B120", + "date": "2022-10-02 12:00:00", + "duration": 3, + "price": 0, + "maxParticipants": 25, + "description": "Et kurs for nybegynnere i Python-programmering. Lær grunnleggende konsepter og bygg dine første programmer.", + "picturePath": "db/eventImages/uuid9.png", + "participants": [], + "transportation": false, + "food": true, + "public": true, + "bindingRegistration": false, + "registeredPenalties": [] + }, + { + "eid": "59669e52716f4e82bf5365498b79bb8c", + "title": "Kveldsmingling med Accenture", + "address": "Realfagbygget A200", + "date": "2022-11-05 17:00:00", + "duration": 2, + "price": 0, + "maxParticipants": 30, + "description": "Accenture inviterer til mingling og nettverksbygging. Lær om deres prosjekter og karrieremuligheter i selskapet.", + "picturePath": "db/eventImages/uuid10.png", + "participants": [], + "transportation": true, + "food": false, + "public": true, + "bindingRegistration": true, + "registeredPenalties": [] + }, + { + "eid": "d01da25dc2894dcdbb098bb28752c6e5", + "title": "Workshop i prosjektledelse", + "address": "Informatikkbygget A110", + "date": "2022-12-10 14:00:00", + "duration": 2, + "price": 20, + "maxParticipants": 20, + "description": "Lær grunnleggende prosjektledelse og metodikk som Scrum og Kanban. Perfekt for deg som ønsker å lede egne prosjekter.", + "picturePath": "db/eventImages/uuid11.png", + "participants": [], + "transportation": false, + "food": false, + "public": true, + "bindingRegistration": false, + "registeredPenalties": [] + }, + { + "eid": "99fb1f7b428d4fd3a3856fa184f345ec", + "title": "Introduksjon til UI/UX design", + "address": "Realfagbygget C204", + "date": "2022-04-22 09:30:00", + "duration": 2, + "price": 0, + "maxParticipants": 35, + "description": "Lær om prinsipper for UI/UX design og hvordan du kan forbedre brukeropplevelsen i dine prosjekter.", + "picturePath": "db/eventImages/uuid12.png", + "participants": [], + "transportation": true, + "food": false, + "public": true, + "bindingRegistration": true, + "registeredPenalties": [] + }, + { + "eid": "a6cab28b4de540d9a47f2ee55807b102", + "title": "Workshop i DevOps", + "address": "Informatikkbygget E120", + "date": "2022-05-17 16:00:00", + "duration": 2, + "price": 0, + "maxParticipants": 40, + "description": "Lær DevOps-verktøy og metodikk, inkludert CI/CD og automatisering. En praktisk workshop for teknologientusiaster.", + "picturePath": "db/eventImages/uuid13.png", + "participants": [], + "transportation": true, + "food": true, + "public": true, + "bindingRegistration": true, + "registeredPenalties": [] + }, + { + "eid": "37c3970af14546028467ef6235de3dff", + "title": "Forelesning i IT-sikkerhet", + "address": "Realfagbygget B101", + "date": "2022-06-20 12:00:00", + "duration": 2, + "price": 0, + "maxParticipants": 45, + "description": "Bli med på en informativ forelesning om cybersikkerhet og hvilke trusler bedrifter står overfor.", + "picturePath": "db/eventImages/uuid14.png", + "participants": [], + "transportation": true, + "food": false, + "public": true, + "bindingRegistration": true, + "registeredPenalties": [] + }, + { + "eid": "a891362a04e345e79810aec1d451386b", + "title": "Kodekveld med Open Source", + "address": "Informatikkbygget A220", + "date": "2022-09-25 17:30:00", + "duration": 3, + "price": 0, + "maxParticipants": 20, + "description": "Utforsk verdenen av open-source-prosjekter. Lær hvordan du kan bidra og hva som skal til for å bli en open-source-utvikler.", + "picturePath": "db/eventImages/uuid15.png", + "participants": [], + "transportation": false, + "food": true, + "public": true, + "bindingRegistration": false, + "registeredPenalties": [] + }, + { + "eid": "a07930c3119746f586d029cb9d89ad67", + "title": "Foredrag: Karriereveier i tech", + "address": "Realfagbygget A003", + "date": "2022-03-23 15:00:00", + "duration": 2, + "price": 0, + "maxParticipants": 50, + "description": "Lær om muligheter og karriereveier i teknologibransjen. Ideelt for nyutdannede og studenter som vurderer ulike retninger.", + "picturePath": "db/eventImages/uuid16.png", + "participants": [], + "transportation": true, + "food": true, + "public": true, + "bindingRegistration": true, + "registeredPenalties": [] + }, + { + "eid": "322e7ad606fb4be2b79208aab6c81eb8", + "title": "Workshop i datastrukturer", + "address": "Informatikkbygget C103", + "date": "2022-11-08 13:00:00", + "duration": 2, + "price": 0, + "maxParticipants": 30, + "description": "Grunnleggende workshop i datastrukturer og algoritmer. Lær om lister, stakker, køer og trær, samt deres praktiske anvendelser.", + "picturePath": "db/eventImages/uuid17.png", + "participants": [], + "transportation": false, + "food": false, + "public": true, + "bindingRegistration": true, + "registeredPenalties": [] + }, + { + "eid": "09effc6304fe489c921cadc53e195efa", + "title": "Introduksjon til Kotlin", + "address": "Realfagbygget A200", + "date": "2022-10-05 10:00:00", + "duration": 2, + "price": 0, + "maxParticipants": 15, + "description": "Et introduksjonskurs i programmeringsspråket Kotlin, spesielt for Java-utviklere som ønsker å lære mer.", + "picturePath": "db/eventImages/uuid18.png", + "participants": [], + "transportation": true, + "food": true, + "public": true, + "bindingRegistration": true, + "registeredPenalties": [] } ] diff --git a/tests/test_endpoints/test_event.py b/tests/test_endpoints/test_event.py index 41d67d9..e2ebd4e 100644 --- a/tests/test_endpoints/test_event.py +++ b/tests/test_endpoints/test_event.py @@ -122,19 +122,44 @@ def test_get_upcoming_events(client): response = client.get("/api/upcoming/") assert len(response.json()) == 1 - -def test_get_past_events(client): - # Login +def test_get_past_events_count(client): + # Login as admin client_login(client, admin_member["email"], admin_member["password"]) - # Create future event - response = client.post('/api/event/', json=new_event) + # Get count of past events + response = client.get('/api/event/past-events/count') assert response.status_code == 200 + count_response = response.json() + assert "count" in count_response + + # Assert that the count matches the expected number + expected_past_events_count = 17 + assert count_response["count"] == expected_past_events_count + - # Should only get past events - response = client.get('/api/event/past-events/') +def test_get_past_events_with_pagination(client): + # Login as admin + client_login(client, admin_member["email"], admin_member["password"]) + + # Get total count of past events + response = client.get('/api/event/past-events/count') assert response.status_code == 200 - assert len(response.json()) == len(test_events) + total_past_events = response.json()["count"] + + # Set pagination parameters + limit = 5 + 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}') + assert response.status_code == 200 + past_events = response.json() + + # Calculate expected number of events on this page + expected_events = min(limit, total_past_events - skip) + assert len(past_events) == expected_events @authentication_required("/api/event/joined-events", "get") From f4d105d14764f583585c0216670c537314418b19 Mon Sep 17 00:00:00 2001 From: Torgrim Limstrand Date: Tue, 29 Oct 2024 14:35:22 +0100 Subject: [PATCH 4/5] =?UTF-8?q?=E2=9C=A8=20add=20endpoint=20for=20getting?= =?UTF-8?q?=20count=20of=20total=20events?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/events.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/api/events.py b/app/api/events.py index be48320..d954b06 100644 --- a/app/api/events.py +++ b/app/api/events.py @@ -95,14 +95,14 @@ 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)): - """ Get count of past events """ + """Get count of past events""" db = get_database(request) - # Get current UTC time as an ISO-formatted string - date_str = datetime.now(timezone.utc).isoformat(timespec='seconds') + # Get current UTC datetime + current_datetime = datetime.now(timezone.utc) # Apply search filter according to role - search_filter = {'date': {"$lt": date_str}} + search_filter = {"date": {"$lt": current_datetime}} if token and token.role != Role.admin: search_filter["public"] = True From 4d6f5ed8dca366b70f38d1aed4826c5e85a7816b Mon Sep 17 00:00:00 2001 From: Torgrim Limstrand Date: Fri, 13 Dec 2024 11:57:23 +0100 Subject: [PATCH 5/5] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor=20past=20even?= =?UTF-8?q?ts=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()