inertia-django-utils 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Bellawatt
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,28 @@
1
+ Metadata-Version: 2.1
2
+ Name: inertia-django-utils
3
+ Version: 0.1.0
4
+ Summary: Django Utilities for Inertia.js
5
+ Author-Email: Paul Bailey <paul@neutron.studio>
6
+ License: BSD3Clause
7
+ Requires-Python: >=3.11
8
+ Description-Content-Type: text/markdown
9
+
10
+ # Inertia Django Utils
11
+
12
+ Django Utilities for Inertia.js
13
+
14
+ ## Installation
15
+
16
+ `pdm add inertia-django-utils`
17
+
18
+ ## Login Required Decorator
19
+
20
+ Decorator checks if the user is authenticated and redirects to a login page. Works like the standard `@login_required` except that it uses the Inertia `location` redirect if an Inertia request is detected.
21
+
22
+ ```python
23
+ from inertia_utils import login_required
24
+
25
+ @login_required
26
+ def my_awesome_view(request):
27
+ return InertiaResponse(request, 'my_awesome_page', props={})
28
+ ```
@@ -0,0 +1,19 @@
1
+ # Inertia Django Utils
2
+
3
+ Django Utilities for Inertia.js
4
+
5
+ ## Installation
6
+
7
+ `pdm add inertia-django-utils`
8
+
9
+ ## Login Required Decorator
10
+
11
+ Decorator checks if the user is authenticated and redirects to a login page. Works like the standard `@login_required` except that it uses the Inertia `location` redirect if an Inertia request is detected.
12
+
13
+ ```python
14
+ from inertia_utils import login_required
15
+
16
+ @login_required
17
+ def my_awesome_view(request):
18
+ return InertiaResponse(request, 'my_awesome_page', props={})
19
+ ```
@@ -0,0 +1,22 @@
1
+ [project]
2
+ name = "inertia-django-utils"
3
+ version = "0.1.0"
4
+ description = "Django Utilities for Inertia.js"
5
+ authors = [
6
+ { name = "Paul Bailey", email = "paul@neutron.studio" },
7
+ ]
8
+ dependencies = []
9
+ requires-python = ">=3.11"
10
+ readme = "README.md"
11
+
12
+ [project.license]
13
+ text = "BSD3Clause"
14
+
15
+ [build-system]
16
+ requires = [
17
+ "pdm-backend",
18
+ ]
19
+ build-backend = "pdm.backend"
20
+
21
+ [tool.pdm]
22
+ distribution = true
@@ -0,0 +1,2 @@
1
+ from .common import is_inertia_request
2
+ from .auth import login_required
@@ -0,0 +1,70 @@
1
+ from functools import wraps
2
+ from inspect import isawaitable, iscoroutinefunction
3
+ from urllib.parse import quote
4
+
5
+ from asgiref.sync import async_to_sync
6
+
7
+ from inertia import location
8
+
9
+ from django.conf import settings
10
+ from django import http
11
+
12
+ from .common import is_inertia_request
13
+
14
+
15
+ REDIRECT_FIELD_NAME = getattr(settings, 'AUTH_REDIRECT_FIELD_NAME', 'next')
16
+ LOGIN_URL = settings.LOGIN_URL
17
+
18
+
19
+ async def run_test(request, test_func, redirect_field_name, login_url):
20
+ user = await request.auser()
21
+ passed = test_func(user)
22
+
23
+ if isawaitable(passed):
24
+ passed = await passed
25
+
26
+ if not passed:
27
+ nxt = request.get_full_path()
28
+ nxt = quote(nxt, safe='')
29
+ url = f'{login_url}?{redirect_field_name}={nxt}'
30
+
31
+ if is_inertia_request(request):
32
+ return location(url)
33
+
34
+ return http.HttpResponseRedirect(url)
35
+
36
+
37
+ def user_passes_test(test_func, redirect_field_name=REDIRECT_FIELD_NAME, login_url=LOGIN_URL):
38
+ def decorator(view_func):
39
+ if iscoroutinefunction(view_func):
40
+ async def view_wrapper(request, *args, **kwargs):
41
+ response = await run_test(request, test_func, redirect_field_name, login_url)
42
+ if response:
43
+ return response
44
+
45
+ return await view_func(request, *args, **kwargs)
46
+
47
+ else:
48
+ def view_wrapper(request, *args, **kwargs):
49
+ response = async_to_sync(run_test)(request, test_func, redirect_field_name, login_url)
50
+ if response:
51
+ return response
52
+
53
+ return view_func(request, *args, **kwargs)
54
+
55
+ return wraps(view_func)(view_wrapper)
56
+
57
+
58
+ return decorator
59
+
60
+
61
+ def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=LOGIN_URL):
62
+ actual_decorator = user_passes_test(
63
+ lambda u: u.is_authenticated,
64
+ redirect_field_name,
65
+ login_url,
66
+ )
67
+ if function:
68
+ return actual_decorator(function)
69
+
70
+ return actual_decorator
@@ -0,0 +1,2 @@
1
+ def is_inertia_request(request):
2
+ return "X-Inertia" in request.headers
File without changes