Skip to content

Allow AlwaysProp #76

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

Open
wants to merge 4 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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.idea/

# because this is a package, we don't want to commit the lock file
poetry.lock
Expand Down
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",
]
14 changes: 10 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,14 +92,20 @@ def build_props(self):
**(self.request.inertia),
**self.props,
}
delete_queue = set()

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

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

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

return deep_transform_callables(_props)

Expand Down
4 changes: 4 additions & 0 deletions inertia/prop_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class IgnoreOnFirstLoadProp:
pass


class AlwaysProp(CallableProp):
pass


class OptionalProp(CallableProp, IgnoreOnFirstLoadProp):
pass

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"),
}
6 changes: 5 additions & 1 deletion inertia/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.db.models.query import QuerySet
from django.forms.models import model_to_dict as base_model_to_dict

from .prop_classes import DeferredProp, MergeProp, OptionalProp
from .prop_classes import DeferredProp, MergeProp, OptionalProp, AlwaysProp


def model_to_dict(model):
Expand Down Expand Up @@ -41,6 +41,10 @@ def lazy(prop):
return optional(prop)


def always(prop):
return AlwaysProp(prop)


def optional(prop):
return OptionalProp(prop)

Expand Down
211 changes: 211 additions & 0 deletions uv.lock

Large diffs are not rendered by default.