-
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
84faeb3
commit f06564d
Showing
18 changed files
with
373 additions
and
62 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,5 +23,6 @@ Topics | |
real-time-updates.md | ||
extend-turbo-stream.md | ||
multi-format.md | ||
signal-decorator.md | ||
redirect.md | ||
test.md |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Signal Decorator | ||
|
||
In Django, developer usually use `post_save` signal to perform certain actions after a model instance is saved. | ||
|
||
Even `created` parameter indicates whether the instance is newly created or an existing one, this is not that straightforward. | ||
|
||
With `turbo_helper`, we provide **syntax suger** to make it more clear, just like Rails. | ||
|
||
```python | ||
from turbo_helper import after_create_commit, after_update_commit, after_delete_commit | ||
|
||
|
||
@after_create_commit(sender=Message) | ||
def create_message_content(sender, instance, created, **kwargs): | ||
broadcast_action_to( | ||
"chat", | ||
instance.chat_id, | ||
action="append", | ||
template="demo_openai/message_content.html", | ||
context={ | ||
"instance": instance, | ||
}, | ||
target=dom_id(instance.chat_id, "message_list"), | ||
) | ||
``` | ||
|
||
Notes: | ||
|
||
1. `after_create_commit`, `after_update_commit`, `after_delete_commit`, are decorators, they are used to decorate a function, which will be called after the model instance is created, updated or deleted. | ||
2. The function decorated by `after_create_commit`, `after_update_commit`, receive the same arguments as `post_save` signal handler. | ||
3. The function decorated by `after_delete_commit` receive the same arguments as `post_delete` signal handler. | ||
4. This can make our code more clear, especially when we need to some broadcasts. |
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.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,5 @@ | ||
{% load turbo_helper %} | ||
|
||
{% turbo_stream 'append' 'todo_list' %} | ||
<div>{{ instance.description }}</div> | ||
{% endturbo_stream %} |
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,114 @@ | ||
import unittest | ||
from unittest import mock | ||
|
||
import pytest | ||
|
||
import turbo_helper.channels.broadcasts | ||
from tests.testapp.models import TodoItem | ||
from tests.utils import assert_dom_equal | ||
from turbo_helper import dom_id | ||
from turbo_helper.channels.broadcasts import ( | ||
broadcast_action_to, | ||
broadcast_render_to, | ||
broadcast_stream_to, | ||
) | ||
|
||
pytestmark = pytest.mark.django_db | ||
|
||
|
||
class TestBroadcastStreamTo: | ||
def test_broadcast_stream_to(self, monkeypatch): | ||
mock_cable_broadcast = mock.MagicMock(name="cable_broadcast") | ||
monkeypatch.setattr( | ||
turbo_helper.channels.broadcasts, "cable_broadcast", mock_cable_broadcast | ||
) | ||
|
||
################################################################################ | ||
|
||
broadcast_stream_to("test", content="hello world") | ||
|
||
mock_cable_broadcast.assert_called_with( | ||
group_name="test", message="hello world" | ||
) | ||
|
||
################################################################################ | ||
todo_item = TodoItem.objects.create(description="Test Model") | ||
|
||
broadcast_stream_to(todo_item, content="hello world") | ||
|
||
mock_cable_broadcast.assert_called_with( | ||
group_name=dom_id(todo_item), message="hello world" | ||
) | ||
|
||
################################################################################ | ||
todo_item = TodoItem.objects.create(description="Test Model") | ||
|
||
broadcast_stream_to(todo_item, "test", content="hello world") | ||
|
||
mock_cable_broadcast.assert_called_with( | ||
group_name=f"{dom_id(todo_item)}_test", message="hello world" | ||
) | ||
|
||
|
||
class TestBroadcastActionTo: | ||
def test_broadcast_action_to(self, monkeypatch): | ||
mock_cable_broadcast = mock.MagicMock(name="cable_broadcast") | ||
monkeypatch.setattr( | ||
turbo_helper.channels.broadcasts, "cable_broadcast", mock_cable_broadcast | ||
) | ||
|
||
################################################################################ | ||
|
||
broadcast_action_to("tasks", action="remove", target="new_task") | ||
|
||
assert mock_cable_broadcast.call_args.kwargs["group_name"] == "tasks" | ||
assert_dom_equal( | ||
mock_cable_broadcast.call_args.kwargs["message"], | ||
'<turbo-stream action="remove" target="new_task"><template></template></turbo-stream>', | ||
) | ||
|
||
################################################################################ | ||
todo_item = TodoItem.objects.create(description="Test Model") | ||
|
||
broadcast_action_to(todo_item, action="remove", target="new_task") | ||
|
||
mock_cable_broadcast.assert_called_with( | ||
group_name=dom_id(todo_item), message=unittest.mock.ANY | ||
) | ||
|
||
################################################################################ | ||
todo_item = TodoItem.objects.create(description="Test Model") | ||
|
||
broadcast_action_to(todo_item, "test", action="remove", target="new_task") | ||
|
||
mock_cable_broadcast.assert_called_with( | ||
group_name=f"{dom_id(todo_item)}_test", message=unittest.mock.ANY | ||
) | ||
|
||
|
||
class TestBroadcastRenderTo: | ||
def test_broadcast_render_to(self, monkeypatch): | ||
mock_cable_broadcast = mock.MagicMock(name="cable_broadcast") | ||
monkeypatch.setattr( | ||
turbo_helper.channels.broadcasts, "cable_broadcast", mock_cable_broadcast | ||
) | ||
|
||
################################################################################ | ||
todo_item = TodoItem.objects.create(description="test") | ||
|
||
broadcast_render_to( | ||
todo_item, | ||
template="todoitem.turbo_stream.html", | ||
context={ | ||
"instance": todo_item, | ||
}, | ||
) | ||
|
||
mock_cable_broadcast.assert_called_with( | ||
group_name=dom_id(todo_item), message=unittest.mock.ANY | ||
) | ||
|
||
assert_dom_equal( | ||
mock_cable_broadcast.call_args.kwargs["message"], | ||
'<turbo-stream action="append" target="todo_list"><template><div>test</div></template></turbo-stream>', | ||
) |
Oops, something went wrong.