serializers.py 2.99 KB
Newer Older
1
from django.urls import reverse
2
from django.utils import timezone
3
from django.utils.html import strip_tags
4
5
from rest_framework import serializers

6
from events.models import Event
7
8
9
10
11
12
13


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

    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')
25
26
27
    backgroundColor = serializers.SerializerMethodField('_background_color')
    textColor = serializers.SerializerMethodField('_text_color')
    blank = serializers.SerializerMethodField('_target_blank')
28
    registered = serializers.SerializerMethodField('_registered')
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

    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):
49
        return strip_tags(instance.description)
50
51
52
53
54
55
56
57
58
59

    def _background_color(self, instance):
        pass

    def _text_color(self, instance):
        pass

    def _target_blank(self, instance):
        return False

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

63
64
65
66
67
68

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

    def _url(self, instance):
69
        return reverse('events:event', kwargs={'event_id': instance.id})
70
71
72

    def _registered(self, instance):
        try:
73
74
75
            return instance.is_member_registered(self.context['user'].member)
        except AttributeError:
            return None
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90


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

    def _background_color(self, instance):
        return "#888888"

    def _text_color(self, instance):
        return "white"

    def _url(self, instance):
        return reverse('events:admin-details', kwargs={
            'event_id': instance.id})
Gijs Hendriksen's avatar
Gijs Hendriksen committed
91
92
93
94
95
96
97


class EventDataSerializer(serializers.ModelSerializer):
    class Meta:
        model = Event
        fields = ('title', 'description', 'start', 'end', 'organiser',
                  'location', 'price', 'fine')
AuckeBos's avatar
AuckeBos committed
98
99
100
101
102
103
104


class EventDataForEventListSerializer(serializers.ModelSerializer):
    class Meta:
        model = Event
        fields = ('title', 'description', 'start',
                  'location', 'price', 'id')