Commit ad6e2f26 authored by Gijs Hendriksen's avatar Gijs Hendriksen Committed by Luuk Scholten
Browse files

Serializer for display name and avatar

parent 8143e661
......@@ -3,6 +3,8 @@ from django.urls import reverse
from events.api.serializers import CalenderJSSerializer
from members.models import Member
from rest_framework import serializers
class MemberBirthdaySerializer(CalenderJSSerializer):
class Meta(CalenderJSSerializer.Meta):
......@@ -40,3 +42,13 @@ class MemberBirthdaySerializer(CalenderJSSerializer):
def _text_color(self, instance):
return 'white'
class MemberSerializer(serializers.ModelSerializer):
photo = serializers.CharField(source='b64_photo')
class Meta:
model = Member
fields = ('display_name', 'photo')
......@@ -8,7 +8,7 @@ from rest_framework.exceptions import ParseError
from rest_framework.response import Response
from rest_framework import permissions
from members.api.serializers import MemberBirthdaySerializer
from members.api.serializers import MemberBirthdaySerializer, MemberSerializer
from members.models import Member
......@@ -61,3 +61,9 @@ class MemberViewset(viewsets.ViewSet):
serializer = MemberBirthdaySerializer(birthdays, many=True)
return Response(serializer.data)
@list_route()
def info(self, request):
serializer = MemberSerializer(request.user.member)
return Response(serializer.data)
......@@ -3,6 +3,7 @@ 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
......@@ -17,6 +18,8 @@ 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"""
......@@ -376,6 +379,23 @@ 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