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.urls import reverse
from django.utils import timezone from django.utils import timezone
from django.utils.html import strip_tags from django.utils.html import strip_tags
from rest_framework import serializers from rest_framework import serializers
from events.models import Event from events.models import Event, Registration
class CalenderJSSerializer(serializers.ModelSerializer): class CalenderJSSerializer(serializers.ModelSerializer):
...@@ -93,22 +97,98 @@ class UnpublishedEventSerializer(CalenderJSSerializer): ...@@ -93,22 +97,98 @@ class UnpublishedEventSerializer(CalenderJSSerializer):
class EventRetrieveSerializer(serializers.ModelSerializer): class EventRetrieveSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Event model = Event
fields = ('title', 'description', 'start', 'end', 'organiser', fields = ('pk', 'title', 'description', 'start', 'end', 'organiser',
'location', 'price', 'fine') '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') 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): def _description(self, instance):
return strip_tags(instance.description) 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 EventListSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Event model = Event
fields = ('title', 'description', 'start', fields = ('pk', 'title', 'description', 'start', 'end',
'location', 'price', 'pk') 'location', 'price', 'registered')
description = serializers.SerializerMethodField('_description') description = serializers.SerializerMethodField('_description')
registered = serializers.SerializerMethodField('_registered')
def _description(self, instance): def _description(self, instance):
return strip_tags(instance.description) 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 ...@@ -2,19 +2,19 @@ from datetime import datetime
from django.utils import timezone from django.utils import timezone
from rest_framework import viewsets from rest_framework import viewsets
from rest_framework.decorators import list_route, detail_route
from rest_framework.exceptions import ParseError 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.permissions import IsAuthenticated, IsAdminUser
from rest_framework.response import Response
from events.api.permissions import UnpublishedEventPermissions from events.api.permissions import UnpublishedEventPermissions
from events.api.serializers import ( from events.api.serializers import (
EventCalenderJSSerializer, EventCalenderJSSerializer,
UnpublishedEventSerializer, UnpublishedEventSerializer,
EventRetrieveSerializer, EventRetrieveSerializer,
EventListSerializer EventListSerializer,
) EventRegistrationSerializer)
from events.models import Event from events.models import Event, Registration
def _extract_date_range(request): def _extract_date_range(request):
...@@ -42,6 +42,15 @@ class EventViewset(viewsets.ReadOnlyModelViewSet): ...@@ -42,6 +42,15 @@ class EventViewset(viewsets.ReadOnlyModelViewSet):
return EventRetrieveSerializer return EventRetrieveSerializer
return EventCalenderJSSerializer 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=[]) @list_route(permission_classes=[])
def calendarjs(self, request): def calendarjs(self, request):
end, start = _extract_date_range(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