diff --git a/src/turbo_response/__init__.py b/src/turbo_response/__init__.py index 3a24708..94854b1 100644 --- a/src/turbo_response/__init__.py +++ b/src/turbo_response/__init__.py @@ -1,4 +1,5 @@ -from .constants import Action +from .constants import TURBO_STREAM_MIME_TYPE as TURBO_STREAM_CONTENT_TYPE +from .constants import Action, ResponseFormat from .frame import TurboFrame from .renderers import render_turbo_frame, render_turbo_stream from .response import ( @@ -9,7 +10,7 @@ TurboStreamStreamingResponse, TurboStreamTemplateResponse, ) -from .shortcuts import redirect_303, render_form_response +from .shortcuts import redirect_303, render_form_response, response_format from .stream import TurboStream from .template import render_turbo_frame_template, render_turbo_stream_template from .templatetags.turbo_helper import dom_id @@ -31,4 +32,7 @@ "render_turbo_stream", "render_turbo_stream_template", "dom_id", + "response_format", + "ResponseFormat", + "TURBO_STREAM_CONTENT_TYPE", ] diff --git a/src/turbo_response/constants.py b/src/turbo_response/constants.py index 6eaa788..a8a03bb 100644 --- a/src/turbo_response/constants.py +++ b/src/turbo_response/constants.py @@ -14,3 +14,9 @@ class Action(enum.Enum): TURBO_STREAM_MIME_TYPE = "text/vnd.turbo-stream.html" + + +class ResponseFormat(enum.Enum): + HTML = "html" + JSON = "json" + TurboStream = "turbo_stream" diff --git a/src/turbo_response/middleware.py b/src/turbo_response/middleware.py index c7c7374..372bed8 100644 --- a/src/turbo_response/middleware.py +++ b/src/turbo_response/middleware.py @@ -8,9 +8,7 @@ class TurboData: def __init__(self, request: HttpRequest): - self.has_turbo_header = TURBO_STREAM_MIME_TYPE in request.headers.get( - "Accept", "" - ) + self.has_turbo_header = request.accepts(TURBO_STREAM_MIME_TYPE) self.frame = request.headers.get("Turbo-Frame", None) def __bool__(self): diff --git a/src/turbo_response/shortcuts.py b/src/turbo_response/shortcuts.py index d23109d..fbbacb8 100644 --- a/src/turbo_response/shortcuts.py +++ b/src/turbo_response/shortcuts.py @@ -1,4 +1,5 @@ import http +from contextlib import contextmanager from typing import Dict, List, Optional, Union from django.db.models import Model @@ -7,7 +8,7 @@ from django.shortcuts import resolve_url from django.template.response import TemplateResponse -from .constants import Action +from .constants import TURBO_STREAM_MIME_TYPE, Action, ResponseFormat from .response import HttpResponseSeeOther from .stream import TurboStream @@ -64,3 +65,34 @@ def render_form_response( else http.HTTPStatus.OK, **response_kwargs, ) + + +def get_response_format(request): + """ + Inspired by Rails + + respond_to do |format| + format.turbo_stream { render turbo_stream: turbo_stream_template } + end + """ + if request.accepts(TURBO_STREAM_MIME_TYPE): + return ResponseFormat.TurboStream + elif request.accepts("application/json"): + return ResponseFormat.JSON + else: + return ResponseFormat.HTML + + +@contextmanager +def response_format(request): + """ + Get supported response format from request headers + + html, json, turbo_stream + """ + resp_format = get_response_format(request) + try: + yield resp_format + finally: + # Clean-up code, if needed + pass