# 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 UserDetailSerializer(serializers.ModelSerializer):
group_names: serializers.SlugRelatedField = serializers.SlugRelatedField(
source="groups",
many=True,
read_only=True,
slug_field="name",
)
all_groups = serializers.SerializerMethodField()
[docs]
def get_all_groups(self, obj):
return list(Group.objects.values("id", "name"))
[docs]
class UserSessionSerializer(serializers.ModelSerializer):
[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 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]
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)