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

Use ActiveMemberManager and use avatar urls in members api list

parent 73ba2e4b
...@@ -7,6 +7,7 @@ from rest_framework import serializers ...@@ -7,6 +7,7 @@ from rest_framework import serializers
from events.api.serializers import CalenderJSSerializer from events.api.serializers import CalenderJSSerializer
from members.models import Member from members.models import Member
from members.services import member_achievements from members.services import member_achievements
from thaliawebsite.settings import settings
class MemberBirthdaySerializer(CalenderJSSerializer): class MemberBirthdaySerializer(CalenderJSSerializer):
...@@ -91,16 +92,12 @@ class MemberListSerializer(serializers.ModelSerializer): ...@@ -91,16 +92,12 @@ class MemberListSerializer(serializers.ModelSerializer):
model = Member model = Member
fields = ('pk', 'display_name', 'photo',) fields = ('pk', 'display_name', 'photo',)
photo = serializers.SerializerMethodField('_b64_photo') photo = serializers.SerializerMethodField('_photo')
def _b64_photo(self, instance): def _photo(self, instance):
if instance.photo: if instance.photo:
photo = ''.join(['data:image/jpeg;base64,', return self.context['request'].build_absolute_uri(
b64encode(instance.photo.file.read()).decode()]) '%s%s' % (settings.MEDIA_URL, instance.photo))
else: else:
filename = find_static_file('members/images/default-avatar.jpg') return self.context['request'].build_absolute_uri(
with open(filename, 'rb') as f: 'members/images/default-avatar.jpg')
photo = ''.join(['data:image/jpeg;base64,',
b64encode(f.read()).decode()])
return photo
import copy import copy
from datetime import datetime from datetime import datetime
from django.db.models import Q
from django.utils import timezone from django.utils import timezone
from rest_framework import permissions from rest_framework import permissions
from rest_framework import viewsets, filters from rest_framework import viewsets, filters
...@@ -12,13 +11,13 @@ from rest_framework.response import Response ...@@ -12,13 +11,13 @@ from rest_framework.response import Response
from members.api.serializers import (MemberBirthdaySerializer, from members.api.serializers import (MemberBirthdaySerializer,
MemberRetrieveSerializer, MemberRetrieveSerializer,
MemberListSerializer) MemberListSerializer)
from members.models import Member, Membership from members.models import Member
class MemberViewset(viewsets.ReadOnlyModelViewSet): class MemberViewset(viewsets.ReadOnlyModelViewSet):
queryset = Member.objects.all() queryset = Member.objects.all()
permission_classes = (permissions.IsAuthenticated,) permission_classes = (permissions.IsAuthenticated,)
filter_backends = (filters.OrderingFilter,filters.SearchFilter,) filter_backends = (filters.OrderingFilter, filters.SearchFilter,)
ordering_fields = ('starting_year', 'user__first_name', 'user__last_name') ordering_fields = ('starting_year', 'user__first_name', 'user__last_name')
search_fields = ('nickname', 'user__first_name', search_fields = ('nickname', 'user__first_name',
'user__last_name', 'user__username') 'user__last_name', 'user__username')
...@@ -30,9 +29,7 @@ class MemberViewset(viewsets.ReadOnlyModelViewSet): ...@@ -30,9 +29,7 @@ class MemberViewset(viewsets.ReadOnlyModelViewSet):
def get_queryset(self): def get_queryset(self):
if self.action == 'list': if self.action == 'list':
memberships = Membership.objects.filter( return Member.active_members
Q(until__gt=datetime.now()) | Q(until=None))
return Member.objects.filter(user__in=memberships.values('user'))
return Member.objects.all() return Member.objects.all()
def _get_birthdays(self, member, start, end): def _get_birthdays(self, member, start, end):
...@@ -66,8 +63,7 @@ class MemberViewset(viewsets.ReadOnlyModelViewSet): ...@@ -66,8 +63,7 @@ class MemberViewset(viewsets.ReadOnlyModelViewSet):
raise ParseError(detail='start or end query parameters invalid') raise ParseError(detail='start or end query parameters invalid')
queryset = ( queryset = (
Member Member.active_members
.active_members
.with_birthdays_in_range(start, end) .with_birthdays_in_range(start, end)
.filter(show_birthday=True) .filter(show_birthday=True)
) )
......
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