Skip to content
Snippets Groups Projects
Verified Commit 72806f0a authored by ynerant's avatar ynerant
Browse files

Add profile and membership information to OAuth views


Signed-off-by: ynerant's avatarYohann D'ANELLO <ynerant@crans.org>
parent b244e012
No related branches found
No related tags found
3 merge requests!162Bugs mineurs, documentation,!157[Invoices] Product quantities can be floating,!155CAS + OAuth2
Pipeline #8888 passed with warnings with stages
in 9 minutes and 39 seconds
...@@ -4,10 +4,14 @@ ...@@ -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): ...@@ -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): ...@@ -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 @@ ...@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment