Skip to content

Commit b307b3e

Browse files
authored
UserModel related hooks, decorators, and settings (#190)
- New Django `User` related features! - `reactpy_django.hooks.use_user` can be used to access the current user. - `reactpy_django.hooks.use_user_data` provides a simplified interface for storing user key-value data. - `reactpy_django.decorators.user_passes_test` is inspired by Django's [`user_passes_test`](http://docs.djangoproject.com/en/dev/topics/auth/default/#django.contrib.auth.decorators.user_passes_test) decorator, but works with ReactPy components. - `settings.py:REACTPY_AUTO_RELOGIN` will cause component WebSocket connections to automatically [re-login](https://channels.readthedocs.io/en/latest/topics/authentication.html#how-to-log-a-user-in-out) users that are already authenticated. This is useful to continuously update `last_login` timestamps and refresh the [Django login session](https://docs.djangoproject.com/en/dev/topics/http/sessions/).
1 parent 6f79c4c commit b307b3e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1165
-356
lines changed

CHANGELOG.md

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,38 @@ Using the following categories, list your changes in this order:
3636

3737
### Added
3838

39-
- ReactPy components can now use SEO compatible rendering!
40-
- `settings.py:REACTPY_PRERENDER` can be set to `True` to enable this behavior by default
41-
- Or, you can enable it on individual components via the template tag: `{% component "..." prerender="True" %}`
42-
- `reactpy_django.components.view_to_iframe` component has been added, which uses an `<iframe>` to render a Django view.
43-
- `reactpy_django.utils.register_iframe` function has been added, which is mandatory to use alongside `reactpy_django.components.view_to_iframe`.
39+
- SEO compatible rendering!
40+
- `settings.py:REACTPY_PRERENDER` can be set to `True` to make components pre-render by default.
41+
- Or, you can enable it on individual components via the template tag: `{% component "..." prerender="True" %}`.
42+
- New `view_to_iframe` feature!
43+
- `reactpy_django.components.view_to_iframe` uses an `<iframe>` to render a Django view.
44+
- `reactpy_django.utils.register_iframe` tells ReactPy which views `view_to_iframe` can use.
45+
- New Django `User` related features!
46+
- `reactpy_django.hooks.use_user` can be used to access the current user.
47+
- `reactpy_django.hooks.use_user_data` provides a simplified interface for storing user key-value data.
48+
- `reactpy_django.decorators.user_passes_test` is inspired by the [equivalent Django decorator](http://docs.djangoproject.com/en/dev/topics/auth/default/#django.contrib.auth.decorators.user_passes_test), but ours works with ReactPy components.
49+
- `settings.py:REACTPY_AUTO_RELOGIN` will cause component WebSocket connections to automatically [re-login](https://channels.readthedocs.io/en/latest/topics/authentication.html#how-to-log-a-user-in-out) users that are already authenticated. This is useful to continuously update `last_login` timestamps and refresh the [Django login session](https://docs.djangoproject.com/en/dev/topics/http/sessions/).
4450

4551
### Changed
4652

47-
- Renamed undocumented utility function `reactpy_django.utils.ComponentPreloader` to `reactpy_django.utils.RootComponentFinder`.
53+
- Renamed undocumented utility function `ComponentPreloader` to `RootComponentFinder`.
4854
- It is now recommended to call `as_view()` when using `view_to_component` or `view_to_iframe` with Class Based Views.
49-
- Thread sensitivity has been enabled in all locations where ORM queries are possible.
55+
- For thread safety, `thread_sensitive=True` has been enabled in all `sync_to_async` functions where ORM queries are possible.
56+
- `reactpy_django.hooks.use_mutation` now has a `__call__` method. So rather than writing `my_mutation.execute(...)`, you can now write `my_mutation(...)`.
5057

5158
### Deprecated
5259

53-
- The `compatibility` argument on `reactpy_django.components.view_to_component` is deprecated. Use `reactpy_django.components.view_to_iframe` instead.
54-
- Using `reactpy_django.components.view_to_component` as a decorator is deprecated. Check the docs on the new suggested usage.
60+
- The `compatibility` argument on `reactpy_django.components.view_to_component` is deprecated.
61+
- Use `view_to_iframe` as a replacement.
62+
- `reactpy_django.components.view_to_component` **usage as a decorator** is deprecated.
63+
- Check the docs on how to use `view_to_component` as a function instead.
64+
- `reactpy_django.decorators.auth_required` is deprecated.
65+
- Use `reactpy_django.decorators.user_passes_test` instead.
66+
- An equivalent to `auth_required`'s default is `@user_passes_test(lambda user: user.is_active)`.
67+
68+
### Fixed
69+
70+
- Fixed a bug where exception stacks would not print on failed component renders.
5571

5672
## [3.5.1] - 2023-09-07
5773

docs/overrides/home.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ <h1>{{ config.site_name }}</h1>
5858
<a href="{{ 'reference/components/' | url }}" class="md-button">
5959
API Reference
6060
</a>
61+
<a href="{{ 'about/changelog/' | url }}" class="md-button">
62+
Changelog
63+
</a>
6164
</div>
6265
</div>
6366

docs/python/auth-required-attribute.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

docs/python/auth-required-custom-attribute-model.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

docs/python/auth-required-custom-attribute.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

docs/python/auth-required-vdom-fallback.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

docs/python/auth-required.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

docs/python/configure-asgi-middleware.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@
77

88
# start
99
from channels.auth import AuthMiddlewareStack # noqa: E402
10-
from channels.sessions import SessionMiddlewareStack # noqa: E402
1110

1211
application = ProtocolTypeRouter(
1312
{
1413
"http": django_asgi_app,
15-
"websocket": SessionMiddlewareStack(
16-
AuthMiddlewareStack(
17-
URLRouter(
18-
[REACTPY_WEBSOCKET_ROUTE],
19-
)
14+
"websocket": AuthMiddlewareStack(
15+
URLRouter(
16+
[REACTPY_WEBSOCKET_ROUTE],
2017
)
2118
),
2219
}

docs/python/use-connection.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44

55
@component
66
def my_component():
7-
my_connection = use_connection()
8-
return html.div(str(my_connection))
7+
connection = use_connection()
8+
9+
return html.div(str(connection))

docs/python/use-location.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44

55
@component
66
def my_component():
7-
my_location = use_location()
8-
return html.div(str(my_location))
7+
location = use_location()
8+
9+
return html.div(str(location))

0 commit comments

Comments
 (0)