33
44from aiohttp .client_exceptions import ClientError
55from rest_framework .viewsets import ViewSet
6- from rest_framework .renderers import JSONRenderer , TemplateHTMLRenderer
6+ from rest_framework .renderers import BrowsableAPIRenderer , JSONRenderer , TemplateHTMLRenderer
77from rest_framework .response import Response
88from django .core .exceptions import ObjectDoesNotExist
99from django .shortcuts import redirect
1818 HttpResponseBadRequest ,
1919 StreamingHttpResponse ,
2020 HttpResponse ,
21- JsonResponse ,
2221)
2322from drf_spectacular .utils import extend_schema
2423from dynaconf import settings
@@ -263,32 +262,34 @@ class SimpleView(PackageUploadMixin, ViewSet):
263262 ],
264263 }
265264
266- renderer_classes = [
267- TemplateHTMLRenderer ,
268- PyPISimpleHTMLRenderer ,
269- PyPISimpleJSONRenderer ,
270- ]
265+ def get_renderers (self ):
266+ """Defines custom renderers."""
267+ if self .action in ["list" , "retrieve" ]:
268+ # Use PyPI renderers for simple API endpoints
269+ return [
270+ TemplateHTMLRenderer (),
271+ PyPISimpleHTMLRenderer (),
272+ PyPISimpleJSONRenderer (),
273+ ]
274+ else :
275+ # Use standard DRF renderers
276+ return [JSONRenderer (), BrowsableAPIRenderer ()]
271277
272278 @extend_schema (summary = "Get index simple page" )
273279 def list (self , request , path ):
274280 """Gets the simple api html page for the index."""
275- content_type = _select_content_type (request )
276- if content_type is None :
277- return HttpResponse ("Not Acceptable Content-Type" , status = 406 )
278-
279281 repo_version , content = self .get_rvc ()
280282 if self .should_redirect (repo_version = repo_version ):
281283 return redirect (urljoin (self .base_content_url , f"{ path } /simple/" ))
282- names = content .order_by ("name" ).values_list ("name" , flat = True ).distinct ().iterator ()
283284
284- if content_type == PYPI_SIMPLE_V1_JSON :
285- names_list = list (names )
286- data_dict = write_simple_index_json (names_list )
285+ names = content .order_by ("name" ).values_list ("name" , flat = True ).distinct ()
286+
287+ if request .accepted_renderer .media_type == PYPI_SIMPLE_V1_JSON :
288+ data_dict = write_simple_index_json (list (names ))
287289 headers = {"X-PyPI-Last-Serial" : str (PYPI_SERIAL_CONSTANT )}
288- response = JsonResponse (data_dict , content_type = content_type , headers = headers )
289- return response
290+ return Response (data_dict , headers = headers )
290291 else :
291- return StreamingHttpResponse (write_simple_index (names , streamed = True ))
292+ return StreamingHttpResponse (write_simple_index (names . iterator () , streamed = True ))
292293
293294 def pull_through_package_simple (self , package , path , remote ):
294295 """Gets the package's simple page from remote."""
@@ -327,10 +328,6 @@ def parse_package(release_package):
327328 @extend_schema (operation_id = "pypi_simple_package_read" , summary = "Get package simple page" )
328329 def retrieve (self , request , path , package ):
329330 """Retrieves the simple api html page for a package."""
330- content_type = _select_content_type (request )
331- if content_type is None :
332- return HttpResponse ("Not Acceptable Content-Type" , status = 406 )
333-
334331 repo_ver , content = self .get_rvc ()
335332 # Should I redirect if the normalized name is different?
336333 normalized = canonicalize_name (package )
@@ -352,12 +349,11 @@ def retrieve(self, request, path, package):
352349 name = present [2 ]
353350 releases = ((f , urljoin (self .base_content_url , f"{ path } /{ f } " ), d ) for f , d , _ in packages )
354351
355- if content_type == PYPI_SIMPLE_V1_JSON :
352+ if request . accepted_renderer . media_type == PYPI_SIMPLE_V1_JSON :
356353 releases_list = list (releases )
357354 data_dict = write_simple_detail_json (name , releases_list )
358355 headers = {"X-PyPI-Last-Serial" : str (PYPI_SERIAL_CONSTANT )}
359- response = JsonResponse (data_dict , content_type = content_type , headers = headers )
360- return response
356+ return Response (data_dict , headers = headers )
361357 else :
362358 return StreamingHttpResponse (write_simple_detail (name , releases , streamed = True ))
363359
0 commit comments