Skip to content

Commit

Permalink
Feature/26 rename (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-yin authored Jan 11, 2024
1 parent c98a86e commit 1fa1bda
Show file tree
Hide file tree
Showing 37 changed files with 68 additions and 62 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Use file-based templates for rendering streams/frames

## [0.0.39] - 2021-4-2

Fix for https://github.com/hotwire-django/django-turbo-response/issues/5
Fix for https://github.com/rails-inspire-django/django-turbo-response/issues/5

## [0.0.36] - 2021-3-30

Expand Down
10 changes: 5 additions & 5 deletions docs/source/channels.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ This approach is not recommended anymore, please consider using [turbo_stream_fr
This library can also be used with [django-channels](https://channels.readthedocs.io/en/stable/). As with multiple streams, you can use the **TurboStream** class to broadcast turbo-stream content from your consumers.

```python
from turbo_response import render_turbo_stream, render_turbo_stream_template
from turbo_helper import render_turbo_stream, render_turbo_stream_template
from channels.generic.websocket import AsyncJsonWebsocketConsumer


class ChatConsumer(AsyncJsonWebsocketConsumer):

async def chat_message(self, event):

# DB methods omitted for brevity
message = await self.get_message(event["message"]["id"])
num_unread_messages = await self.get_num_unread_messages()

if message:
await self.send(
TurboStream("unread_message_counter")
.replace.render(str(num_unread_messages))
.replace.render(str(num_unread_messages))
)

await self.send(
TurboStream("messages").append.template(
"chat/_message.html",
{"message": message, "user": self.scope['user']},
"chat/_message.html",
{"message": message, "user": self.scope['user']},
).render()
)
```
Expand Down
35 changes: 20 additions & 15 deletions docs/source/form.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,16 @@ def my_view(request):
return TemplateResponse(request, "my_form.html", {"form": my_form}, status=status)
```

As this is such a common pattern, we provide for convenience the **turbo_response.render_form_response** shortcut function which automatically sets the correct status depending on the form state (and adds "form" to the template context):
As this is such a common pattern, we provide for convenience the **turbo_helper.render_form_response** shortcut function which automatically sets the correct status depending on the form state (and adds "form" to the template context):

```python
from django.shortcuts import redirect

from turbo_response import render_form_response
from turbo_helper import render_form_response

from myapp import MyForm


def my_view(request):
if request.method == "POST":
form = MyForm(request.POST)
Expand All @@ -65,16 +66,17 @@ def my_view(request):
return render_form_response(request, form, "my_form.html")
```

If you are using CBVs, this package has a mixin class, **turbo_response.mixins.TurboFormMixin** that sets the correct status automatically to 422 for an invalid form:
If you are using CBVs, this package has a mixin class, **turbo_helper.mixins.TurboFormMixin** that sets the correct status automatically to 422 for an invalid form:

```python
from django.views.generic import FormView

from turbo_response import redirect_303
from turbo_response.mixins import TurboFormMixin
from turbo_helper import redirect_303
from turbo_helper.mixins import TurboFormMixin

from myapp import MyForm


class MyView(TurboFormMixin, FormView):
template_name = "my_form.html"

Expand All @@ -84,9 +86,9 @@ class MyView(TurboFormMixin, FormView):

In addition you can just subclass these views for common cases:

- **turbo_response.views.TurboFormView**
- **turbo_response.views.TurboCreateView**
- **turbo_response.views.TurboUpdateView**
- **turbo_helper.views.TurboFormView**
- **turbo_helper.views.TurboCreateView**
- **turbo_helper.views.TurboUpdateView**

In some cases you may wish to return a turbo-stream response containing just the form when the form is invalid instead of a full page visit. In this case just return a stream rendering the form partial in the usual manner. For example:

Expand All @@ -95,10 +97,11 @@ from django.shortcuts import redirect_303
from django.template.response import TemplateResponse
from django.views.generic import FormView

from turbo_response import TurboStream
from turbo_helper import TurboStream

from myapp import MyForm


def my_view(request):
if request.method == "POST":
form = MyForm(request.POST)
Expand Down Expand Up @@ -146,11 +149,12 @@ And your templates would look like this:
</form>
```

As this is a useful pattern in many situations, for example when handling forms inside modals, this package provides a mixin class **turbo_response.mixins.TurboStreamFormMixin**:
As this is a useful pattern in many situations, for example when handling forms inside modals, this package provides a mixin class **turbo_helper.mixins.TurboStreamFormMixin**:

```python
from django.views.generic import FormView
from turbo_response.mixins import TurboStreamFormMixin
from turbo_helper.mixins import TurboStreamFormMixin


class MyView(TurboStreamFormMixin, FormView):
turbo_stream_target = "form-target"
Expand All @@ -162,14 +166,15 @@ This mixin will automatically add the target name to the template context as *tu

As with the form mixin above, the package includes a number of view classes using this mixin:

- **turbo_response.views.TurboStreamFormView**
- **turbo_response.views.TurboStreamCreateView**
- **turbo_response.views.TurboStreamUpdateView**
- **turbo_helper.views.TurboStreamFormView**
- **turbo_helper.views.TurboStreamCreateView**
- **turbo_helper.views.TurboStreamUpdateView**

So the above example could be rewritten as:

```python
from turbo_response.views import TurboStreamFormView
from turbo_helper.views import TurboStreamFormView


class MyView(TurboStreamFormView):
turbo_stream_target = "form-target"
Expand Down
6 changes: 3 additions & 3 deletions docs/source/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Next update **INSTALLED_APPS**:

```python
INSTALLED_APPS = [
"turbo_response",
"turbo_helper",
...
]
```
Expand All @@ -23,12 +23,12 @@ INSTALLED_APPS = [

## Middleware

You can optionally install `turbo_response.middleware.TurboMiddleware`. This adds the attribute `turbo` to your `request` if the Turbo client adds `Accept: text/vnd.turbo-stream.html;` to the header:
You can optionally install `turbo_helper.middleware.TurboMiddleware`. This adds the attribute `turbo` to your `request` if the Turbo client adds `Accept: text/vnd.turbo-stream.html;` to the header:

```python
MIDDLEWARE = [
...
"turbo_response.middleware.TurboMiddleware",
"turbo_helper.middleware.TurboMiddleware",
"django.middleware.common.CommonMiddleware",
...
]
Expand Down
14 changes: 9 additions & 5 deletions docs/source/original.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A couple of classes, **TurboFrame** and **TurboStream**, provide the basic API f
To render plain strings:

```python
from turbo_response import TurboFrame, TurboStream, Action
from turbo_helper import TurboFrame, TurboStream, Action

# returns <turbo-stream action="replace" target="msg"><template>OK</template></turbo-stream>
TurboStream("msg").replace.render("OK")
Expand Down Expand Up @@ -102,7 +102,8 @@ Each item in the cart has an inline edit form that might look like this:
```

```python
from turbo_response import TurboStreamResponse, TurboStream
from turbo_helper import TurboStreamResponse, TurboStream


def update_cart_item(request, item_id):
# item saved to e.g. session or db
Expand All @@ -121,7 +122,8 @@ def update_cart_item(request, item_id):
Or using a generator:

```python
from turbo_response import TurboStreamStreamingResponse, TurboStream
from turbo_helper import TurboStreamStreamingResponse, TurboStream


def update_cart_item(request, item_id):
# item saved to e.g. session or db
Expand All @@ -134,6 +136,7 @@ def update_cart_item(request, item_id):
def render_response():
yield TurboStream("nav-cart-total").replace.render(total_amount)
yield TurboStream("cart-summary-total").replace.render(total_amount)

return TurboStreamStreamingResponse(render_response())
```

Expand All @@ -146,8 +149,9 @@ Note that this technique is something of an anti-pattern; if you have to update
You can accomplish the above using the **turbo_stream_response** decorator with your view. This will check the output and wrap the response in a **TurboStreamResponse** or **TurboStreamStreamingResponse**:

```python
from turbo_response import TurboStream
from turbo_response.decorators import turbo_stream_response
from turbo_helper import TurboStream
from turbo_helper.decorators import turbo_stream_response


@turbo_stream_response
def update_cart_item(request, item_id):
Expand Down
6 changes: 3 additions & 3 deletions docs/source/template-tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Notes:
You can return Turbo Stream resposne in Django view like this:

```python
from turbo_response.response import TurboStreamResponse
from turbo_helper.response import TurboStreamResponse
from django.template.loader import render_to_string

context = {}
Expand Down Expand Up @@ -110,7 +110,7 @@ In `routing.py`, register `TurboStreamCableChannel`

```python
from actioncable import cable_channel_register
from turbo_response.cable_channel import TurboStreamCableChannel
from turbo_helper.cable_channel import TurboStreamCableChannel

cable_channel_register(TurboStreamCableChannel)
```
Expand All @@ -128,7 +128,7 @@ In Django template, we can subscribe to stream source like this:
Then in Python code, we can send Turbo Stream to the stream source like this

```python
from turbo_response.channel_helper import broadcast_render_to
from turbo_helper.channel_helper import broadcast_render_to

broadcast_render_to(
["chat", instance.chat_id],
Expand Down
3 changes: 2 additions & 1 deletion docs/source/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ To simulate the Turbo-Stream header, you should set **HTTP_ACCEPT**.

```python
from django.test import TestCase
from turbo_response.constants import TURBO_STREAM_MIME_TYPE
from turbo_helper.constants import TURBO_STREAM_MIME_TYPE


class TestViews(TestCase):

Expand Down
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[tool.poetry]
name = "django-turbo-response"
name = "django-turbo-helper"
version = "1.0.3"
description = "Hotwired/Turbo response helpers for Django"
authors = ["Michael Yin <[email protected]>"]
license = "MIT"
homepage = "https://github.com/hotwire-django/django-turbo-response"
homepage = "https://github.com/rails-inspire-django/django-turbo-response"
readme = "README.md"
packages = [{ include = "turbo_response", from = "src" }]
packages = [{ include = "turbo_helper", from = "src" }]

[tool.poetry.urls]
Changelog = "https://github.com/hotwire-django/django-turbo-response/releases"
Changelog = "https://github.com/rails-inspire-django/django-turbo-response/releases"

[tool.poetry.dependencies]
python = ">=3.8"
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions src/turbo_helper/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class TurboHelperConfig(AppConfig):
name = "turbo_helper"
verbose_name = "Turbo Helper"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 0 additions & 6 deletions src/turbo_response/apps.py

This file was deleted.

2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def pytest_configure():
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.sites",
"turbo_response",
"turbo_helper",
"channels",
"tests.testapp.apps.TestAppConfig",
],
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cable_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from channels.layers import get_channel_layer
from channels.testing import WebsocketCommunicator

from turbo_response.cable_channel import TurboStreamCableChannel
from turbo_response.channel_helper import generate_signed_stream_key
from turbo_helper.cable_channel import TurboStreamCableChannel
from turbo_helper.channel_helper import generate_signed_stream_key

# register the TurboStreamCableChannel
cable_channel_register(TurboStreamCableChannel)
Expand Down
8 changes: 2 additions & 6 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

from django.http import HttpResponse

from turbo_response import (
TurboStream,
TurboStreamResponse,
TurboStreamStreamingResponse,
)
from turbo_response.decorators import turbo_stream_response
from turbo_helper import TurboStream, TurboStreamResponse, TurboStreamStreamingResponse
from turbo_helper.decorators import turbo_stream_response


class TestTurboStreamResponse:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_frame.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import http

from turbo_response import TurboFrame
from turbo_helper import TurboFrame


class TestTurboFrame:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_middleware.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from django.http import HttpResponse

from turbo_response.middleware import TurboMiddleware
from turbo_helper.middleware import TurboMiddleware


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion tests/test_renderers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from turbo_response import Action, render_turbo_frame, render_turbo_stream
from turbo_helper import Action, render_turbo_frame, render_turbo_stream


class TestRenderTurboStream:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
from django import forms

from turbo_response.shortcuts import redirect_303, render_form_response
from turbo_helper.shortcuts import redirect_303, render_form_response

pytestmark = pytest.mark.django_db

Expand Down
2 changes: 1 addition & 1 deletion tests/test_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.http import HttpRequest

from turbo_response import TurboStream
from turbo_helper import TurboStream


class TestTurboStream:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.template import Context, Template

from tests.testapp.models import TodoItem
from turbo_response.templatetags.turbo_helper import dom_id
from turbo_helper.templatetags.turbo_helper import dom_id

pytestmark = pytest.mark.django_db

Expand Down
2 changes: 1 addition & 1 deletion tests/test_template.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.http import HttpRequest

from turbo_response import (
from turbo_helper import (
Action,
render_turbo_frame_template,
render_turbo_stream_template,
Expand Down
6 changes: 3 additions & 3 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from django import forms
from django.views.generic import CreateView

from turbo_response import Action
from turbo_response.mixins import TurboFormAdapterMixin
from turbo_response.views import (
from turbo_helper import Action
from turbo_helper.mixins import TurboFormAdapterMixin
from turbo_helper.views import (
TurboCreateView,
TurboFormView,
TurboFrameTemplateView,
Expand Down

0 comments on commit 1fa1bda

Please sign in to comment.