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