Skip to content

Commit

Permalink
Tidyup mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
danjac committed Jan 13, 2021
1 parent 4943802 commit 6a6eeac
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Latest

Changes to mixin APIs.

## [0.0.18] - 2021-1-12

**TurboStreamDeleteView** automatically resolves target based on model name+PK.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Third Party Libraries
from setuptools import setup

version = "0.0.18"
version = "0.0.19"

setup(
name="django-turbo-response",
Expand Down
115 changes: 98 additions & 17 deletions src/turbo_response/mixins.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Standard Library
import http
import warnings
from typing import Any, Dict, Iterable, Optional

# Django
Expand All @@ -18,9 +19,7 @@
)


class TurboStreamResponseMixin:
"""Mixin to handle turbo-stream responses"""

class TurboStreamArgsMixin:
turbo_stream_action: Optional[Action] = None
turbo_stream_target: Optional[str] = None

Expand All @@ -38,13 +37,29 @@ def get_turbo_stream_target(self) -> Optional[str]:
"""
return self.turbo_stream_target


class TurboFrameArgsMixin:
turbo_frame_dom_id = None

def get_turbo_frame_dom_id(self) -> Optional[str]:
"""Should return a valid DOM ID target for the turbo frame."""
return self.turbo_frame_dom_id


class TurboStreamResponseMixin(TurboStreamArgsMixin):
"""Handles turbo-stream responses"""

def get_response_content(self) -> str:
"""Returns turbo-stream content."""

return ""

def render_turbo_stream_response(self, **response_kwargs) -> TurboStreamResponse:
"""Returns a turbo-stream response."""
def render_turbo_stream(self, **response_kwargs) -> TurboStreamResponse:
"""Returns a turbo-stream response.
.. deprecated:: 0.0.19
Use :func:`render_turbo_stream` instead.
"""
if (target := self.get_turbo_stream_target()) is None:
raise ImproperlyConfigured("target is None")

Expand All @@ -58,22 +73,46 @@ def render_turbo_stream_response(self, **response_kwargs) -> TurboStreamResponse
**response_kwargs,
)

def render_turbo_stream_response(self, **response_kwargs) -> TurboStreamResponse:
"""Returns a turbo-stream response.
.. deprecated:: 0.0.19
Use :func:`render_turbo_stream` instead.
"""
warnings.warn(
f"use {self.__class__.__name__}.render_turbo_stream() instead",
DeprecationWarning,
)

return self.render_turbo_stream(**response_kwargs)


class TurboStreamTemplateResponseMixin(TurboStreamResponseMixin):
class TurboStreamTemplateResponseMixin(TurboStreamArgsMixin):
"""Handles turbo-stream template responses."""

def get_turbo_stream_template_names(self) -> Iterable[str]:
"""Returns list of template names."""
"""Returns list of template names.
.. deprecated:: 0.0.19
Will use :func:`get_template_names()`
"""
warnings.warn(
f"use {self.__class__.__name__}.get_template_names() instead",
PendingDeprecationWarning,
)

return self.get_template_names()

def render_turbo_stream_template_response(
def render_turbo_stream(
self, context: Dict[str, Any], **response_kwargs
) -> TurboStreamTemplateResponse:
"""Renders a turbo-stream template response.
:param context: template context
:type context: dict
.. deprecated:: 0.0.19
Use :func:`render_turbo_stream` instead.
"""

if (target := self.get_turbo_stream_target()) is None:
Expand All @@ -91,6 +130,23 @@ def render_turbo_stream_template_response(
using=self.template_engine,
)

def render_turbo_stream_template_response(
self, context: Dict[str, Any], **response_kwargs
) -> TurboStreamTemplateResponse:
"""Renders a turbo-stream template response.
:param context: template context
:type context: dict
.. deprecated:: 0.0.19
Use :func:`render_turbo_stream` instead.
"""
warnings.warn(
f"use {self.__class__.__name__}.render_turbo_stream instead",
DeprecationWarning,
)
return self.render_turbo_stream(context, **response_kwargs)


class TurboFormMixin:
"""Mixin for handling form validation. Ensures response
Expand All @@ -113,16 +169,14 @@ def form_valid(self, form):
return super().form_valid(form)


class TurboFrameResponseMixin:
turbo_frame_dom_id = None

