Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add renderers and format extension handling #22

Closed
banesullivan opened this issue Apr 15, 2022 · 0 comments · Fixed by #30
Closed

Add renderers and format extension handling #22

banesullivan opened this issue Apr 15, 2022 · 0 comments · Fixed by #30

Comments

@banesullivan
Copy link
Contributor

banesullivan commented Apr 15, 2022

Can we add renderers (see details at bottom) and remove the duplicate _png, _jpeg, and _tif variants of many of the endpoints so that they are one endpoint that accepts different formats? I know this is possible, but I haven't been able to get it working with DRF 😕

For example, we have the thumbnail endpoint split as two endpoints:

  • /image-file/{id}/thumbnail.png
  • /image-file/{id}/thumbnail.jpeg

Can we combine these to: /image-file/{id}/thumbnail.{format}

Thus reducing the method shims for these endpoints like

def thumbnail_png(self, request: Request, pk: int = None) -> HttpResponse:

and

def thumbnail_jpeg(self, request: Request, pk: int = None) -> HttpResponse:

So that we are only using

def thumbnail(self, request: Request, pk: int = None, format: str = None) -> HttpResponse:

with the format argument.

So far I have been unsuccessful in getting this to work. What's annoying is that the default .json and .api "formats" work for these endpoints, but I cannot add any custom formats/renderers like .png, .jpeg, or .tif

from rest_framework.renderers import BaseRenderer as RFBaseRenderer


class BaseRenderer(RFBaseRenderer):
    render_style = 'binary'
    charset = None

    def render(self, data, media_type=None, renderer_context=None):
        return data


class PNGRenderer(BaseRenderer):
    media_type = 'image/png'
    format = 'png'


class JPEGRenderer(BaseRenderer):
    media_type = 'image/jpeg'
    format = 'jpeg'


class JPGRenderer(BaseRenderer):
    media_type = 'image/jpeg'
    format = 'jpg'


image_renderers = [PNGRenderer, JPEGRenderer, JPGRenderer]


class TifRenderer(BaseRenderer):
    media_type = 'image/jpeg'
    format = 'tif'


class TiffRenderer(BaseRenderer):
    media_type = 'image/jpeg'
    format = 'tiff'


image_data_renderers = image_renderers + [TifRenderer, TiffRenderer]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant