Skip to content

Commit

Permalink
Feature/22 turbo stream response (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-yin authored Jan 10, 2024
1 parent 6ec6ddf commit c98a86e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/turbo_response/__init__.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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
Expand All @@ -31,4 +32,7 @@
"render_turbo_stream",
"render_turbo_stream_template",
"dom_id",
"response_format",
"ResponseFormat",
"TURBO_STREAM_CONTENT_TYPE",
]
6 changes: 6 additions & 0 deletions src/turbo_response/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 1 addition & 3 deletions src/turbo_response/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
34 changes: 33 additions & 1 deletion src/turbo_response/shortcuts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import http
from contextlib import contextmanager
from typing import Dict, List, Optional, Union

from django.db.models import Model
Expand All @@ -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

Expand Down Expand Up @@ -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

0 comments on commit c98a86e

Please sign in to comment.