Birthday API and some more enhacements

This commit is contained in:
Antoine Bartuccio 2016-08-07 22:57:38 +02:00
parent 8e8ee808b5
commit bfa966e3d8
2 changed files with 21 additions and 5 deletions

View File

@ -1,3 +1,5 @@
import datetime
from django.shortcuts import get_object_or_404
from rest_framework.response import Response
from rest_framework.decorators import api_view
@ -7,7 +9,7 @@ from rest_framework.decorators import list_route
from core.templatetags.renderer import markdown
from counter.models import Counter
from core.models import User, Group
from core.models import User, RealGroup
from club.models import Club
from api.views import serializers
from api.views import RightManagedModelViewSet
@ -42,10 +44,21 @@ class CounterViewSet(RightManagedModelViewSet):
class UserViewSet(RightManagedModelViewSet):
"""
Manage Users (api/v1/user/)
Only show active users
"""
serializer_class = serializers.UserRead
queryset = User.objects.all()
queryset = User.objects.filter(is_active=True)
@list_route()
def birthday(self, request):
"""
Return all users born today (api/v1/user/birstdays)
"""
date = datetime.datetime.today()
self.queryset = self.queryset.filter(date_of_birth=date)
serializer = self.get_serializer(self.queryset, many=True)
return Response(serializer.data)
class ClubViewSet(RightManagedModelViewSet):
@ -62,4 +75,4 @@ class GroupViewSet(RightManagedModelViewSet):
"""
serializer_class = serializers.GroupRead
queryset = Group.objects.all()
queryset = RealGroup.objects.all()

View File

@ -1,6 +1,6 @@
from rest_framework import serializers
from counter.models import Counter
from core.models import User, Group
from core.models import User, RealGroup
from club.models import Club
@ -20,15 +20,18 @@ class UserRead(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'first_name', 'last_name', 'email',
'date_of_birth', 'nick_name', 'is_active', 'date_joined')
class ClubRead(serializers.ModelSerializer):
class Meta:
model = Club
fields = ('id', 'name', 'unix_name', 'address', 'members')
class GroupRead(serializers.ModelSerializer):
class Meta:
model = Group
model = RealGroup