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

Commit 7310502

Browse files
authored
Merge pull request #6 from girardinsamuel/feat/add-share-custom
2 parents a3e6ac0 + a6c2091 commit 7310502

File tree

6 files changed

+84
-54
lines changed

6 files changed

+84
-54
lines changed

.github/workflows/inertia-ci.yml

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,33 @@
11
name: Python package
22

3-
on: [push]
3+
on: [push, pull_request]
44

55
jobs:
66
build:
7-
87
runs-on: ubuntu-latest
98
strategy:
109
matrix:
1110
python-version: [3.6, 3.7, 3.8]
1211

1312
steps:
14-
- uses: actions/checkout@v2
15-
- name: Set up Python ${{ matrix.python-version }}
16-
uses: actions/setup-python@v2
17-
with:
18-
python-version: ${{ matrix.python-version }}
19-
- name: Install dependencies
20-
run: |
21-
python -m pip install --upgrade pip
22-
pip install flake8 bake-cli
23-
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
24-
- name: Lint with flake8
25-
run: |
26-
# stop the build if there are Python syntax errors or undefined names
27-
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
28-
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
29-
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
30-
- name: Executing Test
31-
run: bake t
32-
-
33-
uses: AndreMiras/coveralls-python-action@develop
34-
with:
35-
github-token: ${{ secrets.GITHUB_TOKEN }}
13+
- uses: actions/checkout@v2
14+
- name: Set up Python ${{ matrix.python-version }}
15+
uses: actions/setup-python@v2
16+
with:
17+
python-version: ${{ matrix.python-version }}
18+
- name: Install dependencies
19+
run: |
20+
python -m pip install --upgrade pip
21+
pip install flake8 bake-cli
22+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
23+
- name: Lint with flake8
24+
run: |
25+
# stop the build if there are Python syntax errors or undefined names
26+
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
27+
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
28+
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
29+
- name: Executing Test
30+
run: bake t
31+
- uses: AndreMiras/coveralls-python-action@develop
32+
with:
33+
github-token: ${{ secrets.GITHUB_TOKEN }}

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, HttpResponseRedirect
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: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
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+
try:
6+
asset_version = settings.VERSION
7+
except AttributeError:
8+
asset_version = 1
9+
if callable(asset_version):
10+
return asset_version()
11+
else:
12+
return asset_version

inertia/views.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
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
11-
from .version import asset_version
12+
from .version import get_version
1213

1314
from django.views.generic import View
1415
from django.conf import settings
@@ -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 or default authenticated user
53+
share_method_path = getattr(settings, "INERTIA_SHARE", "inertia.share.share_auth")
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 = {}
@@ -62,23 +69,22 @@ def render_inertia(request, component_name, props=None, template_name=None):
6269
del request.session[key]
6370

6471
# subsequent renders
65-
inertia_version = asset_version.get_version()
72+
inertia_version = get_version()
6673
is_version_correct = 'X-Inertia-Version' in request.headers and \
6774
request.headers["X-Inertia-Version"] == str(inertia_version)
6875
if 'X-Inertia' in request.headers:
6976
response = JsonResponse({
7077
"component": component_name,
7178
"props": props,
72-
"version": asset_version.get_version(),
79+
"version": inertia_version,
7380
"url": request.get_full_path()
7481
})
7582
response['X-Inertia'] = True
7683
response['Vary'] = 'Accept'
7784
return response
7885
context = _build_context(component_name, props,
79-
asset_version.get_version(),
86+
inertia_version,
8087
url=request.get_full_path())
81-
8288
return render(request, inertia_template, context)
8389

8490
class InertiaMixin:
@@ -95,4 +101,3 @@ def render_to_response(self, context, **kwargs):
95101
self.props = {}
96102
self.props.update(self.get_data(context))
97103
return render_inertia(self.request, self.component_name, self.props, self.template_name)
98-

test.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,33 @@
77
from django.http import HttpResponse, HttpResponseRedirect
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()
19-
from inertia.version import asset_version
23+
from inertia.version import get_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()
2735
request = requestfactory.get("/")
36+
self.set_session(request)
2837
response = render_inertia(request, "Index")
2938
self.assertTrue(b'id="page"' in response.content)
3039

@@ -46,7 +55,7 @@ def test_middlware_missing_header(self):
4655
defaults = {
4756
'X-Inertia': 'true',
4857
'X-Requested-With': 'XMLHttpRequest',
49-
'X-Inertia-Version': str(asset_version.get_version()+1),
58+
'X-Inertia-Version': str(get_version()+1),
5059
}
5160
request = RequestFactory().get("/")
5261
request.headers = defaults
@@ -58,7 +67,7 @@ def test_middleware(self):
5867
view = lambda request: HttpResponse()
5968
defaults = {
6069
'x-Inertia': 'true',
61-
'X-Inertia-Version': asset_version.get_version(),
70+
'X-Inertia-Version': get_version(),
6271
'x-Requested-With': 'XMLHttpRequest'
6372
}
6473
request = RequestFactory().get("/", **defaults)
@@ -67,6 +76,14 @@ def test_middleware(self):
6776
response = InertiaMiddleware(view)(request)
6877
self.assertTrue(response.status_code == 200, response.status_code)
6978

79+
def test_share_custom_data(self):
80+
requestfactory = RequestFactory()
81+
request = requestfactory.get("/")
82+
self.set_session(request)
83+
response = render_inertia(request, "Index")
84+
self.assertDictEqual({"custom_data": "custom_value"}, request.session["share"])
85+
# self.assertTrue(b'share_custom_value"' in response.content)
86+
7087
def test_redirect_303_for_put_patch_delete_requests(self):
7188
request = RequestFactory().put("/users/1")
7289
self.set_session(request)

0 commit comments

Comments
 (0)