Skip to content

Commit

Permalink
finished events now appear in the correct order (newest first) (#849)
Browse files Browse the repository at this point in the history
* finished events now appear in the correct order (newest first)

* added description of change in changelog

* fixed formatting

* updated method to use Django ORM instead of using python methods
  • Loading branch information
EmilJohns1 authored Sep 17, 2024
1 parent bf42e7a commit 99ba049
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
22 changes: 13 additions & 9 deletions app/content/views/user.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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}
Expand Down
70 changes: 70 additions & 0 deletions app/tests/content/test_user_bio_integration.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)

0 comments on commit 99ba049

Please sign in to comment.