serializers.py 2.46 KB
Newer Older
1
from django.urls import reverse
2
from django.utils import timezone
3
4
from rest_framework import serializers

5
from events.models import Event, Registration
6
7
8
9
10
11
12


class CalenderJSSerializer(serializers.ModelSerializer):
    class Meta:
        fields = (
            'start', 'end', 'all_day', 'is_birthday',
            'url', 'title', 'description',
13
14
            'backgroundColor', 'textColor', 'blank',
            'registered'
15
16
17
18
19
20
21
22
23
        )

    start = serializers.SerializerMethodField('_start')
    end = serializers.SerializerMethodField('_end')
    all_day = serializers.SerializerMethodField('_all_day')
    is_birthday = serializers.SerializerMethodField('_is_birthday')
    url = serializers.SerializerMethodField('_url')
    title = serializers.SerializerMethodField('_title')
    description = serializers.SerializerMethodField('_description')
24
25
26
    backgroundColor = serializers.SerializerMethodField('_background_color')
    textColor = serializers.SerializerMethodField('_text_color')
    blank = serializers.SerializerMethodField('_target_blank')
27
    registered = serializers.SerializerMethodField('_registered')
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

    def _start(self, instance):
        return timezone.localtime(instance.start)

    def _end(self, instance):
        return timezone.localtime(instance.end)

    def _all_day(self, instance):
        return False

    def _is_birthday(self, instance):
        return False

    def _url(self, instance):
        raise NotImplementedError

    def _title(self, instance):
        return instance.title

    def _description(self, instance):
        return instance.description

    def _background_color(self, instance):
        pass

    def _text_color(self, instance):
        pass

    def _target_blank(self, instance):
        return False

59
60
61
    def _registered(self, instance):
        return None

62
63
64
65
66
67

class EventSerializer(CalenderJSSerializer):
    class Meta(CalenderJSSerializer.Meta):
        model = Event

    def _url(self, instance):
68
        return reverse('events:event', kwargs={'event_id': instance.id})
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

    def _registered(self, instance):
        try:
            if not self.context['user'].member:
                return None
        except AttributeError:
            return None

        try:
            registration = Registration.objects.get(
                event=instance,
                member=self.context['user'].member
            )
        except (Registration.DoesNotExist, AttributeError):
            return False

        return registration.is_registered()