Skip to content

Commit

Permalink
Docstrings updated
Browse files Browse the repository at this point in the history
  • Loading branch information
danjac committed Dec 30, 2020
1 parent 041f138 commit b9bdf88
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [0.0.10] - 2020-12-30

Remove __str__ methods from TurboStream and TurboFrame classes

## [0.0.9] - 2020-12-30

Add render() method to template proxies
Expand Down
1 change: 0 additions & 1 deletion doc/source/main.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ The classes and functions above are a bit verbose for common operations. A coupl
# first argument is the target
TurboStream("msg").replace.render("OK")
# you can also just do TurboStream("msg").remove if result will
# be resolved as string
TurboStream("msg").remove.render()
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.9"
version = "0.0.10"

setup(
name="django-turbo-response",
Expand Down
106 changes: 106 additions & 0 deletions src/turbo_response/shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,99 @@


class TurboStream:
"""Class for creating Turbo Stream strings and responses."""

def __init__(self, target):
"""
:param target: stream target
:type target: str
"""
self.target = target

@property
def append(self):
"""
:rtype: TurboStreamAction
"""
return TurboStreamAction(self.target, Action.APPEND)

@property
def prepend(self):
"""
:rtype: TurboStreamAction
"""
return TurboStreamAction(self.target, Action.PREPEND)

@property
def remove(self):
"""
:rtype: TurboStreamAction
"""
return TurboStreamAction(self.target, Action.REMOVE)

@property
def replace(self):
"""
:rtype: TurboStreamAction
"""
return TurboStreamAction(self.target, Action.REPLACE)

@property
def update(self):
"""
:rtype: TurboStreamAction
"""
return TurboStreamAction(self.target, Action.UPDATE)


class TurboStreamAction:
"""Returns strings and responses for a specific Turbo Stream action type."""

def __init__(self, target, action):
"""
:param target: Turbo Stream target
:param action: Turbo Stream action
:type target: str
:type action: str
"""
self.action = action
self.target = target

def render(self, content=""):
"""
:param content: enclosed content
:type content: str
:return: a *<turbo-stream>* string
:rtype: str
"""
return render_turbo_stream(
action=self.action, target=self.target, content=content
)

def response(self, content="", **response_kwargs):
"""
:param content: enclosed content
:type content: str
:return: a *<turbo-stream>* HTTP response
:rtype: turbo_response.TurboStreamResponse
"""
return TurboStreamResponse(
action=self.action, target=self.target, content=content, **response_kwargs
)

def template(self, template_name, context=None, **template_kwargs):
"""
:param template_name: Django template name
:param context: template context
:type template_name: str or list
:type context: dict
:return: a *<turbo-stream>* HTTP response
:rtype: TurboStreamTemplateProxy
"""
return TurboStreamTemplateProxy(
template_name,
context,
Expand All @@ -60,6 +113,8 @@ def template(self, template_name, context=None, **template_kwargs):


class TurboStreamTemplateProxy:
"""Wraps template functionality."""

def __init__(self, template_name, context, *, action, target, **template_kwargs):
self.action = action
self.target = target
Expand All @@ -68,6 +123,10 @@ def __init__(self, template_name, context, *, action, target, **template_kwargs)
self.template_kwargs = template_kwargs

def render(self):
"""
:return: rendered template string
:rtype: str
"""
return render_turbo_stream_template(
self.template_name,
self.context,
Expand All @@ -77,6 +136,10 @@ def render(self):
)

def response(self, request, **kwargs):
"""
:return: HTTP response
:rtype: turbo_response.TurboStreamTemplateResponse
"""
return TurboStreamTemplateResponse(
request,
self.template_name,
Expand All @@ -88,36 +151,79 @@ def response(self, request, **kwargs):


class TurboFrame:
"""Class for creating Turbo Frame strings and responses."""

def __init__(self, dom_id):
"""
:param dom_id: DOM ID of turbo frame
:type dom_id: str
"""
self.dom_id = dom_id

def render(self, content=""):
"""
:param content: enclosed content
:type content: str
:return: a *<turbo-frame>* string
:rtype: str
"""
return render_turbo_frame(dom_id=self.dom_id, content=content)

def response(self, content="", **response_kwargs):
"""
:param content: enclosed content
:type content: str
:return: a *<turbo-frame>* HTTP response
:rtype: turbo_response.TurboFrameResponse
"""
return TurboFrameResponse(
dom_id=self.dom_id, content=content, **response_kwargs
)

def template(self, template_name, context=None, **template_kwargs):
"""
:param template_name: Django template name
:param context: template context
:type template_name: str or list
:type context: dict
:return: a *<turbo-frame>* HTTP response
:rtype: TurboFrameTemplateProxy
"""
return TurboFrameTemplateProxy(
template_name, context, dom_id=self.dom_id, **template_kwargs
)


class TurboFrameTemplateProxy:
"""Wraps template functionality."""

def __init__(self, template_name, context, *, dom_id, **template_kwargs):
self.template_name = template_name
self.context = context
self.template_kwargs = template_kwargs
self.dom_id = dom_id

def render(self):
"""
:param content: enclosed content
:type content: str
:return: a *<turbo-frame>* string
:rtype: str
"""
return render_turbo_frame_template(
self.template_name, self.context, dom_id=self.dom_id, **self.template_kwargs
)

def response(self, request, **kwargs):
"""
:return: HTTP response
:rtype: turbo_response.TurboFrameTemplateResponse
"""
return TurboFrameTemplateResponse(
request,
self.template_name,
Expand Down

0 comments on commit b9bdf88

Please sign in to comment.