mirror of https://gitlab.crans.org/bde/nk20
Add profile and membership information to OAuth views
Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
This commit is contained in:
parent
b244e01231
commit
72806f0ace
|
@ -4,10 +4,14 @@
|
||||||
|
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from rest_framework.serializers import ModelSerializer
|
from django.utils import timezone
|
||||||
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
from member.api.serializers import ProfileSerializer, MembershipSerializer
|
||||||
|
from note.models import Alias
|
||||||
|
|
||||||
|
|
||||||
class UserSerializer(ModelSerializer):
|
class UserSerializer(serializers.ModelSerializer):
|
||||||
"""
|
"""
|
||||||
REST API Serializer for Users.
|
REST API Serializer for Users.
|
||||||
The djangorestframework plugin will analyse the model `User` and parse all fields in the API.
|
The djangorestframework plugin will analyse the model `User` and parse all fields in the API.
|
||||||
|
@ -22,7 +26,7 @@ class UserSerializer(ModelSerializer):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class ContentTypeSerializer(ModelSerializer):
|
class ContentTypeSerializer(serializers.ModelSerializer):
|
||||||
"""
|
"""
|
||||||
REST API Serializer for Users.
|
REST API Serializer for Users.
|
||||||
The djangorestframework plugin will analyse the model `User` and parse all fields in the API.
|
The djangorestframework plugin will analyse the model `User` and parse all fields in the API.
|
||||||
|
@ -31,3 +35,39 @@ class ContentTypeSerializer(ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = ContentType
|
model = ContentType
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
|
|
||||||
|
|
||||||
|
class OAuthSerializer(serializers.ModelSerializer):
|
||||||
|
"""
|
||||||
|
Informations that are transmitted by OAuth.
|
||||||
|
For now, this includes user, profile and valid memberships.
|
||||||
|
This should be better managed later.
|
||||||
|
"""
|
||||||
|
normalized_name = serializers.SerializerMethodField()
|
||||||
|
|
||||||
|
profile = ProfileSerializer()
|
||||||
|
|
||||||
|
memberships = serializers.SerializerMethodField()
|
||||||
|
|
||||||
|
def get_normalized_name(self, obj):
|
||||||
|
return Alias.normalize(obj.username)
|
||||||
|
|
||||||
|
def get_memberships(self, obj):
|
||||||
|
return serializers.ListSerializer(child=MembershipSerializer()).to_representation(
|
||||||
|
obj.memberships.filter(date_start__lte=timezone.now(), date_end__gte=timezone.now()))
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = User
|
||||||
|
fields = (
|
||||||
|
'id',
|
||||||
|
'username',
|
||||||
|
'normalized_name',
|
||||||
|
'first_name',
|
||||||
|
'last_name',
|
||||||
|
'email',
|
||||||
|
'is_superuser',
|
||||||
|
'is_active',
|
||||||
|
'is_staff',
|
||||||
|
'profile',
|
||||||
|
'memberships',
|
||||||
|
)
|
||||||
|
|
|
@ -4,11 +4,14 @@
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from rest_framework.generics import RetrieveAPIView
|
from rest_framework.generics import RetrieveAPIView
|
||||||
|
|
||||||
from .serializers import UserSerializer
|
from .serializers import OAuthSerializer
|
||||||
|
|
||||||
|
|
||||||
class UserInformationView(RetrieveAPIView):
|
class UserInformationView(RetrieveAPIView):
|
||||||
serializer_class = UserSerializer
|
"""
|
||||||
|
These fields are give to OAuth authenticators.
|
||||||
|
"""
|
||||||
|
serializer_class = OAuthSerializer
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
return User.objects.filter(pk=self.request.user.pk)
|
return User.objects.filter(pk=self.request.user.pk)
|
||||||
|
|
Loading…
Reference in New Issue