serializers.py 6.15 KB
Newer Older
1
from django.templatetags.static import static
2
3
from django.urls import reverse
from rest_framework import serializers
4
5

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

10
11
12
13
14
15

class MemberBirthdaySerializer(CalenderJSSerializer):
    class Meta(CalenderJSSerializer.Meta):
        model = Member

    def _start(self, instance):
16
        return instance.profile.birthday
17
18
19
20
21
22
23
24
25
26
27

    def _end(self, instance):
        pass

    def _all_day(self, instance):
        return True

    def _is_birthday(self, instance):
        return True

    def _url(self, instance):
28
        return reverse('members:profile', kwargs={'pk': instance.pk})
29
30

    def _title(self, instance):
31
        return instance.profile.display_name()
32
33
34
35

    def _description(self, instance):
        membership = instance.current_membership
        if membership and membership.type == 'honorary':
Thom Wiggers's avatar
Thom Wiggers committed
36
            return membership.get_type_display()
37
38
39
40
41
42
43
44
45
46
        return ''

    def _background_color(self, instance):
        membership = instance.current_membership
        if membership and membership.type == 'honorary':
            return '#E62272'
        return 'black'

    def _text_color(self, instance):
        return 'white'
47
48


49
class ProfileRetrieveSerializer(serializers.ModelSerializer):
50
    class Meta:
51
        model = Profile
52
53
54
        fields = ('pk', 'display_name', 'avatar', 'profile_description',
                  'birthday', 'starting_year', 'programme', 'website',
                  'membership_type', 'achievements', 'societies')
55

56
    pk = serializers.SerializerMethodField('_pk')
57
    avatar = serializers.SerializerMethodField('_avatar')
58
59
60
    birthday = serializers.SerializerMethodField('_birthday')
    membership_type = serializers.SerializerMethodField('_membership_type')
    achievements = serializers.SerializerMethodField('_achievements')
61
    societies = serializers.SerializerMethodField('_societies')
62

63
64
    def _pk(self, instance):
        return instance.user.pk
65

66
    def _birthday(self, instance):
67
68
        if instance.show_birthday:
            return instance.birthday
69
70
71
        return None

    def _membership_type(self, instance):
72
        membership = instance.user.current_membership
73
74
75
76
77
        if membership:
            return membership.type
        return None

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

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

83
84
85
    def _avatar(self, instance):
        placeholder = self.context['request'].build_absolute_uri(
                static('members/images/default-avatar.jpg'))
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
86
        file = None
87
88
        if instance.photo:
            file = instance.photo
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
89
90
91
        return create_image_thumbnail_dict(
            self.context['request'], file, placeholder=placeholder,
            size_large='800x800')
92

93
94

class MemberListSerializer(serializers.ModelSerializer):
95
96
    class Meta:
        model = Member
97
        fields = ('pk', 'display_name', 'avatar')
98

99
    display_name = serializers.SerializerMethodField('_display_name')
100
    avatar = serializers.SerializerMethodField('_avatar')
101

102
103
104
    def _display_name(self, instance):
        return instance.profile.display_name()

105
106
    def _avatar(self, instance):
        placeholder = self.context['request'].build_absolute_uri(
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
107
108
            static('members/images/default-avatar.jpg'))
        file = None
109
        if instance.profile.photo:
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
110
111
112
113
            file = instance.profile.photo
        return create_image_thumbnail_dict(
            self.context['request'], file, placeholder=placeholder,
            size_large='800x800')
114
115


116
class ProfileEditSerializer(serializers.ModelSerializer):
117
118
    class Meta:
        model = Profile
119
        fields = ('pk', 'email', 'first_name', 'last_name', 'address_street',
120
                  'address_street2', 'address_postal_code', 'address_city',
121
122
123
124
                  'address_country', 'phone_number', 'show_birthday',
                  'website', 'photo', 'emergency_contact',
                  'emergency_contact_phone_number', 'profile_description',
                  'nickname', 'display_name_preference',
125
126
127
                  'language', 'receive_optin', 'receive_newsletter',
                  'display_name', 'avatar', 'birthday', 'starting_year',
                  'programme', 'membership_type', 'achievements', 'societies')
128

129
130
131
132
        read_only_fields = ('display_name', 'starting_year', 'programme',
                            'birthday')

    pk = serializers.SerializerMethodField('_pk')
133
134
135
    email = serializers.SerializerMethodField('_email')
    first_name = serializers.SerializerMethodField('_first_name')
    last_name = serializers.SerializerMethodField('_last_name')
136
137
138
139
140
141
142
    avatar = serializers.SerializerMethodField('_avatar')
    membership_type = serializers.SerializerMethodField('_membership_type')
    achievements = serializers.SerializerMethodField('_achievements')
    societies = serializers.SerializerMethodField('_societies')

    def _pk(self, instance):
        return instance.user.pk
143
144
145
146
147
148
149
150
151
152

    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

153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
    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):
        placeholder = self.context['request'].build_absolute_uri(
                static('members/images/default-avatar.jpg'))
        file = None
        if instance.photo:
            file = instance.photo
        return create_image_thumbnail_dict(
            self.context['request'], file, placeholder=placeholder,
            size_large='800x800')

175

176
177
178
179
class SentryIdentitySerializer(serializers.ModelSerializer):
    class Meta:
        model = Member
        fields = ('pk', 'first_name', 'last_name', 'email', 'is_superuser')