Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add event token to router_data #316

Merged
merged 6 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pynecone/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ async def process(app: App, event: Event) -> StateUpdate:
state = app.state_manager.get_state(event.token)

state.router_data = event.router_data
state.router_data[constants.RouteVar.CLIENT_TOKEN] = event.token

# Preprocess the event.
pre = app.preprocess(state, event)
Expand Down
8 changes: 8 additions & 0 deletions pynecone/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,14 @@ class PathArgType(SimpleNamespace):
LIST = str("arg_list")


class RouteVar(SimpleNamespace):
"""Names of variables used in the router_data dict stored in State."""

CLIENT_TOKEN = "token"
PATH = "pathname"
QUERY = "query"


class RouteRegex(SimpleNamespace):
"""Regex used for extracting path args in path."""

Expand Down
12 changes: 10 additions & 2 deletions pynecone/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,21 @@ def _set_default_value(cls, prop: BaseVar):
field.required = False
field.default = default_value

def get_token(self) -> str:
"""Return the token of the client associated with this state.

Returns:
The token of the client.
"""
return self.router_data.get(constants.RouteVar.CLIENT_TOKEN, "")

def get_current_page(self) -> str:
"""Obtain the path of current page from the router data.

Returns:
The current page.
"""
return self.router_data.get("pathname", "")
return self.router_data.get(constants.RouteVar.PATH, "")

def get_query_params(self) -> Dict[str, str]:
"""Obtain the query parameters for the queried page.
Expand All @@ -277,7 +285,7 @@ def get_query_params(self) -> Dict[str, str]:
Returns:
The dict of query parameters.
"""
return self.router_data.get("query", {})
return self.router_data.get(constants.RouteVar.QUERY, {})

@classmethod
def setup_dynamic_args(cls, args: dict[str, str]):
Expand Down
29 changes: 29 additions & 0 deletions tests/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pynecone.event import Event
from pynecone.state import State
from pynecone.var import BaseVar, ComputedVar
from pynecone.constants import RouteVar


class Object(Base):
Expand Down Expand Up @@ -609,3 +610,31 @@ def test_format_event_handler():
utils.format_event_handler(GrandchildState.do_nothing) # type: ignore
== "test_state.child_state.grandchild_state.do_nothing"
)


def test_get_token(test_state):
assert test_state.get_token() == ""

token = "b181904c-3953-4a79-dc18-ae9518c22f05"
test_state.router_data = {RouteVar.CLIENT_TOKEN: token}

assert test_state.get_token() == token


def test_get_current_page(test_state):

assert test_state.get_current_page() == ""

route = "mypage/subpage"
test_state.router_data = {RouteVar.PATH: route}

assert test_state.get_current_page() == route


def test_get_query_params(test_state):
assert test_state.get_query_params() == {}

params = {"p1": "a", "p2": "b"}
test_state.router_data = {RouteVar.QUERY: params}

assert test_state.get_query_params() == params