diff --git a/CHANGELOG.md b/CHANGELOG.md index 48850105..591e7d99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ ## Neste versjon +- ⚡**Profil**. Endret rekkefølge på tidligere arrangementer slik at nyeste kommer først. + ## Versjon 2024.09.14 - ⚡**Codex**. Det er nå et skille mellom dokumenter opprettet av Drift og Index. diff --git a/app/content/views/user.py b/app/content/views/user.py index b8ef4e18..f7b5d5d2 100644 --- a/app/content/views/user.py +++ b/app/content/views/user.py @@ -1,5 +1,6 @@ from django.db import IntegrityError from django.shortcuts import get_object_or_404 +from django.utils import timezone from django_filters.rest_framework import DjangoFilterBackend from rest_framework import filters, status from rest_framework.decorators import action @@ -21,6 +22,7 @@ from app.communication.notifier import Notify from app.content.filters import UserFilter from app.content.models import User +from app.content.models.event import Event from app.content.serializers import ( DefaultUserSerializer, EventListSerializer, @@ -304,17 +306,19 @@ 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) + event_has_ended = CaseInsensitiveBooleanQueryParam(filter_field).value - events = [ - registration.event - for registration in registrations - if registration.event.expired == event_has_ended.value - ] + now = timezone.now() + + if event_has_ended: + events = Event.objects.filter( + registered_users_list=request.user, end_date__lte=now + ).order_by("-start_date") + else: + events = Event.objects.filter( + registered_users_list=request.user, end_date__gt=now + ).order_by("start_date") return self.paginate_response( data=events, serializer=EventListSerializer, context={"request": request} diff --git a/app/tests/content/test_user_bio_integration.py b/app/tests/content/test_user_bio_integration.py index a55978d3..b1afc5c7 100644 --- a/app/tests/content/test_user_bio_integration.py +++ b/app/tests/content/test_user_bio_integration.py @@ -1,7 +1,13 @@ +from datetime import timedelta + +from django.utils import timezone from rest_framework import status import pytest +from app.content.factories.event_factory import EventFactory +from app.content.factories.registration_factory import RegistrationFactory +from app.content.models.registration import Registration from app.content.models.user_bio import UserBio from app.util.test_utils import get_api_client @@ -122,3 +128,67 @@ def test_destroy_other_bios(member, user_bio): assert response.status_code == status.HTTP_403_FORBIDDEN assert len(UserBio.objects.filter(id=user_bio.id)) + + +@pytest.mark.django_db +def test_get_user_events_sorted_when_expired_true(member, api_client): + """When the expired filter is 'true', the events should be sorted by start_date in descending order""" + event1 = EventFactory( + start_date=timezone.now() - timedelta(days=5), + end_date=timezone.now() - timedelta(days=4), + ) + event2 = EventFactory( + start_date=timezone.now() - timedelta(days=10), + end_date=timezone.now() - timedelta(days=9), + ) + event3 = EventFactory( + start_date=timezone.now() - timedelta(days=2), + end_date=timezone.now() - timedelta(days=1), + ) + + RegistrationFactory(user=member, event=event1) + RegistrationFactory(user=member, event=event2) + RegistrationFactory(user=member, event=event3) + + client = api_client(user=member) + response = client.get("/users/me/events/?page=1&expired=true") + + assert response.status_code == status.HTTP_200_OK + + event_ids = [event3.id, event1.id, event2.id] + returned_event_ids = [event["id"] for event in response.data["results"]] + + assert returned_event_ids == event_ids + + +@pytest.mark.django_db +def test_get_user_events_unsorted_when_expired_false(member, api_client): + """When the expired filter is not 'true', the events should not be sorted by start_date""" + event1 = EventFactory( + start_date=timezone.now() + timedelta(days=5), + end_date=timezone.now() + timedelta(days=6), + ) + event2 = EventFactory( + start_date=timezone.now() + timedelta(days=10), + end_date=timezone.now() + timedelta(days=11), + ) + event3 = EventFactory( + start_date=timezone.now() + timedelta(days=1), + end_date=timezone.now() + timedelta(days=2), + ) + + RegistrationFactory(user=member, event=event1) + RegistrationFactory(user=member, event=event2) + RegistrationFactory(user=member, event=event3) + + client = api_client(user=member) + response = client.get("/users/me/events/?page=1&expired=false") + + assert response.status_code == status.HTTP_200_OK + + registration_ids = Registration.objects.filter(user=member).values_list( + "event_id", flat=True + ) + returned_event_ids = [event["id"] for event in response.data["results"]] + + assert returned_event_ids == list(registration_ids)