serializers.py 7.79 KB
Newer Older
1
2
from html import unescape

Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
3
from django.templatetags.static import static
4
from django.urls import reverse
5
from django.utils import timezone
6
from django.utils.html import strip_tags
7
8
from rest_framework import serializers

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


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

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

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

    def _background_color(self, instance):
        pass

    def _text_color(self, instance):
        pass

    def _target_blank(self, instance):
        return False

65
66
67
    def _registered(self, instance):
        return None

68

69
class EventCalenderJSSerializer(CalenderJSSerializer):
70
71
72
73
    class Meta(CalenderJSSerializer.Meta):
        model = Event

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

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


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
96
97


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

108
    description = serializers.SerializerMethodField('_description')
109
110
111
112
113
    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
114

115
    def _description(self, instance):
116
        return unescape(strip_tags(instance.description))
117

118
    def _num_participants(self, instance):
119
120
        if (instance.max_participants and
                instance.num_participants() > instance.max_participants):
121
            return instance.max_participants
122
123
124
125
126
127
        return instance.num_participants()

    def _user_registration(self, instance):
        try:
            reg = instance.registration_set.get(
                member=self.context['request'].user.member)
128
            return RegistrationSerializer(reg, context=self.context).data
129
130
131
132
133
134
135
136
137
138
139
140
        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()

141

142
class EventListSerializer(serializers.ModelSerializer):
AuckeBos's avatar
AuckeBos committed
143
144
    class Meta:
        model = Event
145
        fields = ('pk', 'title', 'description', 'start', 'end',
146
                  'location', 'price', 'registered', 'pizza')
147
148

    description = serializers.SerializerMethodField('_description')
149
    registered = serializers.SerializerMethodField('_registered')
150
    pizza = serializers.SerializerMethodField('_pizza')
151
152

    def _description(self, instance):
153
        return unescape(strip_tags(instance.description))
154
155
156
157
158
159
160
161

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

162
163
164
165
    def _pizza(self, instance):
        pizza_events = PizzaEvent.objects.filter(event=instance)
        return pizza_events.exists()

166

167
class RegistrationSerializer(serializers.ModelSerializer):
168
169
    class Meta:
        model = Registration
170
171
        fields = ('pk', 'member', 'name', 'photo', 'registered_on',
                  'is_late_cancellation', 'is_cancelled', 'queue_position')
172
173
174
175

    name = serializers.SerializerMethodField('_name')
    photo = serializers.SerializerMethodField('_photo')
    member = serializers.SerializerMethodField('_member')
176
    registered_on = serializers.SerializerMethodField('_registered_on')
177
178
179
180
181
182
    is_cancelled = serializers.SerializerMethodField('_is_cancelled')
    is_late_cancellation = serializers.SerializerMethodField(
        '_is_late_cancellation')
    queue_position = serializers.SerializerMethodField(
        '_queue_position')

183
    def _has_view_permission(self, instance):
Joost Rijneveld's avatar
Joost Rijneveld committed
184
185
        # We dont have an explicit viewing permission model, so we rely on the
        #  'change' permission (Django provides add/change/delete by default)
186
187
188
        return (self.context['request'].user.has_perm('events.change_event')
                or instance.member.user == self.context['request'].user)

189
    def _is_late_cancellation(self, instance):
190
191
192
        if self._has_view_permission(instance):
            return instance.is_late_cancellation()
        return None
193
194

    def _queue_position(self, instance):
195
196
197
198
199
200
201
202
203
        if self._has_view_permission(instance):
            pos = instance.queue_position()
            return pos if pos > 0 else None
        return None

    def _registered_on(self, instance):
        if self._has_view_permission(instance):
            return serializers.DateTimeField().to_representation(instance.date)
        return None
204
205

    def _is_cancelled(self, instance):
206
207
208
        if self._has_view_permission(instance):
            return instance.date_cancelled is not None
        return None
209
210
211

    def _member(self, instance):
        if instance.member:
212
            return instance.member.user.pk
213
214
215
216
217
218
219
220
221
        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:
222
223
            return self.context['request'].build_absolute_uri(
                '%s%s' % (settings.MEDIA_URL, instance.member.photo))
224
        else:
225
            return self.context['request'].build_absolute_uri(
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
226
                static('members/images/default-avatar.jpg'))