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

Add Event API fields to make sure design can be realised

parent 66f4cf30
import mimetypes
from base64 import b64encode
from django.contrib.staticfiles.finders import find as find_static_file
from django.urls import reverse
from django.utils import timezone
from django.utils.html import strip_tags
from rest_framework import serializers
from events.models import Event
from events.models import Event, Registration
class CalenderJSSerializer(serializers.ModelSerializer):
......@@ -93,22 +97,98 @@ class UnpublishedEventSerializer(CalenderJSSerializer):
class EventRetrieveSerializer(serializers.ModelSerializer):
class Meta:
model = Event
fields = ('title', 'description', 'start', 'end', 'organiser',
'location', 'price', 'fine')
fields = ('pk', 'title', 'description', 'start', 'end', 'organiser',
'category', 'registration_start', 'registration_end',
'cancel_deadline', 'location', 'map_location', 'price',
'fine', 'max_participants', 'num_participants', 'status',
'user_registration', 'registration_allowed',
'no_registration_message', 'has_fields')
description = serializers.SerializerMethodField('_description')
user_registration = serializers.SerializerMethodField('_user_registration')
num_participants = serializers.SerializerMethodField('_num_participants')
registration_allowed = serializers.SerializerMethodField(
'_registration_allowed')
has_fields = serializers.SerializerMethodField('_has_fields')
def _description(self, instance):
return strip_tags(instance.description)
def _num_participants(self, instance):
return instance.num_participants()
def _user_registration(self, instance):
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(),
}
except Registration.DoesNotExist:
return None
def _registration_allowed(self, instance):
member = self.context['request'].user.member
return (member is not None and
member.current_membership is not None and
member.can_attend_events)
def _has_fields(self, instance):
return instance.has_fields()
class EventListSerializer(serializers.ModelSerializer):
class Meta:
model = Event
fields = ('title', 'description', 'start',
'location', 'price', 'pk')
fields = ('pk', 'title', 'description', 'start', 'end',
'location', 'price', 'registered')
description = serializers.SerializerMethodField('_description')
registered = serializers.SerializerMethodField('_registered')
def _description(self, instance):
return strip_tags(instance.description)
def _registered(self, instance):
try:
return instance.is_member_registered(
self.context['request'].user.member)
except AttributeError:
return None
class EventRegistrationSerializer(serializers.ModelSerializer):
class Meta:
model = Registration
fields = ('pk', 'member', 'name', 'photo')
name = serializers.SerializerMethodField('_name')
photo = serializers.SerializerMethodField('_photo')
member = serializers.SerializerMethodField('_member')
def _member(self, instance):
if instance.member:
return instance.member.pk
return None
def _name(self, instance):
if instance.member:
return instance.member.display_name()
return instance.name
def _photo(self, instance):
if instance.member and instance.member.photo:
f = instance.member.photo.file
type = mimetypes.guess_type(f.name)[0]
photo = ''.join(['data:{};base64,'.format(type),
b64encode(f.read()).decode()])
else:
filename = find_static_file('members/images/default-avatar.jpg')
with open(filename, 'rb') as f:
photo = ''.join(['data:image/jpeg;base64,',
b64encode(f.read()).decode()])
return photo
......@@ -2,19 +2,19 @@ from datetime import datetime
from django.utils import timezone
from rest_framework import viewsets
from rest_framework.decorators import list_route, detail_route
from rest_framework.exceptions import ParseError
from rest_framework.response import Response
from rest_framework.decorators import list_route
from rest_framework.permissions import IsAuthenticated, IsAdminUser
from rest_framework.response import Response
from events.api.permissions import UnpublishedEventPermissions
from events.api.serializers import (
EventCalenderJSSerializer,
UnpublishedEventSerializer,
EventRetrieveSerializer,
EventListSerializer
)
from events.models import Event
EventListSerializer,
EventRegistrationSerializer)
from events.models import Event, Registration
def _extract_date_range(request):
......@@ -42,6 +42,15 @@ class EventViewset(viewsets.ReadOnlyModelViewSet):
return EventRetrieveSerializer
return EventCalenderJSSerializer
@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)
return Response(serializer.data)
@list_route(permission_classes=[])
def calendarjs(self, request):
end, start = _extract_date_range(request)
......
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