From 4a484bfb426d817379aa6d6b2049c2a8a7cd73ae Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 9 Nov 2024 16:04:47 +0100 Subject: [PATCH 1/2] add current_time in search --- app/routers/events.py | 96 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/app/routers/events.py b/app/routers/events.py index 43d6f0e..cdff7b8 100644 --- a/app/routers/events.py +++ b/app/routers/events.py @@ -6,6 +6,7 @@ from ..models import events, users from pydantic import EmailStr from typing import Annotated from bson import ObjectId +from datetime import datetime router = APIRouter() @@ -42,7 +43,7 @@ async def read_events(authorize: Annotated[bool, Depends(permissions_checker.Per @router.get("/events/search", tags=["events"], response_model=list[events.EventOut]) -async def search_events(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], skip: int = 0, limit: int = 20, item: str | None = None, status: int = 1, min_lat: float | None = None, max_lat: float | None = None, min_lon: float | None = None, max_lon: float | None = None): +async def search_events(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], skip: int = 0, limit: int = 20, item: str | None = None, status: int = 1, min_lat: float | None = None, max_lat: float | None = None, min_lon: float | None = None, max_lon: float | None = None, current_datetime: datetime | None = None): if limit < 1 or skip < 0 or limit < skip: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, @@ -70,6 +71,37 @@ async def search_events(authorize: Annotated[bool, Depends(permissions_checker.P } ] } + if current_datetime is not None: + object_search = { + "$and": [ + {"status": {"$eq": status}}, + { + "latitude": {"$gte": min_lat}, # Minimum latitude + }, + { + "latitude": {"$lte": max_lat}, # Maximum latitude + }, + { + "longitude": {"$gte": min_lon}, # Minimum longitude + }, + { + "longitude": {"$lte": max_lon}, # Maximum longitude + }, + { + "$or": [ + {"start_date": {"$gte": current_datetime}}, # Upcoming events + {"$and": [ # Ongoing events + {"start_date": {"$lte": current_datetime}}, # Already started + {"$or": [ + {"end_date": {"$gte": current_datetime}}, # End date in the future + {"end_date": None} # No end date set + ]} + ]} + ] + } + ] + } + if item is not None: object_search = { "$and": [ @@ -83,6 +115,31 @@ async def search_events(authorize: Annotated[bool, Depends(permissions_checker.P {"status": {"$eq": status}} ] } + if current_datetime is not None: + object_search = { + "$and": [ + { + "$or": [ + {"name": {"$regex": item}}, + {"tags": {"$regex": item}}, + {"organizers": {"$regex": item}} + ] + }, + {"status": {"$eq": status}}, + { + "$or": [ + {"start_date": {"$gte": current_datetime}}, # Upcoming events + {"$and": [ # Ongoing events + {"start_date": {"$lte": current_datetime}}, # Already started + {"$or": [ + {"end_date": {"$gte": current_datetime}}, # End date in the future + {"end_date": None} # No end date set + ]} + ]} + ] + } + ] + } if min_lat is not None and max_lat is not None and min_lon is not None and max_lon is not None: object_search = { "$and": [ @@ -108,6 +165,43 @@ async def search_events(authorize: Annotated[bool, Depends(permissions_checker.P } ] } + if current_datetime is not None: + object_search = { + "$and": [ + { + "$or": [ + {"name": {"$regex": item}}, + {"tags": {"$regex": item}}, + {"organizers": {"$regex": item}} + ] + }, + {"status": {"$eq": status}}, + { + "latitude": {"$gte": min_lat}, # Minimum latitude + }, + { + "latitude": {"$lte": max_lat}, # Maximum latitude + }, + { + "longitude": {"$gte": min_lon}, # Minimum longitude + }, + { + "longitude": {"$lte": max_lon}, # Maximum longitude + }, + { + "$or": [ + {"start_date": {"$gte": current_datetime}}, # Upcoming events + {"$and": [ # Ongoing events + {"start_date": {"$lte": current_datetime}}, # Already started + {"$or": [ + {"end_date": {"$gte": current_datetime}}, # End date in the future + {"end_date": None} # No end date set + ]} + ]} + ] + } + ] + } for event_index in event_repository.find_by(object_search, limit=limit, skip=skip): event = events.EventOut(id=event_index.id, tags=event_index.tags, imgUrl=event_index.imgUrl, name=event_index.name, description=event_index.description, place=event_index.place, status=event_index.status, start_date=event_index.start_date, end_date=event_index.end_date) From 49a22bc567a7867a82fdab6a14228ddaf6804137 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 9 Nov 2024 16:19:52 +0100 Subject: [PATCH 2/2] add current_datetime in events --- app/routers/events.py | 83 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 81 insertions(+), 2 deletions(-) diff --git a/app/routers/events.py b/app/routers/events.py index cdff7b8..66ac51c 100644 --- a/app/routers/events.py +++ b/app/routers/events.py @@ -12,7 +12,7 @@ router = APIRouter() @router.get("/events", tags=["events"], response_model=list[events.EventOut]) -async def read_events(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], skip: int = 0, limit: int = 20, id_event: str | None = None, name: str | None = None, status: int = 1, tags: str | None = None, organizers: str | None = None): +async def read_events(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], skip: int = 0, limit: int = 20, id_event: str | None = None, name: str | None = None, status: int = 1, tags: str | None = None, organizers: str | None = None, current_datetime: datetime | None = None): if limit < 1 or skip < 0 or limit < skip: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, @@ -24,16 +24,95 @@ async def read_events(authorize: Annotated[bool, Depends(permissions_checker.Per object_search = {} if status is not 1: object_search = {"status":{"$eq": status}} - + if current_datetime is not None: + object_search = { "$and": [ {"status":{"$eq": status}}, + { + "$or": [ + {"start_date": {"$gte": current_datetime}}, # Upcoming events + {"$and": [ # Ongoing events + {"start_date": {"$lte": current_datetime}}, # Already started + {"$or": [ + {"end_date": {"$gte": current_datetime}}, # End date in the future + {"end_date": None} # No end date set + ]} + ]} + ] + } + ] } if tags is not None: object_search = {"$and":[{"tags":{"$eq": tags}}, {"status":{"$eq":status}}]} + if current_datetime is not None: + object_search = {"$and":[{"tags":{"$eq": tags}}, + {"status":{"$eq":status}}, + { + "$or": [ + {"start_date": {"$gte": current_datetime}}, # Upcoming events + {"$and": [ # Ongoing events + {"start_date": {"$lte": current_datetime}}, # Already started + {"$or": [ + {"end_date": {"$gte": current_datetime}}, # End date in the future + {"end_date": None} # No end date set + ]} + ]} + ] + } + ] + } if organizers is not None: object_search = {"$and":[{"organizers":{"$eq": organizers}}, {"status":{"$eq":status}}]} + if current_datetime is not None: + object_search = {"$and":[{"organizers":{"$eq": organizers}}, + {"status":{"$eq":status}}, + { + "$or": [ + {"start_date": {"$gte": current_datetime}}, # Upcoming events + {"$and": [ # Ongoing events + {"start_date": {"$lte": current_datetime}}, # Already started + {"$or": [ + {"end_date": {"$gte": current_datetime}}, # End date in the future + {"end_date": None} # No end date set + ]} + ]} + ] + } + ]} if id_event is not None: eventid = ObjectId(id_event) object_search = {"$and":[{"id":{"$regex": eventid}}, {"status":{"$eq":status}}]} + if current_datetime is not None: + object_search = {"$and":[{"id":{"$regex": eventid}}, + {"status":{"$eq":status}}, + { + "$or": [ + {"start_date": {"$gte": current_datetime}}, # Upcoming events + {"$and": [ # Ongoing events + {"start_date": {"$lte": current_datetime}}, # Already started + {"$or": [ + {"end_date": {"$gte": current_datetime}}, # End date in the future + {"end_date": None} # No end date set + ]} + ]} + ] + } + ]} if name is not None: object_search = {"$and":[{"name":{"$regex": name}}, {"status":{"$eq":status}}]} + if current_datetime is not None: + object_search = {"$and":[{"name":{"$regex": name}}, + {"status":{"$eq":status}}, + { + "$or": [ + {"start_date": {"$gte": current_datetime}}, # Upcoming events + {"$and": [ # Ongoing events + {"start_date": {"$lte": current_datetime}}, # Already started + {"$or": [ + {"end_date": {"$gte": current_datetime}}, # End date in the future + {"end_date": None} # No end date set + ]} + ]} + ] + } + ]} for event_index in event_repository.find_by(object_search, limit=limit, skip=skip):