2024-02-07 01:26:49 +00:00
|
|
|
# Copyright (C) 2018-2024 by BDE ENS Paris-Saclay
|
2020-09-02 17:00:04 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
from django.contrib.auth.models import User
|
2021-03-09 09:57:35 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
from rest_framework import serializers
|
|
|
|
from member.api.serializers import ProfileSerializer, MembershipSerializer
|
2021-12-23 22:25:18 +00:00
|
|
|
from member.models import Membership
|
2021-03-09 11:55:19 +00:00
|
|
|
from note.api.serializers import NoteSerializer
|
2021-03-09 09:57:35 +00:00
|
|
|
from note.models import Alias
|
2021-12-23 22:25:18 +00:00
|
|
|
from note_kfet.middlewares import get_current_request
|
|
|
|
from permission.backends import PermissionBackend
|
2020-09-03 19:21:09 +00:00
|
|
|
|
2021-03-09 09:57:35 +00:00
|
|
|
|
|
|
|
class UserSerializer(serializers.ModelSerializer):
|
2020-09-02 17:00:04 +00:00
|
|
|
"""
|
|
|
|
REST API Serializer for Users.
|
|
|
|
The djangorestframework plugin will analyse the model `User` and parse all fields in the API.
|
|
|
|
"""
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
exclude = (
|
|
|
|
'password',
|
|
|
|
'groups',
|
|
|
|
'user_permissions',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-03-09 09:57:35 +00:00
|
|
|
class ContentTypeSerializer(serializers.ModelSerializer):
|
2020-09-02 17:00:04 +00:00
|
|
|
"""
|
|
|
|
REST API Serializer for Users.
|
|
|
|
The djangorestframework plugin will analyse the model `User` and parse all fields in the API.
|
|
|
|
"""
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = ContentType
|
|
|
|
fields = '__all__'
|
2021-03-09 09:57:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
2021-12-23 22:25:18 +00:00
|
|
|
profile = serializers.SerializerMethodField()
|
2021-03-09 09:57:35 +00:00
|
|
|
|
2021-12-23 22:25:18 +00:00
|
|
|
note = serializers.SerializerMethodField()
|
2021-03-09 11:55:19 +00:00
|
|
|
|
2021-03-09 09:57:35 +00:00
|
|
|
memberships = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
def get_normalized_name(self, obj):
|
|
|
|
return Alias.normalize(obj.username)
|
|
|
|
|
2021-12-23 22:25:18 +00:00
|
|
|
def get_profile(self, obj):
|
|
|
|
# Display the profile of the user only if we have rights to see it.
|
|
|
|
return ProfileSerializer().to_representation(obj.profile) \
|
2022-03-09 10:45:24 +00:00
|
|
|
if PermissionBackend.check_perm(get_current_request(), 'member.view_profile', obj.profile) else None
|
2021-12-23 22:25:18 +00:00
|
|
|
|
|
|
|
def get_note(self, obj):
|
|
|
|
# Display the note of the user only if we have rights to see it.
|
|
|
|
return NoteSerializer().to_representation(obj.note) \
|
2022-03-09 10:45:24 +00:00
|
|
|
if PermissionBackend.check_perm(get_current_request(), 'note.view_note', obj.note) else None
|
2021-12-23 22:25:18 +00:00
|
|
|
|
2021-03-09 09:57:35 +00:00
|
|
|
def get_memberships(self, obj):
|
2021-12-23 22:25:18 +00:00
|
|
|
# Display only memberships that we are allowed to see.
|
2021-03-09 09:57:35 +00:00
|
|
|
return serializers.ListSerializer(child=MembershipSerializer()).to_representation(
|
2021-12-23 22:25:18 +00:00
|
|
|
obj.memberships.filter(date_start__lte=timezone.now(), date_end__gte=timezone.now())
|
|
|
|
.filter(PermissionBackend.filter_queryset(get_current_request(), Membership, 'view')))
|
2021-03-09 09:57:35 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = (
|
|
|
|
'id',
|
|
|
|
'username',
|
|
|
|
'normalized_name',
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
'email',
|
|
|
|
'is_superuser',
|
|
|
|
'is_active',
|
|
|
|
'is_staff',
|
|
|
|
'profile',
|
2021-03-09 11:55:19 +00:00
|
|
|
'note',
|
2021-03-09 09:57:35 +00:00
|
|
|
'memberships',
|
|
|
|
)
|