def get_turbo_frame_dom_id(self) -> Optional[str]:
return self.turbo_frame_dom_id
class TurboFrameResponseMixin(TurboFrameArgsMixin):
"""Renders turbo-frame responses"""

def get_response_content(self) -> str:
return ""

def render_turbo_frame_response(self, **response_kwargs) -> TurboFrameResponse:
def render_turbo_frame(self, **response_kwargs) -> TurboFrameResponse:
"""Renders a turbo frame to response."""

if (dom_id := self.get_turbo_frame_dom_id()) is None:
raise ValueError("dom_id must be specified")
Expand All @@ -131,17 +185,28 @@ def render_turbo_frame_response(self, **response_kwargs) -> TurboFrameResponse:
content=self.get_response_content(), dom_id=dom_id, **response_kwargs,
)

def render_turbo_frame_response(self, **response_kwargs) -> TurboFrameResponse:
"""Renders a turbo frame to response.
.. deprecated:: 0.0.19
Use :func:`render_turbo_frame` instead.
"""
warnings.warn(
f"use {self.__class__.__name__}.render_turbo_frame instead",
DeprecationWarning,
)
return self.render_turbo_frame(**response_kwargs)


class TurboFrameTemplateResponseMixin(TurboFrameResponseMixin):
class TurboFrameTemplateResponseMixin(TurboFrameArgsMixin):
"""Handles turbo-frame template responses."""

def render_turbo_frame_template_response(
def render_turbo_frame(
self, context: Dict[str, Any], **response_kwargs
) -> TurboFrameTemplateResponse:
"""Returns a turbo-frame response.
:param context: template context
"""
if (dom_id := self.get_turbo_frame_dom_id()) is None:
raise ValueError("dom_id must be specified")
Expand All @@ -154,3 +219,19 @@ def render_turbo_frame_template_response(
using=self.template_engine,
**response_kwargs,
)

def render_turbo_frame_template_response(
self, context: Dict[str, Any], **response_kwargs
) -> TurboFrameTemplateResponse:
"""Returns a turbo-frame response.
:param context: template context
.. deprecated:: 0.0.19
Use :func:`render_turbo_frame` instead.
"""
warnings.warn(
f"use {self.__class__.__name__}.render_turbo_frame instead",
DeprecationWarning,
)
return self.render_turbo_frame(context, **response_kwargs)
10 changes: 5 additions & 5 deletions src/turbo_response/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TurboStreamView(TurboStreamResponseMixin, View):
"""Renders a simple turbo-stream view"""

def dispatch(self, *args, **kwargs) -> HttpResponse:
return self.render_turbo_stream_response()
return self.render_turbo_stream()


class TurboStreamTemplateView(TurboStreamTemplateResponseMixin, TemplateView):
Expand All @@ -37,7 +37,7 @@ class TurboStreamTemplateView(TurboStreamTemplateResponseMixin, TemplateView):
def render_to_response(
self, context: Dict[str, Any], **response_kwargs
) -> HttpResponse:
return self.render_turbo_stream_template_response(context, **response_kwargs)
return self.render_turbo_stream(context, **response_kwargs)


class TurboFormView(TurboFormMixin, FormView):
Expand Down Expand Up @@ -67,7 +67,7 @@ def delete(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
# deleted as ID will be None. So we need to get the response first
# in order to resolve the target ID.
self.object = self.get_object()
response = self.render_turbo_stream_response()
response = self.render_turbo_stream()
self.object.delete()
return response

Expand All @@ -76,7 +76,7 @@ class TurboFrameView(TurboFrameResponseMixin, View):
"""Retuns a simple turbo-frame response."""

def dispatch(self, *args, **kwargs) -> HttpResponse:
return self.render_turbo_frame_response()
return self.render_turbo_frame()


class TurboFrameTemplateView(TurboFrameTemplateResponseMixin, TemplateView):
Expand All @@ -85,4 +85,4 @@ class TurboFrameTemplateView(TurboFrameTemplateResponseMixin, TemplateView):
def render_to_response(
self, context: Dict[str, Any], **response_kwargs
) -> HttpResponse:
return self.render_turbo_frame_template_response(context, **response_kwargs)
return self.render_turbo_frame(context, **response_kwargs)

0 comments on commit 6a6eeac

Please sign in to comment.