Verified Commit e60f6bfd authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg
Browse files

Use same serializer for all registration objects

parent 2954ceb5
......@@ -119,13 +119,7 @@ class EventRetrieveSerializer(serializers.ModelSerializer):
try:
reg = instance.registration_set.get(
member=self.context['request'].user.member)
return {
'registered_on': reg.date,
'queue_position': reg.queue_position()
if reg.queue_position() > 0 else None,
'is_cancelled': reg.date_cancelled is not None,
'is_late_cancellation': reg.is_late_cancellation(),
}
return RegistrationSerializer(reg, context=self.context).data
except Registration.DoesNotExist:
return None
......@@ -164,14 +158,31 @@ class EventListSerializer(serializers.ModelSerializer):
return pizza_events.exists()
class EventRegistrationSerializer(serializers.ModelSerializer):
class RegistrationSerializer(serializers.ModelSerializer):
class Meta:
model = Registration
fields = ('pk', 'member', 'name', 'photo')
fields = ('pk', 'member', 'name', 'photo', 'registered_on',
'is_late_cancellation', 'is_cancelled', 'queue_position')
name = serializers.SerializerMethodField('_name')
photo = serializers.SerializerMethodField('_photo')
member = serializers.SerializerMethodField('_member')
registered_on = serializers.DateTimeField(source='date')
is_cancelled = serializers.SerializerMethodField('_is_cancelled')
is_late_cancellation = serializers.SerializerMethodField(
'_is_late_cancellation')
queue_position = serializers.SerializerMethodField(
'_queue_position')
def _is_late_cancellation(self, instance):
return instance.is_late_cancellation()
def _queue_position(self, instance):
return (instance.queue_position()
if instance.queue_position() > 0 else None)
def _is_cancelled(self, instance):
return instance.date_cancelled is not None
def _member(self, instance):
if instance.member:
......
......@@ -13,7 +13,7 @@ from events.api.serializers import (
UnpublishedEventSerializer,
EventRetrieveSerializer,
EventListSerializer,
EventRegistrationSerializer)
RegistrationSerializer)
from events.models import Event, Registration
......@@ -42,13 +42,28 @@ class EventViewset(viewsets.ReadOnlyModelViewSet):
return EventRetrieveSerializer
return EventCalenderJSSerializer
def get_serializer_context(self):
return super().get_serializer_context()
@detail_route()
def registrations(self, request, pk):
event = Event.objects.get(pk=pk)
queryset = Registration.objects.filter(
event=pk, date_cancelled=None)[:event.max_participants]
serializer = EventRegistrationSerializer(queryset, many=True,
context={'request': request})
status = request.query_params.get('status', None)
queryset = Registration.objects.filter(event=pk)
if status is not None:
if status == 'queued':
queryset = Registration.objects.filter(
event=pk, date_cancelled=None)[event.max_participants:]
elif status == 'cancelled':
queryset = Registration.objects.filter(
event=pk, date_cancelled__not=None)
elif status == 'registered':
queryset = Registration.objects.filter(
event=pk, date_cancelled=None)[event.max_participants:]
serializer = RegistrationSerializer(queryset, many=True,
context={'request': request})
return Response(serializer.data)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment