from drf_spectacular.utils import extend_schema from rest_framework import serializers from rest_framework.views import APIView, Response from account import selectors, services class GetUsersView(APIView): class OutputSerializer(serializers.Serializer): id = serializers.IntegerField() email = serializers.EmailField() is_staff = serializers.BooleanField() is_active = serializers.BooleanField() class FilterSerializer(serializers.Serializer): name = serializers.CharField(required=False) @extend_schema(responses=OutputSerializer, parameters=[FilterSerializer]) def get(self, request): users = selectors.get_users(self.FilterSerializer(request.query_params).data) return Response(self.OutputSerializer(users, many=True).data) get_users_view = GetUsersView.as_view() class AddUserView(APIView): class InputSerializer(serializers.Serializer): first_name = serializers.CharField() last_name = serializers.CharField() email = serializers.EmailField() is_staff = serializers.BooleanField() institution_id = serializers.CharField() class OutputSerializer(serializers.Serializer): id = serializers.IntegerField() email = serializers.EmailField() is_staff = serializers.BooleanField() is_active = serializers.BooleanField() serializer_class = InputSerializer def post(self, request): serz = self.InputSerializer(data=request.data) serz.is_valid(raise_exception=True) user = services.add_user( first_name=serz.validated_data["first_name"], # type: ignore last_name=serz.validated_data["last_name"], # type: ignore email=serz.validated_data["email"], # type: ignore is_staff=serz.validated_data["is_staff"], # type: ignore institution_id=serz.validated_data["institution_id"], # type: ignore ) return Response(self.OutputSerializer(user).data) add_user_view = AddUserView.as_view()