From b9bdf88f9d379f0ec00278da0b8f8bc7ba68758b Mon Sep 17 00:00:00 2001 From: Dan Jacob Date: Wed, 30 Dec 2020 17:16:09 +0200 Subject: [PATCH] Docstrings updated --- CHANGES.md | 4 ++ doc/source/main.rst | 1 - setup.py | 2 +- src/turbo_response/shortcuts.py | 106 ++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index af66a36..c2fa9da 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 diff --git a/doc/source/main.rst b/doc/source/main.rst index 7137287..ae27c91 100644 --- a/doc/source/main.rst +++ b/doc/source/main.rst @@ -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() diff --git a/setup.py b/setup.py index 18b0e0e..ecf5de7 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ # Third Party Libraries from setuptools import setup -version = "0.0.9" +version = "0.0.10" setup( name="django-turbo-response", diff --git a/src/turbo_response/shortcuts.py b/src/turbo_response/shortcuts.py index 4f1fa5d..6dd2bdb 100644 --- a/src/turbo_response/shortcuts.py +++ b/src/turbo_response/shortcuts.py @@ -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 ** 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 ** 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 ** HTTP response + :rtype: TurboStreamTemplateProxy + """ return TurboStreamTemplateProxy( template_name, context, @@ -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 @@ -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, @@ -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, @@ -88,24 +151,56 @@ 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 ** 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 ** 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 ** 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 @@ -113,11 +208,22 @@ def __init__(self, template_name, context, *, dom_id, **template_kwargs): self.dom_id = dom_id def render(self): + """ + :param content: enclosed content + :type content: str + + :return: a ** 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,