diff --git a/CHANGES.md b/CHANGES.md index 0fc6674..d743b73 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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. diff --git a/setup.py b/setup.py index 0146a9a..57f1a91 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ # Third Party Libraries from setuptools import setup -version = "0.0.18" +version = "0.0.19" setup( name="django-turbo-response", diff --git a/src/turbo_response/mixins.py b/src/turbo_response/mixins.py index 13c9da4..9279e2c 100644 --- a/src/turbo_response/mixins.py +++ b/src/turbo_response/mixins.py @@ -1,5 +1,6 @@ # Standard Library import http +import warnings from typing import Any, Dict, Iterable, Optional # Django @@ -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 @@ -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") @@ -58,15 +73,37 @@ 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. @@ -74,6 +111,8 @@ def render_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: @@ -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 @@ -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") @@ -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") @@ -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) diff --git a/src/turbo_response/views.py b/src/turbo_response/views.py index 682b0d8..15ac760 100644 --- a/src/turbo_response/views.py +++ b/src/turbo_response/views.py @@ -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): @@ -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): @@ -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 @@ -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): @@ -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)