Skip to content
This repository was archived by the owner on Dec 11, 2024. It is now read-only.

Commit ae98e69

Browse files
add share custom function settings
1 parent 4a6c37d commit ae98e69

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

inertia/views.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from django.shortcuts import render
88
from django.http import JsonResponse
99
from django.middleware import csrf
10+
from django.urls import get_callable
1011
from .share import share
1112
from .version import asset_version
1213

@@ -48,6 +49,12 @@ def render_inertia(request, component_name, props=None, template_name=None):
4849
"in settings.py or pass template parameter."
4950
)
5051

52+
# share custom data if any
53+
share_method_path = getattr(settings, "INERTIA_SHARE", None)
54+
if share_method_path:
55+
share_method = get_callable(share_method_path)
56+
share_method(request)
57+
5158
if props is None:
5259
props = {}
5360
shared = {}
@@ -78,7 +85,7 @@ def render_inertia(request, component_name, props=None, template_name=None):
7885
context = _build_context(component_name, props,
7986
asset_version.get_version(),
8087
url=request.get_full_path())
81-
88+
import pdb ; pdb.set_trace()
8289
return render(request, inertia_template, context)
8390

8491
class InertiaMixin:
@@ -95,4 +102,3 @@ def render_to_response(self, context, **kwargs):
95102
self.props = {}
96103
self.props.update(self.get_data(context))
97104
return render_inertia(self.request, self.component_name, self.props, self.template_name)
98-

test.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,28 @@
77
from django.http import HttpResponse
88
import os
99

10+
from inertia.share import share
11+
12+
1013
settings.configure(
1114
VERSION=1, DEBUG=True,
1215
TEMPLATES= [{
1316
'BACKEND': 'django.template.backends.django.DjangoTemplates',
1417
'APP_DIRS': True,
1518
'DIRS': [ os.path.join('testutils'), ],
16-
}]
19+
}],
20+
INERTIA_SHARE = "test.share_custom_func"
1721
)
1822
django.setup()
1923
from inertia.version import asset_version
2024
from inertia.views import render_inertia
2125
from inertia.middleware import InertiaMiddleware
2226

2327

28+
def share_custom_func(request):
29+
share(request, "custom_data", "custom_value")
30+
31+
2432
class TestInertia(TestCase):
2533
def test_views(self):
2634
requestfactory = RequestFactory()
@@ -66,3 +74,12 @@ def test_middleware(self):
6674
self.set_session(request)
6775
response = InertiaMiddleware(view)(request)
6876
self.assertTrue(response.status_code == 200, response.status_code)
77+
78+
def test_share_custom_data(self):
79+
requestfactory = RequestFactory()
80+
request = requestfactory.get("/")
81+
self.set_session(request)
82+
response = render_inertia(request, "Index")
83+
import pdb ; pdb.set_trace()
84+
self.assertTrue(b'share_custom_data"' in response.content)
85+
self.assertTrue(b'share_custom_value"' in response.content)

0 commit comments

Comments
 (0)