Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion inertia/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .http import InertiaResponse, inertia, location, render
from .share import share
from .utils import defer, lazy, merge, optional
from .utils import defer, lazy, merge, optional, always

__all__ = [
"InertiaResponse",
Expand All @@ -12,4 +12,5 @@
"lazy",
"merge",
"optional",
"always"
]
8 changes: 4 additions & 4 deletions inertia/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.template.loader import render_to_string

from .helpers import deep_transform_callables, validate_type
from .prop_classes import DeferredProp, IgnoreOnFirstLoadProp, MergeableProp
from .prop_classes import DeferredProp, IgnoreOnFirstLoadProp, MergeableProp, AlwaysProp
from .settings import settings

INERTIA_REQUEST_ENCRYPT_HISTORY = "_inertia_encrypt_history"
Expand Down Expand Up @@ -92,18 +92,18 @@ def build_props(self):
**(self.request.inertia),
**self.props,
}
delete_queue = []
delete_queue = set()

for key, value in _props.items():
if isinstance(value, AlwaysProp):
continue

if self.request.is_a_partial_render(self.component):
if key not in self.request.partial_keys():
delete_queue.append(key)
delete_queue.add(key)
else:
if isinstance(_props[key], IgnoreOnFirstLoadProp):
delete_queue.append(key)
delete_queue.add(key)

_props = {key: val for key, val in _props.items() if key not in delete_queue}

Expand Down
18 changes: 18 additions & 0 deletions inertia/tests/test_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,21 @@ def test_with_props(self):
),
):
self.client.get("/props/")


class AlwaysPropsTestCase(InertiaTestCase):
def test_always_props_are_included_by_default(self):
self.assertJSONResponse(
self.inertia.get("/always/"),
inertia_page("always", props={"name": "Brian", "sport": "Basketball"}),
)

def test_always_props_are_included_when_not_requested(self):
self.assertJSONResponse(
self.inertia.get(
"/always/",
HTTP_X_INERTIA_PARTIAL_DATA="grit",
HTTP_X_INERTIA_PARTIAL_COMPONENT="TestComponent",
),
inertia_page("always", props={"sport": "Basketball", "grit": "intense"}),
)
1 change: 1 addition & 0 deletions inertia/tests/testapp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
path("clear-history/", views.clear_history_test),
path("clear-history-redirect/", views.clear_history_redirect_test),
path("clear-history-type-error/", views.clear_history_type_error_test),
path("always/", views.always_test),
]
11 changes: 10 additions & 1 deletion inertia/tests/testapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.shortcuts import redirect
from django.utils.decorators import decorator_from_middleware

from inertia import defer, inertia, lazy, location, merge, optional, render, share
from inertia import defer, inertia, lazy, location, merge, optional, render, share, always
from inertia.http import INERTIA_SESSION_CLEAR_HISTORY, clear_history, encrypt_history


Expand Down Expand Up @@ -152,3 +152,12 @@ def clear_history_redirect_test(request):
def clear_history_type_error_test(request):
request.session[INERTIA_SESSION_CLEAR_HISTORY] = "foo"
return {}


@inertia("TestComponent")
def always_test(request):
return {
"name": "Brian",
"sport": always(lambda: "Basketball"),
"grit": optional(lambda: "intense"),
}
Loading