Source code for nlpmed_portal.management.api.serializers

# SPDX-FileCopyrightText: Copyright (C) 2026 Omid Jafari <omidjafari.com>
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from allauth.usersessions.models import UserSession
from django.contrib.auth.models import Group
from django.contrib.auth.models import Permission
from rest_framework import serializers

from nlpmed_portal.management.constants import PERMISSION_CODENAMES
from nlpmed_portal.management.models import Feedback
from nlpmed_portal.users.models import User


[docs] class UserListSerializer(serializers.ModelSerializer): group_names: serializers.SlugRelatedField = serializers.SlugRelatedField( source="groups", many=True, read_only=True, slug_field="name", )
[docs] class Meta: model = User fields = [ "pk", "is_active", "username", "email", "name", "groups", "group_names", ] read_only_fields = fields
[docs] class UserDetailSerializer(serializers.ModelSerializer): group_names: serializers.SlugRelatedField = serializers.SlugRelatedField( source="groups", many=True, read_only=True, slug_field="name", ) all_groups = serializers.SerializerMethodField()
[docs] class Meta: model = User fields = [ "pk", "is_active", "username", "email", "name", "groups", "group_names", "all_groups", ] read_only_fields = ["pk", "username", "email", "group_names"]
[docs] def get_all_groups(self, obj): return list(Group.objects.values("id", "name"))
[docs] class UserSessionSerializer(serializers.ModelSerializer):
[docs] class Meta: model = UserSession fields = ["created_at", "ip", "user_agent", "last_seen_at"]
[docs] class GroupListSerializer(serializers.ModelSerializer): permissions = serializers.PrimaryKeyRelatedField( queryset=Permission.objects.filter( content_type__app_label__in=["management", "nlp", "annotations"], codename__in=PERMISSION_CODENAMES, ), many=True, ) permissions_display: serializers.StringRelatedField = serializers.StringRelatedField( source="permissions", many=True, read_only=True, )
[docs] class Meta: model = Group fields = ["pk", "name", "permissions", "permissions_display"] read_only_fields = fields
[docs] class GroupDetailSerializer(serializers.ModelSerializer): permissions = serializers.PrimaryKeyRelatedField( queryset=Permission.objects.filter( content_type__app_label__in=["management", "nlp", "annotations"], codename__in=PERMISSION_CODENAMES, ), many=True, ) permissions_display: serializers.StringRelatedField = serializers.StringRelatedField( source="permissions", many=True, read_only=True, ) all_permissions = serializers.SerializerMethodField()
[docs] class Meta: model = Group fields = ["pk", "name", "permissions", "permissions_display", "all_permissions"] read_only_fields = ["pk", "permissions_display", "all_permissions"]
[docs] def get_all_permissions(self, obj): return self.context["all_perms"]
[docs] class FeedbackSerializer(serializers.ModelSerializer): user = serializers.HiddenField(default=serializers.CurrentUserDefault()) username = serializers.CharField(source="user.username", read_only=True)
[docs] class Meta: model = Feedback fields = "__all__" read_only_fields = ["id", "created_at", "username"]