serializers.py 6.85 KB
Newer Older
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
1
from django.templatetags.static import static
2
from django.urls import reverse
3
from django.utils import timezone
4
from django.utils.html import strip_tags
5
6
from rest_framework import serializers

7
from events.models import Event, Registration
8
from pizzas.models import PizzaEvent
9
from thaliawebsite.settings import settings
10
11
12
13
14
15
16


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

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

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

    def _background_color(self, instance):
        pass

    def _text_color(self, instance):
        pass

    def _target_blank(self, instance):
        return False

63
64
65
    def _registered(self, instance):
        return None

66

67
class EventCalenderJSSerializer(CalenderJSSerializer):
68
69
70
71
    class Meta(CalenderJSSerializer.Meta):
        model = Event

    def _url(self, instance):
72
        return reverse('events:event', kwargs={'event_id': instance.id})
73
74
75

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


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
94
95


96
class EventRetrieveSerializer(serializers.ModelSerializer):
Gijs Hendriksen's avatar
Gijs Hendriksen committed
97
98
    class Meta:
        model = Event
99
100
101
102
103
104
        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')
AuckeBos's avatar
AuckeBos committed
105

106
    description = serializers.SerializerMethodField('_description')
107
108
109
110
111
    user_registration = serializers.SerializerMethodField('_user_registration')
    num_participants = serializers.SerializerMethodField('_num_participants')
    registration_allowed = serializers.SerializerMethodField(
        '_registration_allowed')
    has_fields = serializers.SerializerMethodField('_has_fields')
AuckeBos's avatar
AuckeBos committed
112

113
114
    def _description(self, instance):
        return strip_tags(instance.description)
115

116
117
118
119
120
121
122
    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)
123
            return RegistrationSerializer(reg, context=self.context).data
124
125
126
127
128
129
130
131
132
133
134
135
        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()

136

137
class EventListSerializer(serializers.ModelSerializer):
AuckeBos's avatar
AuckeBos committed
138
139
    class Meta:
        model = Event
140
        fields = ('pk', 'title', 'description', 'start', 'end',
141
                  'location', 'price', 'registered', 'pizza')
142
143

    description = serializers.SerializerMethodField('_description')
144
    registered = serializers.SerializerMethodField('_registered')
145
    pizza = serializers.SerializerMethodField('_pizza')
146
147
148

    def _description(self, instance):
        return strip_tags(instance.description)
149
150
151
152
153
154
155
156

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

157
158
159
160
    def _pizza(self, instance):
        pizza_events = PizzaEvent.objects.filter(event=instance)
        return pizza_events.exists()

161

162
class RegistrationSerializer(serializers.ModelSerializer):
163
164
    class Meta:
        model = Registration
165
166
        fields = ('pk', 'member', 'name', 'photo', 'registered_on',
                  'is_late_cancellation', 'is_cancelled', 'queue_position')
167
168
169
170

    name = serializers.SerializerMethodField('_name')
    photo = serializers.SerializerMethodField('_photo')
    member = serializers.SerializerMethodField('_member')
171
172
173
174
175
176
177
178
179
180
181
182
    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):
        return (instance.queue_position()
183
                if instance.queue_position() > 0 else None)
184
185
186

    def _is_cancelled(self, instance):
        return instance.date_cancelled is not None
187
188
189
190
191
192
193
194
195
196
197
198
199

    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:
200
201
            return self.context['request'].build_absolute_uri(
                '%s%s' % (settings.MEDIA_URL, instance.member.photo))
202
        else:
203
            return self.context['request'].build_absolute_uri(
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
204
                static('members/images/default-avatar.jpg'))