From d6ad2391a2d5c85dabfd42df285a567f299a50d8 Mon Sep 17 00:00:00 2001 From: Mads Nylund Date: Fri, 11 Oct 2024 20:18:06 +0200 Subject: [PATCH] fixed race condition --- app/content/models/event.py | 2 +- app/content/views/registration.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/content/models/event.py b/app/content/models/event.py index 21871d1a..c23401fc 100644 --- a/app/content/models/event.py +++ b/app/content/models/event.py @@ -136,7 +136,7 @@ def get_has_attended(self): return self.get_participants().filter(has_attended=True) def get_participants(self): - return self.registrations.select_for_update().filter(is_on_wait=False) + return self.registrations.filter(is_on_wait=False) def get_waiting_list(self): """Number of users on the waiting list""" diff --git a/app/content/views/registration.py b/app/content/views/registration.py index 520b9405..0aa13b3a 100644 --- a/app/content/views/registration.py +++ b/app/content/views/registration.py @@ -1,6 +1,6 @@ import uuid -from django.db import transaction +from django.db.transaction import atomic from django.shortcuts import get_object_or_404 from django_filters.rest_framework import DjangoFilterBackend from rest_framework import filters, status @@ -49,7 +49,7 @@ def _is_own_registration(self): def _is_not_own_registration(self): return not self._is_own_registration() - @transaction.atomic + @atomic def create(self, request, *args, **kwargs): """Register the current user for the given event.""" @@ -70,7 +70,7 @@ def create(self, request, *args, **kwargs): serializer.is_valid(raise_exception=True) event_id = self.kwargs.get("event_id", None) - event = Event.objects.get(pk=event_id) + event = Event.objects.select_for_update().get(pk=event_id) registration = super().perform_create( serializer, event=event, user=request.user