Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set a component's parent when restoring it from cache #670

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
run: poetry install

- name: ruff check
run: poetry run ruff .
run: poetry run ruff check .

- name: Run all tests
run: poetry run nox
13 changes: 11 additions & 2 deletions django_unicorn/cacher.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,17 @@ def restore_from_cache(component_cache_key: str, request: HttpRequest = None) ->
roots[root.component_cache_key] = root

while root.parent:
root = cache.get(root.parent.component_cache_key)
roots[root.component_cache_key] = root
parent_component_cache_key = root.parent.component_cache_key

# Store the actual parent component for later
cached_parent_component = cache.get(parent_component_cache_key)
roots[parent_component_cache_key] = cached_parent_component

# Set the component's parent to the actual component to override the pointer
# that will be here when using nested components with a direct view
root.parent = cached_parent_component

root = cached_parent_component

to_traverse: List["django_unicorn.views.UnicornView"] = []
to_traverse.append(root)
Expand Down
4 changes: 3 additions & 1 deletion django_unicorn/components/unicorn_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def __init__(self, component_args: Optional[List] = None, **kwargs):
if not self.component_name:
raise AssertionError("Component name is required")

if "id" in kwargs and kwargs["id"]:
if kwargs.get("id") is not None:
# Sometimes the component_id is initially in kwargs["id"]
self.component_id = kwargs["id"]

Expand Down Expand Up @@ -379,6 +379,8 @@ def dispatch(self, request, *args, **kwargs): # noqa: ARG002
self.mount()
self.hydrate()

self._cache_component(**kwargs)

return self.render_to_response(
context=self.get_context_data(),
component=self,
Expand Down
Loading
Loading