Skip to content

Commit

Permalink
for django-actioncable 1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-yin committed Jan 18, 2024
1 parent 3c39f9c commit 1603a36
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 24 deletions.
6 changes: 4 additions & 2 deletions docs/source/real-time-updates.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ Then in Python code, we can send Turbo Stream to the stream source like this
from turbo_helper.channel_helper import broadcast_render_to

broadcast_render_to(
["chat", instance.chat_id],
"chat",
instance.chat_id,
template="message_append.turbo_stream.html",
context={
"instance": instance,
},
)
```

The `["chat", instance.chat_id]` **should** match the positional arguments in the `turbo_stream_from` tag.
1. `arguments` **should** match the arguments passed in the `turbo_stream_from` tag.
2. `keyword arguments` `template` and `context` are used to render the template.

The web page can be updated in real time, through Turbo Stream over Websocket.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Changelog = "https://github.com/rails-inspire-django/django-turbo-helper/release
[tool.poetry.dependencies]
python = ">=3.8"
django = ">=3.0"
django-actioncable = ">=1.0.4"

[tool.poetry.dev-dependencies]

Expand Down
41 changes: 20 additions & 21 deletions src/turbo_helper/channel_helper.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
from typing import List, Tuple, Union
from typing import Tuple

from actioncable import cable_broadcast
from django.core import signing
from django.core.signing import Signer
from django.template.loader import render_to_string

from .templatetags.turbo_helper import dom_id

try:
from actioncable import cable_broadcast
except ImportError as err:
raise Exception("Please make sure django-channels is installed") from err


signer = Signer()


def stream_name_from(streamables: Union[List, object]) -> str:
def stream_name_from(*streamables) -> str:
"""
Generate stream_name from a list of objects or a single object.
"""
if isinstance(streamables, list):
return "_".join(stream_name_from(streamable) for streamable in streamables)
if len(streamables) == 1:
return dom_id(streamables[0])
else:
return dom_id(streamables)
return "_".join(stream_name_from(streamable) for streamable in streamables)


def generate_signed_stream_key(stream_name: str) -> str:
Expand All @@ -46,8 +41,10 @@ def verify_signed_stream_key(signed_stream_key: str) -> Tuple[bool, str]:
return False, ""


def broadcast_render_to(streamables: Union[List, object], template: str, context=None):
def broadcast_render_to(*args, **kwargs):
"""
Rails: Turbo::Streams::Broadcasts#broadcast_render_to
This function help render HTML to Turbo Stream Channel
for example, in Django template, we subscribe to a Turbo stream Channel
Expand All @@ -57,21 +54,23 @@ def broadcast_render_to(streamables: Union[List, object], template: str, context
Then in Python code
broadcast_render_to(
["chat", instance.chat_id],
"chat",
instance.chat_id,
template="message_append.turbo_stream.html",
context={
"instance": instance,
},
)
"""
if context is None:
context = {}
template = kwargs.pop("template", None)
broadcast_stream_to(
*args, content=render_to_string(template_name=template, **kwargs)
)

html = render_to_string(template, context=context)
stream_name = stream_name_from(streamables)

def broadcast_stream_to(*args, content):
stream_name = stream_name_from(*args)
cable_broadcast(
stream_name,
{
"message": html,
},
group_name=stream_name,
message=content,
)
2 changes: 1 addition & 1 deletion src/turbo_helper/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def render_turbo_stream_from(stream_name_array: List[Any]):
from .cable_channel import TurboStreamCableChannel
from .channel_helper import generate_signed_stream_key, stream_name_from

stream_name_string = stream_name_from(stream_name_array)
stream_name_string = stream_name_from(*stream_name_array)

django_engine = engines["django"]
template_string = """<turbo-cable-stream-source channel="{{ channel }}" signed-stream-name="{{ signed_stream_name }}"></turbo-cable-stream-source>""" # noqa
Expand Down

0 comments on commit 1603a36

Please sign in to comment.