From 64f427dedf0edf47c402228e710719290df941cb Mon Sep 17 00:00:00 2001 From: Lubos Mjachky Date: Tue, 19 Mar 2024 18:07:30 +0100 Subject: [PATCH] Move the exception handler to registry_api.py The exception handler should be closer to APIView classes where it is actually being used. At the same time, this resolves the circular import errors caused by the recent extraction of the get_content_data method. closes #1561 --- CHANGES/1561.bugfix | 1 + pulp_container/app/exceptions.py | 17 +---------------- pulp_container/app/registry_api.py | 13 +++++++++++-- 3 files changed, 13 insertions(+), 18 deletions(-) create mode 100644 CHANGES/1561.bugfix diff --git a/CHANGES/1561.bugfix b/CHANGES/1561.bugfix new file mode 100644 index 000000000..f27cc207e --- /dev/null +++ b/CHANGES/1561.bugfix @@ -0,0 +1 @@ +Resolved circular import errors raised when using pulp-container as a library. diff --git a/pulp_container/app/exceptions.py b/pulp_container/app/exceptions.py index 634b610b6..8b2c998ed 100644 --- a/pulp_container/app/exceptions.py +++ b/pulp_container/app/exceptions.py @@ -1,19 +1,4 @@ -from rest_framework import status, views -from rest_framework.exceptions import ( - AuthenticationFailed, - NotAuthenticated, - NotFound, - ParseError, -) - - -def unauthorized_exception_handler(exc, context): - response = views.exception_handler(exc, context) - - if isinstance(exc, (AuthenticationFailed, NotAuthenticated)): - response.status_code = status.HTTP_401_UNAUTHORIZED - - return response +from rest_framework.exceptions import NotFound, ParseError class RepositoryNotFound(NotFound): diff --git a/pulp_container/app/registry_api.py b/pulp_container/app/registry_api.py index 3d617c0bb..365aa7a0f 100644 --- a/pulp_container/app/registry_api.py +++ b/pulp_container/app/registry_api.py @@ -43,7 +43,8 @@ from rest_framework.serializers import ModelSerializer from rest_framework.settings import api_settings from rest_framework.viewsets import ViewSet -from rest_framework.views import APIView +from rest_framework.views import APIView, exception_handler +from rest_framework.status import HTTP_401_UNAUTHORIZED from pulp_container.app import models, serializers from pulp_container.app.authorization import AuthorizationService @@ -53,7 +54,6 @@ RegistryApiCache, ) from pulp_container.app.exceptions import ( - unauthorized_exception_handler, InvalidRequest, RepositoryNotFound, RepositoryInvalid, @@ -417,6 +417,15 @@ def get_exception_handler(self): return unauthorized_exception_handler +def unauthorized_exception_handler(exc, context): + response = exception_handler(exc, context) + + if isinstance(exc, (AuthenticationFailed, NotAuthenticated)): + response.status_code = HTTP_401_UNAUTHORIZED + + return response + + class VersionView(ContainerRegistryApiMixin, APIView): """ Handles requests to the /v2/ endpoint.