serializers.py 6.56 KB
Newer Older
1
"""DRF serializers defined by the members package"""
2
from django.templatetags.static import static
3
4
from django.urls import reverse
from rest_framework import serializers
5
6

from events.api.serializers import CalenderJSSerializer
7
from members.models import Member, Profile
8
from members.services import member_achievements, member_societies
9
from thaliawebsite.api.services import create_image_thumbnail_dict
10

11
12

class MemberBirthdaySerializer(CalenderJSSerializer):
13
    """Serializer that renders the member birthdays to the CalendarJS format"""
Luko van der Maas's avatar
Luko van der Maas committed
14

15
16
17
18
    class Meta(CalenderJSSerializer.Meta):
        model = Member

    def _start(self, instance):
19
        return instance.profile.birthday
20
21
22
23
24
25
26
27
28
29
30

    def _end(self, instance):
        pass

    def _all_day(self, instance):
        return True

    def _is_birthday(self, instance):
        return True

    def _url(self, instance):
Luko van der Maas's avatar
Luko van der Maas committed
31
        return reverse("members:profile", kwargs={"pk": instance.pk})
32
33

    def _title(self, instance):
34
        return instance.profile.display_name()
35
36
37

    def _description(self, instance):
        membership = instance.current_membership
Luko van der Maas's avatar
Luko van der Maas committed
38
        if membership and membership.type == "honorary":
Thom Wiggers's avatar
Thom Wiggers committed
39
            return membership.get_type_display()
Luko van der Maas's avatar
Luko van der Maas committed
40
        return ""
41

Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
42
43
    def _class_names(self, instance):
        class_names = ["birthday-event"]
44
        membership = instance.current_membership
Luko van der Maas's avatar
Luko van der Maas committed
45
        if membership and membership.type == "honorary":
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
46
47
            class_names.append("honorary")
        return class_names
48
49


50
class ProfileRetrieveSerializer(serializers.ModelSerializer):
51
    """Serializer that renders a member profile"""
Luko van der Maas's avatar
Luko van der Maas committed
52

53
    class Meta:
54
        model = Profile
Luko van der Maas's avatar
Luko van der Maas committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
        fields = (
            "pk",
            "display_name",
            "avatar",
            "profile_description",
            "birthday",
            "starting_year",
            "programme",
            "website",
            "membership_type",
            "achievements",
            "societies",
        )

    pk = serializers.SerializerMethodField("_pk")
    avatar = serializers.SerializerMethodField("_avatar")
    birthday = serializers.SerializerMethodField("_birthday")
    membership_type = serializers.SerializerMethodField("_membership_type")
    achievements = serializers.SerializerMethodField("_achievements")
    societies = serializers.SerializerMethodField("_societies")
75

76
77
    def _pk(self, instance):
        return instance.user.pk
78

79
    def _birthday(self, instance):
80
81
        if instance.show_birthday:
            return instance.birthday
82
83
84
        return None

    def _membership_type(self, instance):
85
        membership = instance.user.current_membership
86
87
88
89
90
        if membership:
            return membership.type
        return None

    def _achievements(self, instance):
91
        return member_achievements(instance.user)
92

93
    def _societies(self, instance):
94
        return member_societies(instance.user)
95

96
    def _avatar(self, instance):
Luko van der Maas's avatar
Luko van der Maas committed
97
98
99
        placeholder = self.context["request"].build_absolute_uri(
            static("members/images/default-avatar.jpg")
        )
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
100
        file = None
101
102
        if instance.photo:
            file = instance.photo
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
103
        return create_image_thumbnail_dict(
Luko van der Maas's avatar
Luko van der Maas committed
104
105
            self.context["request"], file, placeholder=placeholder, size_large="800x800"
        )
106

107
108

class MemberListSerializer(serializers.ModelSerializer):
109
    """Serializer that renders a list of members"""
Luko van der Maas's avatar
Luko van der Maas committed
110

111
112
    class Meta:
        model = Member
Luko van der Maas's avatar
Luko van der Maas committed
113
        fields = ("pk", "display_name", "avatar")
114

Luko van der Maas's avatar
Luko van der Maas committed
115
116
    display_name = serializers.SerializerMethodField("_display_name")
    avatar = serializers.SerializerMethodField("_avatar")
117

118
119
120
    def _display_name(self, instance):
        return instance.profile.display_name()

121
    def _avatar(self, instance):
Luko van der Maas's avatar
Luko van der Maas committed
122
123
124
        placeholder = self.context["request"].build_absolute_uri(
            static("members/images/default-avatar.jpg")
        )
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
125
        file = None
126
        if instance.profile.photo:
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
127
128
            file = instance.profile.photo
        return create_image_thumbnail_dict(
Luko van der Maas's avatar
Luko van der Maas committed
129
130
            self.context["request"], file, placeholder=placeholder, size_large="800x800"
        )
131
132


133
class ProfileEditSerializer(serializers.ModelSerializer):
134
    """Serializer that renders a profile to be edited"""
Luko van der Maas's avatar
Luko van der Maas committed
135

136
137
    class Meta:
        model = Profile
Luko van der Maas's avatar
Luko van der Maas committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
        fields = (
            "pk",
            "email",
            "first_name",
            "last_name",
            "address_street",
            "address_street2",
            "address_postal_code",
            "address_city",
            "address_country",
            "phone_number",
            "show_birthday",
            "website",
            "photo",
            "emergency_contact",
            "emergency_contact_phone_number",
            "profile_description",
            "nickname",
            "display_name_preference",
            "language",
            "receive_optin",
            "receive_newsletter",
            "display_name",
            "avatar",
            "birthday",
            "starting_year",
            "programme",
            "membership_type",
            "achievements",
            "societies",
        )

        read_only_fields = ("display_name", "starting_year", "programme", "birthday")

    pk = serializers.SerializerMethodField("_pk")
    email = serializers.SerializerMethodField("_email")
    first_name = serializers.SerializerMethodField("_first_name")
    last_name = serializers.SerializerMethodField("_last_name")
    avatar = serializers.SerializerMethodField("_avatar")
    membership_type = serializers.SerializerMethodField("_membership_type")
    achievements = serializers.SerializerMethodField("_achievements")
    societies = serializers.SerializerMethodField("_societies")
180
181
182

    def _pk(self, instance):
        return instance.user.pk
183
184
185
186
187
188
189
190
191
192

    def _email(self, instance):
        return instance.user.email

    def _first_name(self, instance):
        return instance.user.first_name

    def _last_name(self, instance):
        return instance.user.last_name

193
194
195
196
197
198
199
200
201
202
203
204
205
    def _membership_type(self, instance):
        membership = instance.user.current_membership
        if membership:
            return membership.type
        return None

    def _achievements(self, instance):
        return member_achievements(instance.user)

    def _societies(self, instance):
        return member_societies(instance.user)

    def _avatar(self, instance):
Luko van der Maas's avatar
Luko van der Maas committed
206
207
208
        placeholder = self.context["request"].build_absolute_uri(
            static("members/images/default-avatar.jpg")
        )
209
210
211
212
        file = None
        if instance.photo:
            file = instance.photo
        return create_image_thumbnail_dict(
Luko van der Maas's avatar
Luko van der Maas committed
213
214
            self.context["request"], file, placeholder=placeholder, size_large="800x800"
        )