-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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/).
- Loading branch information
1 parent
6f79c4c
commit b307b3e
Showing
59 changed files
with
1,165 additions
and
356 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from reactpy import component, html | ||
from reactpy_django.hooks import use_user_data | ||
|
||
|
||
@component | ||
def my_component(): | ||
user_data = use_user_data( | ||
default_data={ | ||
"basic_example": "123", | ||
"computed_example_sync": sync_default, | ||
"computed_example_async": async_default, | ||
} | ||
) | ||
|
||
return html.div( | ||
html.div(f"Data: {user_data.query.data}"), | ||
) | ||
|
||
|
||
def sync_default(): | ||
return ... | ||
|
||
|
||
async def async_default(): | ||
return ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from reactpy import component, html | ||
from reactpy_django.hooks import use_user_data | ||
|
||
|
||
@component | ||
def my_component(): | ||
query, mutation = use_user_data() | ||
|
||
def on_submit(event): | ||
if event["key"] == "Enter" and query.data: | ||
new_key = str(len(query.data)) | ||
mutation({**query.data, new_key: event["target"]["value"]}) | ||
|
||
return html.div( | ||
html.div(f"Data: {query.data}"), | ||
html.div(f"Loading: {query.loading | mutation.loading}"), | ||
html.div(f"Error(s): {query.error} {mutation.error}"), | ||
html.input({"on_key_press": on_submit}), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from reactpy import component, html | ||
from reactpy_django.hooks import use_user | ||
|
||
|
||
@component | ||
def my_component(): | ||
user = use_user() | ||
|
||
return html.div(user.username) |
8 changes: 6 additions & 2 deletions
8
...ython/auth-required-component-fallback.py → ...on/user-passes-test-component-fallback.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
from reactpy import component, html | ||
from reactpy_django.decorators import auth_required | ||
from reactpy_django.decorators import user_passes_test | ||
|
||
|
||
@component | ||
def my_component_fallback(): | ||
return html.div("I am NOT logged in!") | ||
|
||
|
||
def auth_check(user): | ||
return user.is_authenticated | ||
|
||
|
||
@user_passes_test(auth_check, fallback=my_component_fallback) | ||
@component | ||
@auth_required(fallback=my_component_fallback) | ||
def my_component(): | ||
return html.div("I am logged in!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from reactpy import component, html | ||
from reactpy_django.decorators import user_passes_test | ||
|
||
|
||
def auth_check(user): | ||
return user.is_authenticated | ||
|
||
|
||
@user_passes_test(auth_check, fallback=html.div("I am NOT logged in!")) | ||
@component | ||
def my_component(): | ||
return html.div("I am logged in!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from reactpy import component, html | ||
from reactpy_django.decorators import user_passes_test | ||
|
||
|
||
def auth_check(user): | ||
return user.is_authenticated | ||
|
||
|
||
@user_passes_test(auth_check) | ||
@component | ||
def my_component(): | ||
return html.div("I am logged in!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.