serializers.py 6.4 KB
Newer Older
1
2
3
4
import mimetypes
from base64 import b64encode

from django.contrib.staticfiles.finders import find as find_static_file
5
from django.urls import reverse
6
from django.utils import timezone
7
from django.utils.html import strip_tags
8
9
from rest_framework import serializers

10
from events.models import Event, Registration
11
12
13
14
15
16
17


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

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

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

    def _background_color(self, instance):
        pass

    def _text_color(self, instance):
        pass

    def _target_blank(self, instance):
        return False

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

67

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

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

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


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


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

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

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

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
    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()

143

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

    description = serializers.SerializerMethodField('_description')
151
    registered = serializers.SerializerMethodField('_registered')
152
153
154

    def _description(self, instance):
        return strip_tags(instance.description)
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194

    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