-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9623f5d
commit 158ef32
Showing
14 changed files
with
285 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from actioncable import cable_broadcast | ||
from django.template.loader import render_to_string | ||
|
||
from turbo_helper.renderers import render_turbo_stream_refresh | ||
from turbo_helper.stream import action_proxy | ||
|
||
from .stream_name import stream_name_from | ||
|
||
|
||
def broadcast_render_to(*streamables, **kwargs): | ||
""" | ||
Rails: Turbo::Streams::Broadcasts#broadcast_render_to | ||
Help render Django template to Turbo Stream Channel | ||
for example, in Django template, we subscribe to a Turbo stream Channel | ||
{% turbo_stream_from 'chat' view.kwargs.chat_pk %} | ||
Then in Python code | ||
broadcast_render_to( | ||
"chat", | ||
instance.chat_id, | ||
template="message_append.turbo_stream.html", | ||
context={ | ||
"instance": instance, | ||
}, | ||
) | ||
""" | ||
template = kwargs.pop("template", None) | ||
broadcast_stream_to( | ||
*streamables, content=render_to_string(template_name=template, **kwargs) | ||
) | ||
|
||
|
||
def broadcast_action_to(*streamables, action, target=None, targets=None, **kwargs): | ||
""" | ||
For now, we do not support: | ||
broadcast_remove_to | ||
broadcast_replace_to | ||
broadcast_update_to | ||
... | ||
But we can use to do the same work | ||
For example: | ||
# remove DOM which has id="new_task" | ||
broadcast_action_to("tasks", action="remove", target="new_task") | ||
""" | ||
content = action_proxy( | ||
action, | ||
target=target, | ||
targets=targets, | ||
**kwargs, | ||
) | ||
broadcast_stream_to(*streamables, content=content) | ||
|
||
|
||
def broadcast_refresh_to(*streamables, request, **kwargs): | ||
content = render_turbo_stream_refresh(request_id=request.turbo.request_id, **kwargs) | ||
broadcast_stream_to(*streamables, content=content) | ||
|
||
|
||
def broadcast_stream_to(*streamables, content): | ||
stream_name = stream_name_from(*streamables) | ||
cable_broadcast( | ||
group_name=stream_name, | ||
message=content, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from typing import Tuple | ||
|
||
from django.core import signing | ||
from django.core.signing import Signer | ||
|
||
from turbo_helper.templatetags.turbo_helper import dom_id | ||
|
||
signer = Signer() | ||
|
||
|
||
def stream_name_from(*streamables) -> str: | ||
""" | ||
Generate stream_name from a list of objects or a single object. | ||
""" | ||
if len(streamables) == 1: | ||
return dom_id(streamables[0]) | ||
else: | ||
return "_".join(stream_name_from(streamable) for streamable in streamables) | ||
|
||
|
||
def generate_signed_stream_key(stream_name: str) -> str: | ||
""" | ||
Generate signed stream key from stream_name | ||
""" | ||
return signer.sign(stream_name) | ||
|
||
|
||
def verify_signed_stream_key(signed_stream_key: str) -> Tuple[bool, str]: | ||
""" | ||
Verify signed stream key | ||
""" | ||
try: | ||
unsigned_data = signer.unsign(signed_stream_key) | ||
return True, unsigned_data | ||
|
||
except signing.BadSignature: | ||
pass | ||
|
||
return False, "" |
8 changes: 2 additions & 6 deletions
8
src/turbo_helper/cable_channel.py → src/turbo_helper/channels/streams_channel.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.