我们从Python开源项目中,提取了以下13个代码示例,用于说明如何使用rest_framework.exceptions.MethodNotAllowed()。
def http_method_not_allowed(self, request, *args, **kwargs): """ If `request.method` does not correspond to a handler method, determine what kind of exception to raise. """ raise exceptions.MethodNotAllowed(request.method)
def get_required_permissions(self, method, model_cls): """ Given a model and an HTTP method, return the list of permission codes that the user is required to have. """ kwargs = { 'app_label': model_cls._meta.app_label, 'model_name': model_cls._meta.model_name } if method not in self.perms_map: raise exceptions.MethodNotAllowed(method) return [perm % kwargs for perm in self.perms_map[method]]
def get_required_object_permissions(self, method, model_cls): kwargs = { 'app_label': model_cls._meta.app_label, 'model_name': model_cls._meta.model_name } if method not in self.perms_map: raise exceptions.MethodNotAllowed(method) return [perm % kwargs for perm in self.perms_map[method]]
def post(self, request, format='json', *args, **kwargs): #Only JSON accepted for edit/create/delete if not format == 'json': raise MethodNotAllowed('') else: return super(AlertCreateAPI, self).post(request, format, *args, **kwargs)
def post(self, request, format='json', *args, **kwargs): #Only JSON accepted for edit/create/delete if not format == 'json': raise MethodNotAllowed('') else: return super(NoticeCreateAPI, self).post(request, format, *args, **kwargs)
def filter_method(self, request): """ Raise 405 whenever http method is patch """ if request.method == "PATCH": raise MethodNotAllowed("PATCH")
def get_rule(self, request, model_cls, method_map): template = getattr(method_map, request.method) if not template: raise exceptions.MethodNotAllowed(request.method) return template.format( app_label=model_cls._meta.app_label, model_name=model_cls._meta.model_name )
def get_required_object_permissions(self, method, model_cls): kwargs = { 'app_label': model_cls._meta.app_label, 'model_name': model_cls._meta.model_name } if method not in self.perms_map: raise exceptions.MethodNotAllowed(method) return [perm % kwargs for perm in self.object_perms_map[method]]
def confirm(self, request, pk=None): if not settings.AUTH_WORKFLOW: raise MethodNotAllowed('put') profile = get_object_or_404(Profile, pk=pk) profile.confirm(request) return Response(self.get_serializer(profile).data)
def reject(self, request, pk=None): if not settings.AUTH_WORKFLOW: raise MethodNotAllowed('put') profile = get_object_or_404(Profile, pk=pk) profile.reject(request) return Response(self.get_serializer(profile).data)
def activate(self, request, pk=None): if not settings.AUTH_WORKFLOW: raise MethodNotAllowed('put') profile = get_object_or_404(Profile, pk=pk) profile.activate(request) return Response(self.get_serializer(profile).data)
def update(self, instance, validated_data): request = self.get_request_or_die() if not can_edit(request.user, instance, check_api_read_only=False): # This branch is only entered when doing a PATCH request (partial # update), because datalogger validation is done first. raise PermissionDenied(_("Permission to edit Unit denied")) elif instance.api_read_only: raise MethodNotAllowed(request.method, _("Unit is read only")) return super(UnitSerializer, self).update(instance, validated_data)