13-16) UpdateAPI test 에러
10000자 제한으로 이어 답글 답니다# core/permissions.py from typing import Callable, Type, cast from django.db.models import Model from rest_framework import permissions from rest_framework.request import Request from rest_framework.views import APIView class IsAuthorOrReadonly(permissions.BasePermission): def has_permission(self, request: Request, view: APIView) -> bool: if request.method in permissions.SAFE_METHODS: return True return request.user.is_authenticated # 2차 필터 def has_object_permission( self, request: Request, view: APIView, obj: Model ) -> bool: if request.method in permissions.SAFE_METHODS: return True if not hasattr(obj, "author"): return False return obj.author == request.user def make_drf_permission_class( class_name: str = "PermissionClass", permit_safe_methods: bool = False, has_permission_test_func: Callable[[Request, APIView], bool] = None, has_permission_test_func_name: str = None, has_object_permission_test_func: Callable[[Request, APIView, Model], bool] = None, has_object_permission_test_func_name: str = None, ) -> Type[permissions.BasePermission]: def has_permission(self, request: Request, view: APIView) -> bool: if permit_safe_methods and request.method in permissions.SAFE_METHODS: return True if has_permission_test_func is not None: return has_permission_test_func(request, view) if has_permission_test_func_name is not None: test_func = getattr(view, has_permission_test_func_name) return test_func(request, view) return True def has_object_permission( self, request: Request, view: APIView, obj: Model ) -> bool: if permit_safe_methods and request.method in permissions.SAFE_METHODS: return True if has_object_permission_test_func is not None: return has_object_permission_test_func(request, view, obj) if has_object_permission_test_func_name is not None: test_func = getattr(view, has_object_permission_test_func_name) return test_func(request, view, obj) return True permission_class = type( class_name, (permissions.BasePermission,), { "has_permission": has_permission, "has_object_permission": has_object_permission, }, ) return cast(Type[permissions.BasePermission], permission_class) class TestFuncPermissionMixin: TEST_FUNC_PERMISSION_CLASS_NAME = None @classmethod def get_test_func_permission_instance(cls) -> permissions.BasePermission: permission_class = make_drf_permission_class( class_name=cls.TEST_FUNC_PERMISSION_CLASS_NAME or cls.__name__, has_permission_test_func_name="has_permission", has_object_permission_test_func_name="has_object_permission", ) return permission_class() def get_permissions(self) -> list[permissions.BasePermission]: return super().get_permissions() + [self.get_test_func_permission_instance()] def has_permission(self, request: Request, view: APIView) -> bool: return True def has_object_permission( self, request: Request, view: APIView, obj: Model ) -> bool: return True PermitSafeMethods = make_drf_permission_class( permit_safe_methods=True, )