Skip to content

Commit

Permalink
Added filter for expired and upcoming events (#714)
Browse files Browse the repository at this point in the history
* Added filter for expired and upcoming events

* Removed registration filter on event has ended

* Added manual filtering for expired events in user viewset

* Removed unused import

* Started testing expired event method

* Finished testing filter methode

* format

* removed incorrect True comparison

* Updated changlog

* Added tests where query params give false

* Used CaseInsensitiveBooleanQueryParam for handling all types of query params + removed comments

* Fixed typo

* Formatting

---------

Co-authored-by: Harry Linrui XU <[email protected]>
Co-authored-by: haruixu <[email protected]>
  • Loading branch information
3 people authored Nov 20, 2023
1 parent c2d8f44 commit 9d09af3
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
-**Betalte arrangementer med Vipps betaling**. Det kan nå opprettes arrangementer som krever betaling for å melde seg på. Denne betalingen betales via Vipps.
-**Nyheter** Fondesforvalere kan nå lage nyheter.
-**Arrangementer** Du kan nå se hvilken plass du har på ventelisten til et arrangement.
-**Profil** Filtrere kommende og tidligere arrangementer

## Versjon 2022.10.13

Expand Down
8 changes: 7 additions & 1 deletion app/content/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,17 @@ def get_user_detail_strikes(self, request, *args, **kwargs):
@action(detail=False, methods=["get"], url_path="me/events")
def get_user_events(self, request, *args, **kwargs):
registrations = request.user.registrations.all()

# Apply the filter
filter_field = self.request.query_params.get("expired")
event_has_ended = CaseInsensitiveBooleanQueryParam(filter_field)

events = [
registration.event
for registration in registrations
if not registration.event.expired
if registration.event.expired == event_has_ended
]

return self.paginate_response(
data=events, serializer=EventListSerializer, context={"request": request}
)
Expand Down
98 changes: 98 additions & 0 deletions app/tests/content/test_user_integration.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from datetime import timedelta

from django.utils import timezone
from django.utils.text import slugify
from rest_framework import status

import pytest

from app.common.enums import AdminGroup, GroupType
from app.content.factories.event_factory import EventFactory
from app.content.factories.registration_factory import RegistrationFactory
from app.content.factories.strike_factory import StrikeFactory
from app.content.factories.user_factory import UserFactory
Expand Down Expand Up @@ -32,6 +36,10 @@ def _get_user_detail_url(user):
return f"{API_USER_BASE_URL}{user.user_id}/"


def _get_user_events_url():
return f"{API_USER_BASE_URL}me/events/"


def _get_user_post_data():
return {
"email": "[email protected]",
Expand Down Expand Up @@ -469,3 +477,93 @@ def test_destroy_other_user_as_index_user(member, user, api_client):
response = client.delete(url)

assert response.status_code == status.HTTP_200_OK


@pytest.mark.django_db
def test_list_expired_user_events(member, api_client):
""" "All the events listed as expired should be expired"""
client = api_client(user=member)

two_days_ago = timezone.now() - timedelta(days=2)
event = EventFactory(end_date=two_days_ago)

registration = RegistrationFactory(user=member, event=event)

url = _get_user_events_url()

query_params = {"expired": "true"}

response = client.get(url, data=query_params)

assert response.status_code == status.HTTP_200_OK

registrations = response.json().get("results")
for registration in registrations:
assert registration.get("expired")


@pytest.mark.django_db
def test_list_unexpired_user_events(member, api_client):
"""All the events listed as unexpired should be unexpired"""
client = api_client(user=member)

two_days_ago = timezone.now() - timedelta(days=2)
event = EventFactory(end_date=two_days_ago)

registration = RegistrationFactory(user=member, event=event)

url = _get_user_events_url()

query_params = {"expired": "false"}

response = client.get(url, data=query_params)

assert response.status_code == status.HTTP_200_OK

registrations = response.json().get("results")
for registration in registrations:
assert not registration.get("expired")


@pytest.mark.django_db
def test_list_expired_user_events_with_a_blank_query_params(member, api_client):
"""All the events listed should be unexpired when returning a blank query params"""
client = api_client(user=member)

two_days_ago = timezone.now() - timedelta(days=2)
event = EventFactory(end_date=two_days_ago)

registration = RegistrationFactory(user=member, event=event)

url = _get_user_events_url()

query_params = {"expired": ""}

response = client.get(url, data=query_params)

assert response.status_code == status.HTTP_200_OK

registrations = response.json().get("results")
for registration in registrations:
assert not registration.get("expired")


@pytest.mark.django_db
def test_list_expired_user_events_with_no_query_params(member, api_client):
"""All the events listed should be unexpired with no query params"""
client = api_client(user=member)

two_days_ago = timezone.now() - timedelta(days=2)
event = EventFactory(end_date=two_days_ago)

registration = RegistrationFactory(user=member, event=event)

url = _get_user_events_url()

response = client.get(url)

assert response.status_code == status.HTTP_200_OK

registrations = response.json().get("results")
for registration in registrations:
assert not registration.get("expired")

0 comments on commit 9d09af3

Please sign in to comment.