Commit 1a694377 authored by Luuk Scholten's avatar Luuk Scholten
Browse files

Move photo base64 encoder to the member serializer

parent b01e5491
from django.urls import reverse
from django.contrib.staticfiles.finders import find as find_static_file
from events.api.serializers import CalenderJSSerializer
from members.models import Member
from rest_framework import serializers
from base64 import b64encode
class MemberBirthdaySerializer(CalenderJSSerializer):
......@@ -45,10 +47,20 @@ class MemberBirthdaySerializer(CalenderJSSerializer):
class MemberSerializer(serializers.ModelSerializer):
photo = serializers.CharField(source='b64_photo')
class Meta:
model = Member
fields = ('pk', 'display_name', 'photo')
photo = serializers.SerializerMethodField('_b64_photo')
def _b64_photo(self, instance):
if instance.photo:
photo = ''.join(['data:image/jpeg;base64,',
b64encode(instance.photo.file.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
......@@ -3,7 +3,6 @@ from datetime import date, timedelta
from functools import reduce
from django.conf import settings
from django.contrib.staticfiles.finders import find as find_static_file
from django.core import validators
from django.core.exceptions import ValidationError
from django.db import models
......@@ -18,8 +17,6 @@ from activemembers.models import Committee
from utils.snippets import datetime_to_lectureyear
from utils.validators import validate_file_extension
from base64 import b64encode
class ActiveMemberManager(models.Manager):
"""Get all active members"""
......@@ -379,23 +376,6 @@ class Member(models.Model):
'display name')})
raise ValidationError(errors)
@property
def b64_photo(self):
if self.photo:
photo = ''.join(['data:image/jpeg;base64,',
b64encode(
self.photo.file.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()
])
print(len(photo))
return photo
def __str__(self):
return '{} ({})'.format(self.get_full_name(), self.user.username)
......
Supports Markdown
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