def put(self, request, pk, format=None):
     user_note = self.get_object(pk)
     serializer = UserNoteSerializer(user_note, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response({'error': serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
class UserNoteDetail(APIView):
    """
    Retrieve, update or delete a casting request of user note.
    """
    def get_object(self, pk):
        try:
            return UserNote.objects.get(pk=pk)
        except UserNote.DoesNotExist:
            raise Http404

    @swagger_auto_schema(responses={200: UserNoteDetailSerializer(many=False)})
    def get(self, request, pk, format=None):
        user_note = self.get_object(pk)
        serializer = UserNoteDetailSerializer(user_note)
        return Response(serializer.data)

    @swagger_auto_schema(
        request_body=UserNoteCreateSerializer(many=True),
        responses={200: UserNoteSerializer(many=False)}
    )
    def put(self, request, pk, format=None):
        user_note = self.get_object(pk)
        serializer = UserNoteSerializer(user_note, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response({'error': serializer.errors}, status=status.HTTP_400_BAD_REQUEST)

    @swagger_auto_schema(responses={200: 'OK'})
    def delete(self, request, pk, format=None):
        user_note = self.get_object(pk)
        user_note.delete()
        return Response({'id': int(pk)}, status=status.HTTP_200_OK)
class UserNoteList(APIView):
    """
    Retrieve all notes of user.
    """
    @swagger_auto_schema(responses={200: UserNoteSerializer(many=True)})
    def get(self, request, format=None):
        user = User.objects.get(pk=request.user.pk)
        user_notes = UserNote.objects.all()
        serializer = UserNoteSerializer(user_notes, many=True)
        return Response(serializer.data)
    def post(self, request, format=None):
        admin = self.get_object(request.user)
        serializer = UserNoteSearchSerializer(data=request.data)
        if serializer.is_valid():
            data = serializer.validated_data
            user_notes = UserNote.objects.all()
            if 'creator' in request.data:
                user_notes = user_notes.filter(creator=data['creator'])
            if 'receiver' in data:
                user_notes = user_notes.filter(receiver_id=data['receiver'])
            if 'actor' in data:
                user_notes = user_notes.filter(receiver_id=data['actor'])
            if 'note_types' in data:
                user_notes = user_notes.filter(Q(note_type__in=data['note_types']))
            if 'note' in data:
                user_notes = user_notes.filter(note__icontains=data['note'])

            new_serializer = UserNoteSerializer(user_notes, many=True)
            return Response(new_serializer.data, status=status.HTTP_200_OK)
        
        return Response({'error': serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
 def get(self, request, format=None):
     user = User.objects.get(pk=request.user.pk)
     user_notes = UserNote.objects.all()
     serializer = UserNoteSerializer(user_notes, many=True)
     return Response(serializer.data)