Skip to content

Commit 4d87795

Browse files
48 - Multiple Ways to Associate Users to Data Models
1 parent 3d73016 commit 4d87795

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

full_stack_python/auth/state.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010

1111
class SessionState(reflex_local_auth.LocalAuthState):
1212

13+
@rx.cached_var
14+
def my_user_id(self) -> str | None:
15+
if self.authenticated_user.id < 0:
16+
return None
17+
return self.authenticated_user.id
18+
1319
@rx.cached_var
1420
def authenticated_username(self) -> str | None:
1521
if self.authenticated_user.id < 0:

full_stack_python/contact/form.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
import reflex as rx
22

3-
3+
from ..auth.state import SessionState
44
from .state import ContactState
55

66
def contact_form() -> rx.Component:
77
return rx.form(
8+
# rx.cond(
9+
# SessionState.my_user_id,
10+
# rx.box(
11+
# rx.input(
12+
# type='hidden',
13+
# name='user_id',
14+
# value=SessionState.my_user_id
15+
# ),
16+
# display='none'
17+
# ),
18+
# rx.fragment('')
19+
# ),
820
rx.vstack(
921
rx.hstack(
1022
rx.input(

full_stack_python/contact/page.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import reflex as rx
22

3+
from ..auth.state import SessionState
34
from .. import navigation
45
from ..ui.base import base_page
56

@@ -31,6 +32,7 @@ def contact_entries_list_page() ->rx.Component:
3132
)
3233

3334
def contact_page() -> rx.Component:
35+
3436
my_child = rx.vstack(
3537
rx.heading("Contact Us", size="9"),
3638
rx.cond(state.ContactState.did_submit, state.ContactState.thank_you, ""),

full_stack_python/contact/state.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
from sqlmodel import select
66
from .model import ContactEntryModel
77

8-
class ContactState(rx.State):
8+
from ..auth.state import SessionState
9+
10+
class ContactState(SessionState):
911
form_data: dict = {}
1012
entries: List['ContactEntryModel'] = []
1113
did_submit: bool = False
@@ -24,6 +26,9 @@ async def handle_submit(self, form_data: dict):
2426
if v == "" or v is None:
2527
continue
2628
data[k] = v
29+
if self.my_user_id is not None:
30+
data['user_id'] = self.my_user_id
31+
print("contact data", data)
2732
with rx.session() as session:
2833
db_entry = ContactEntryModel(
2934
**data

0 commit comments

Comments
 (0)