Skip to content

Commit

Permalink
Pagination for events query (#46)
Browse files Browse the repository at this point in the history
* initial pagination added. Ambigious as we had three states

* Change handling of pagination and skiping for current/upcoming events

* updated search and club filter

* Even with name, don't turn off pagination

---------

Co-authored-by: Bhav Beri <bhavberi@gmail.com>
Co-authored-by: Bhav Beri <43399374+bhavberi@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 11, 2024
1 parent 607e368 commit 2f41390
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 15 deletions.
18 changes: 14 additions & 4 deletions queries/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,11 @@ def eventid(code: str, info: Info) -> str:
def events(
info: Info,
clubid: str | None = None,
name: str | None = None,
public: bool | None = None,
paginationOn: bool = False,
limit: int | None = None,
skip: int = 0,
) -> List[EventType]:
"""
if public is set, then return only public/approved events
Expand Down Expand Up @@ -130,6 +133,9 @@ def events(
restrictAccess and not restrictFullAccess
), "restrictAccess and not restrictFullAccess can not be True at the same time." # noqa: E501

if not limit and paginationOn:
raise Exception("Pagination limit is required.")

searchspace: dict[str, Any] = {}
if clubid is not None:
searchspace["$or"] = [
Expand Down Expand Up @@ -165,10 +171,14 @@ def events(
"$in": statuses,
}

events = eventsWithSorting(searchspace, date_filter=False)

if limit is not None:
events = events[:limit]
events = eventsWithSorting(
searchspace,
date_filter=False,
pagination=paginationOn,
name=name,
skip=skip,
limit=limit,
)

if restrictAccess or public:
for event in events:
Expand Down
58 changes: 47 additions & 11 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,14 @@ def getRoleEmails(role: str) -> List[str]:
return []


def eventsWithSorting(searchspace, date_filter=False):
def eventsWithSorting(
searchspace,
name: str | None = None,
date_filter=False,
pagination=False,
skip=0,
limit: int | None = None,
):
"""
Custom sorting of events based on
datetimeperiod with
Expand All @@ -268,6 +275,9 @@ def eventsWithSorting(searchspace, date_filter=False):
)
return events

if name is not None and pagination:
searchspace["name"] = {"$regex": name, "$options": "i"}

ongoing_events_query = {
**searchspace,
"datetimeperiod.0": {"$lte": current_datetime},
Expand All @@ -282,16 +292,42 @@ def eventsWithSorting(searchspace, date_filter=False):
"datetimeperiod.1": {"$lt": current_datetime},
}

ongoing_events = list(
eventsdb.find(ongoing_events_query).sort("datetimeperiod.1", 1)
)
upcoming_events = list(
eventsdb.find(upcoming_events_query).sort("datetimeperiod.0", 1)
)
past_events = list(
eventsdb.find(past_events_query).sort("datetimeperiod.1", -1)
)
events = ongoing_events + upcoming_events + past_events
if pagination:
if skip < 0:
ongoing_events = list(
eventsdb.find(ongoing_events_query).sort(
"datetimeperiod.0", -1
)
)
upcoming_events = list(
eventsdb.find(upcoming_events_query).sort(
"datetimeperiod.0", 1
)
)

events = ongoing_events + upcoming_events
else:
past_events = list(
eventsdb.find(past_events_query)
.sort("datetimeperiod.1", -1)
.skip(skip)
.limit(limit)
)
events = past_events
else:
ongoing_events = list(
eventsdb.find(ongoing_events_query).sort("datetimeperiod.0", -1)
)
upcoming_events = list(
eventsdb.find(upcoming_events_query).sort("datetimeperiod.0", 1)
)
past_events = list(
eventsdb.find(past_events_query).sort("datetimeperiod.1", -1)
)

events = ongoing_events + upcoming_events + past_events
if limit:
events = events[:limit]

return events

Expand Down

0 comments on commit 2f41390

Please sign in to comment.