Commit 5078234a authored by Gijs Hendriksen's avatar Gijs Hendriksen
Browse files

Added admin serializer for the event registrations

parent 7d65f64a
......@@ -116,7 +116,7 @@ class EventRetrieveSerializer(serializers.ModelSerializer):
'fine', 'max_participants', 'num_participants',
'user_registration', 'registration_allowed',
'no_registration_message', 'has_fields', 'is_pizza_event',
'google_maps_url')
'google_maps_url', 'is_admin')
description = serializers.SerializerMethodField('_description')
user_registration = serializers.SerializerMethodField('_user_registration')
......@@ -126,6 +126,7 @@ class EventRetrieveSerializer(serializers.ModelSerializer):
has_fields = serializers.SerializerMethodField('_has_fields')
is_pizza_event = serializers.SerializerMethodField('_is_pizza_event')
google_maps_url = serializers.SerializerMethodField('_google_maps_url')
is_admin = serializers.SerializerMethodField('_is_admin')
def _description(self, instance):
return strip_spaces_between_tags(bleach(instance.description))
......@@ -140,7 +141,8 @@ class EventRetrieveSerializer(serializers.ModelSerializer):
try:
reg = instance.registration_set.get(
member=self.context['request'].member)
return RegistrationListSerializer(reg, context=self.context).data
return RegistrationAdminListSerializer(reg,
context=self.context).data
except Registration.DoesNotExist:
return None
......@@ -161,6 +163,10 @@ class EventRetrieveSerializer(serializers.ModelSerializer):
zoom=13,
size='450x250')
def _is_admin(self, instance):
member = self.context['request'].member
return services.is_organiser(member, instance)
class EventListSerializer(serializers.ModelSerializer):
"""Custom list serializer for events"""
......@@ -192,30 +198,11 @@ class RegistrationListSerializer(serializers.ModelSerializer):
"""Custom registration list serializer"""
class Meta:
model = Registration
fields = ('pk', 'member', 'name', 'photo', 'avatar', 'registered_on',
'is_late_cancellation', 'is_cancelled', 'queue_position',
'payment', 'present')
fields = ('pk', 'member', 'name', 'avatar')
name = serializers.SerializerMethodField('_name')
photo = serializers.SerializerMethodField('_photo')
avatar = serializers.SerializerMethodField('_avatar')
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', read_only=False)
def _is_late_cancellation(self, instance):
return instance.is_late_cancellation()
def _queue_position(self, instance):
pos = instance.queue_position
return pos if pos > 0 else None
def _is_cancelled(self, instance):
return instance.date_cancelled is not None
def _member(self, instance):
if instance.member:
......@@ -227,14 +214,6 @@ class RegistrationListSerializer(serializers.ModelSerializer):
return instance.member.profile.display_name()
return instance.name
def _photo(self, instance):
if instance.member and instance.member.profile.photo:
return self.context['request'].build_absolute_uri(
'%s%s' % (settings.MEDIA_URL, instance.member.profile.photo))
else:
return self.context['request'].build_absolute_uri(
static('members/images/default-avatar.jpg'))
def _avatar(self, instance):
placeholder = self.context['request'].build_absolute_uri(
static('members/images/default-avatar.jpg'))
......@@ -246,6 +225,36 @@ class RegistrationListSerializer(serializers.ModelSerializer):
size_large='800x800')
class RegistrationAdminListSerializer(RegistrationListSerializer):
"""Custom registration admin list serializer"""
class Meta:
model = Registration
fields = ('pk', 'member', 'name', 'registered_on', 'is_cancelled',
'is_late_cancellation', 'queue_position', 'payment',
'present', 'avatar')
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):
pos = instance.queue_position
return pos if pos > 0 else None
def _is_cancelled(self, instance):
return instance.date_cancelled is not None
def _name(self, instance):
if instance.member:
return instance.member.get_full_name()
return instance.name
class RegistrationSerializer(serializers.ModelSerializer):
"""Registration serializer"""
information_fields = None
......
......@@ -21,7 +21,10 @@ from events.api.serializers import (
UnpublishedEventSerializer,
EventRetrieveSerializer,
EventListSerializer,
RegistrationListSerializer, RegistrationSerializer)
RegistrationListSerializer,
RegistrationAdminListSerializer,
RegistrationSerializer
)
from events.exceptions import RegistrationError
from events.models import Event, Registration
from utils.snippets import extract_date_range
......@@ -107,8 +110,13 @@ class EventViewset(viewsets.ReadOnlyModelViewSet):
queryset = Registration.objects.filter(
event=pk, date_cancelled=None)[:event.max_participants]
serializer = RegistrationListSerializer(queryset, many=True,
context={'request': request})
context = {'request': request}
if services.is_organiser(self.request.member, event):
serializer = RegistrationAdminListSerializer(queryset, many=True,
context=context)
else:
serializer = RegistrationListSerializer(queryset, many=True,
context=context)
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