Skip to content

Commit

Permalink
Removed protocol classes
Browse files Browse the repository at this point in the history
  • Loading branch information
danjac committed Jan 7, 2021
1 parent db31fe2 commit 0205e62
Showing 1 changed file with 8 additions and 44 deletions.
52 changes: 8 additions & 44 deletions src/turbo_response/mixins.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
# Standard Library
import http
from typing import TYPE_CHECKING, Any, Dict, Iterable, Optional
from typing import Any, Dict, Iterable, Optional

# Django
from django import forms
from django.core.exceptions import ImproperlyConfigured
from django.http import HttpRequest, HttpResponse
from django.template.engine import Engine

# Third Party Libraries
from typing_extensions import Protocol
from django.http import HttpResponse

# Local
from .renderers import Action
Expand All @@ -21,28 +17,6 @@
)


class SupportsTemplateMixin(Protocol):
request: HttpRequest
template_engine: Engine

def get_template_names(self) -> Iterable[str]:
...


class SupportsFormMixin(Protocol):
def form_invalid(self, form: forms.Form) -> HttpResponse:
...


if TYPE_CHECKING:
TemplateMixinBase = SupportsTemplateMixin
FormMixinBase = SupportsFormMixin

else:
TemplateMixinBase = object
FormMixinBase = object


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

Expand All @@ -64,16 +38,12 @@ def get_turbo_stream_target(self) -> Optional[str]:
return self.turbo_stream_target

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

return ""

def render_turbo_stream_response(self, **response_kwargs) -> TurboStreamResponse:
"""Returns a turbo-stream response.
"""
"""Returns a turbo-stream response."""
if (target := self.get_turbo_stream_target()) is None:
raise ImproperlyConfigured("target is None")

Expand All @@ -88,15 +58,11 @@ def render_turbo_stream_response(self, **response_kwargs) -> TurboStreamResponse
)


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

def get_turbo_stream_template_names(self) -> Iterable[str]:
"""Returns list of template names.
"""
"""Returns list of template names."""
return self.get_template_names()

def render_turbo_stream_template_response(
Expand Down Expand Up @@ -125,7 +91,7 @@ def render_turbo_stream_template_response(
)


class TurboFormMixin(FormMixinBase):
class TurboFormMixin:
"""Mixin for handling form validation. Ensures response
has 422 status on invalid"""

Expand Down Expand Up @@ -154,9 +120,7 @@ def render_turbo_frame_response(self, **response_kwargs) -> TurboFrameResponse:
)


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

def render_turbo_frame_template_response(
Expand Down

0 comments on commit 0205e62

Please sign in to comment.