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

Commit d8a7903

Browse files
add default share_auth and lazy load version
1 parent 5ea496f commit d8a7903

File tree

4 files changed

+33
-26
lines changed

4 files changed

+33
-26
lines changed

inertia/middleware.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from django.http import HttpResponse
22
from .share import share
3-
from .version import asset_version
3+
from .version import get_version
44

55

66
class InertiaMiddleware:
@@ -13,8 +13,8 @@ def __call__(self, request):
1313
if is_ajax:
1414
isInertia = request.headers.get("X-Inertia", False)
1515
assert isInertia, "The client not send X-Inertia Header"
16-
inertia_version = asset_version.get_version()
17-
inertia_version_header = str(request.headers.get("X-Inertia-Version", ""))
16+
inertia_version = get_version()
17+
inertia_version_header = str(request.headers.get("X-Inertia-Version", ""))
1818
if inertia_version_header != "" and \
1919
inertia_version_header != str(inertia_version):
2020
response = HttpResponse(status=409)

inertia/share.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ def share(request, key, value):
66
request.session.setdefault("share",{})
77
request.session["share"][key]=value
88

9-
# used in pingcrm ...
10-
def share_flash(request, success=False, error=False, errors=False):
9+
# used in pingcrm ...
10+
def share_flash(request, success=False, error=False, errors=False):
1111
if success:
1212
request.session["success"]=success
1313
if error:
@@ -20,3 +20,19 @@ def share_flash(request, success=False, error=False, errors=False):
2020
share(request, "flash",{'success':success,'error':error})
2121
if errors:
2222
share(request, "errors",errors)
23+
24+
25+
def share_auth(request):
26+
"""Default function for globally sharing authentication data. It can be
27+
overriden with a custom function defined in SHARE_INERTIA."""
28+
auth = {}
29+
if request.user.is_authenticated:
30+
auth = {
31+
"id": request.user.id,
32+
"username": request.user.username,
33+
"first_name": request.user.first_name,
34+
"last_name": request.user.last_name,
35+
"email": request.user.email,
36+
}
37+
38+
share(request, "auth", auth)

inertia/version.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
# pragma pylint: disable=unused-variable
2-
# pragma pylint: disable=not-callable
31
from django.conf import settings
42

5-
class Version:
63

7-
def set_version(self, version):
8-
self.asset_version = version
9-
10-
def get_version(self):
11-
if callable(self.asset_version):
12-
return self.asset_version()
13-
else:
14-
return self.asset_version
15-
16-
17-
asset_version = Version()
18-
asset_version.set_version(getattr(settings,"VERSION",1))
4+
def get_version():
5+
asset_version = settings.get("VERSION", 1)
6+
if callable(asset_version):
7+
return asset_version()
8+
else:
9+
return asset_version

inertia/views.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from django.middleware import csrf
1010
from django.urls import get_callable
1111
from .share import share
12-
from .version import asset_version
12+
from .version import get_version
1313

1414
from django.views.generic import View
1515
from django.conf import settings
@@ -49,8 +49,8 @@ def render_inertia(request, component_name, props=None, template_name=None):
4949
"in settings.py or pass template parameter."
5050
)
5151

52-
# share custom data if any
53-
share_method_path = getattr(settings, "INERTIA_SHARE", None)
52+
# share custom data or default authenticated user
53+
share_method_path = getattr(settings, "INERTIA_SHARE", "inertia.share.share_auth")
5454
if share_method_path:
5555
share_method = get_callable(share_method_path)
5656
share_method(request)
@@ -69,21 +69,21 @@ def render_inertia(request, component_name, props=None, template_name=None):
6969
del request.session[key]
7070

7171
# subsequent renders
72-
inertia_version = asset_version.get_version()
72+
inertia_version = get_version()
7373
is_version_correct = 'X-Inertia-Version' in request.headers and \
7474
request.headers["X-Inertia-Version"] == str(inertia_version)
7575
if 'X-Inertia' in request.headers:
7676
response = JsonResponse({
7777
"component": component_name,
7878
"props": props,
79-
"version": asset_version.get_version(),
79+
"version": inertia_version,
8080
"url": request.get_full_path()
8181
})
8282
response['X-Inertia'] = True
8383
response['Vary'] = 'Accept'
8484
return response
8585
context = _build_context(component_name, props,
86-
asset_version.get_version(),
86+
inertia_version,
8787
url=request.get_full_path())
8888
return render(request, inertia_template, context)
8989

0 commit comments

Comments
 (0)