Verified Commit c9aa9c00 authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg
Browse files

Add backwards compatible avatar object to events and members API for better thumbnails

parent 029975ca
......@@ -11,6 +11,7 @@ from events.exceptions import RegistrationError
from events.models import Event, Registration, RegistrationInformationField
from pizzas.models import PizzaEvent
from thaliawebsite.settings import settings
from utils.templatetags.thumbnail import thumbnail
class CalenderJSSerializer(serializers.ModelSerializer):
......@@ -198,11 +199,12 @@ class EventListSerializer(serializers.ModelSerializer):
class RegistrationListSerializer(serializers.ModelSerializer):
class Meta:
model = Registration
fields = ('pk', 'member', 'name', 'photo', 'registered_on',
fields = ('pk', 'member', 'name', 'photo', 'avatar', 'registered_on',
'is_late_cancellation', 'is_cancelled', 'queue_position')
name = serializers.SerializerMethodField('_name')
photo = serializers.SerializerMethodField('_photo')
avatar = serializers.SerializerMethodField('_avatar')
member = serializers.SerializerMethodField('_member')
registered_on = serializers.DateTimeField(source='date')
is_cancelled = serializers.SerializerMethodField('_is_cancelled')
......@@ -239,18 +241,39 @@ class RegistrationListSerializer(serializers.ModelSerializer):
return self.context['request'].build_absolute_uri(
static('members/images/default-avatar.jpg'))
def _avatar(self, instance):
placeholder = self.context['request'].build_absolute_uri(
static('members/images/default-avatar.jpg'))
data = {
'full': placeholder,
'small': placeholder,
'medium': placeholder,
'large': placeholder,
}
if instance.member and instance.member.profile.photo:
data['full'] = self.context['request'].build_absolute_uri(
'%s%s' % (settings.MEDIA_URL, instance.member.profile.photo))
data['small'] = self.context['request'].build_absolute_uri(
thumbnail(instance.member.profile.photo, '110x110', 1))
data['medium'] = self.context['request'].build_absolute_uri(
thumbnail(instance.member.profile.photo, '220x220', 1))
data['large'] = self.context['request'].build_absolute_uri(
thumbnail(instance.member.profile.photo, '800x800', 1))
return data
class RegistrationSerializer(serializers.ModelSerializer):
information_fields = None
class Meta:
model = Registration
fields = ('pk', 'member', 'name', 'photo', 'registered_on',
fields = ('pk', 'member', 'name', 'photo', 'avatar', 'registered_on',
'is_late_cancellation', 'is_cancelled',
'queue_position', 'fields')
name = serializers.SerializerMethodField('_name')
photo = serializers.SerializerMethodField('_photo')
avatar = serializers.SerializerMethodField('_avatar')
member = serializers.SerializerMethodField('_member')
registered_on = serializers.DateTimeField(source='date', read_only=True)
is_cancelled = serializers.SerializerMethodField('_is_cancelled')
......@@ -289,6 +312,26 @@ class RegistrationSerializer(serializers.ModelSerializer):
return self.context['request'].build_absolute_uri(
static('members/images/default-avatar.jpg'))
def _avatar(self, instance):
placeholder = self.context['request'].build_absolute_uri(
static('members/images/default-avatar.jpg'))
data = {
'full': placeholder,
'small': placeholder,
'medium': placeholder,
'large': placeholder,
}
if instance.member and instance.member.profile.photo:
data['full'] = self.context['request'].build_absolute_uri(
'%s%s' % (settings.MEDIA_URL, instance.member.profile.photo))
data['small'] = self.context['request'].build_absolute_uri(
thumbnail(instance.member.profile.photo, '110x110', 1))
data['medium'] = self.context['request'].build_absolute_uri(
thumbnail(instance.member.profile.photo, '220x220', 1))
data['large'] = self.context['request'].build_absolute_uri(
thumbnail(instance.member.profile.photo, '800x800', 1))
return data
def __init__(self, instance=None, data=empty, **kwargs):
super().__init__(instance, data, **kwargs)
try:
......
......@@ -9,6 +9,7 @@ from events.api.serializers import CalenderJSSerializer
from members.models import Member
from members.services import member_achievements
from thaliawebsite.settings import settings
from utils.templatetags.thumbnail import thumbnail
class MemberBirthdaySerializer(CalenderJSSerializer):
......@@ -52,9 +53,9 @@ class MemberBirthdaySerializer(CalenderJSSerializer):
class MemberRetrieveSerializer(serializers.ModelSerializer):
class Meta:
model = Member
fields = ('pk', 'display_name', 'photo', 'profile_description',
'birthday', 'starting_year', 'programme',
'website', 'membership_type', 'achievements')
fields = ('pk', 'display_name', 'photo', 'avatar',
'profile_description', 'birthday', 'starting_year',
'programme', 'website', 'membership_type', 'achievements')
display_name = serializers.SerializerMethodField('_display_name')
photo = serializers.SerializerMethodField('_b64_photo')
......@@ -109,14 +110,35 @@ class MemberRetrieveSerializer(serializers.ModelSerializer):
return photo
def _avatar(self, instance):
placeholder = self.context['request'].build_absolute_uri(
static('members/images/default-avatar.jpg'))
data = {
'full': placeholder,
'small': placeholder,
'medium': placeholder,
'large': placeholder,
}
if instance.profile.photo:
data['full'] = self.context['request'].build_absolute_uri(
'%s%s' % (settings.MEDIA_URL, instance.profile.photo))
data['small'] = self.context['request'].build_absolute_uri(
thumbnail(instance.profile.photo, '110x110', 1))
data['medium'] = self.context['request'].build_absolute_uri(
thumbnail(instance.profile.photo, '220x220', 1))
data['large'] = self.context['request'].build_absolute_uri(
thumbnail(instance.profile.photo, '800x800', 1))
return data
class MemberListSerializer(serializers.ModelSerializer):
class Meta:
model = Member
fields = ('pk', 'display_name', 'photo',)
fields = ('pk', 'display_name', 'photo', 'avatar')
display_name = serializers.SerializerMethodField('_display_name')
photo = serializers.SerializerMethodField('_photo')
avatar = serializers.SerializerMethodField('_avatar')
def _display_name(self, instance):
return instance.profile.display_name()
......@@ -124,7 +146,27 @@ class MemberListSerializer(serializers.ModelSerializer):
def _photo(self, instance):
if instance.profile.photo:
return self.context['request'].build_absolute_uri(
'%s%s' % (settings.MEDIA_URL, instance.profile.photo))
thumbnail(instance.profile.photo, '220x220', 1))
else:
return self.context['request'].build_absolute_uri(
static('members/images/default-avatar.jpg'))
def _avatar(self, instance):
placeholder = self.context['request'].build_absolute_uri(
static('members/images/default-avatar.jpg'))
data = {
'full': placeholder,
'small': placeholder,
'medium': placeholder,
'large': placeholder,
}
if instance.profile.photo:
data['full'] = self.context['request'].build_absolute_uri(
'%s%s' % (settings.MEDIA_URL, instance.profile.photo))
data['small'] = self.context['request'].build_absolute_uri(
thumbnail(instance.profile.photo, '110x110', 1))
data['medium'] = self.context['request'].build_absolute_uri(
thumbnail(instance.profile.photo, '220x220', 1))
data['large'] = self.context['request'].build_absolute_uri(
thumbnail(instance.profile.photo, '800x800', 1))
return data
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment