forked from Daxexs/flet-easy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpagesy.py
192 lines (158 loc) · 6.74 KB
/
pagesy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from collections import deque
from dataclasses import dataclass
from functools import wraps
from typing import Any, Callable, Dict, List, Optional
from flet import View
from flet_easy.datasy import Datasy
from flet_easy.extra import Redirect
MiddlewareHandler = Callable[[Datasy], Optional[Redirect]]
Middleware = List[MiddlewareHandler]
ViewHandler = Callable[[Datasy], View]
@dataclass
class Pagesy:
"""To add pages, it requires the following parameters:
* `route`: text string of the url, for example(`'/task'`).
* `view`: Stores the page function.
* `title` : Define the title of the page.
* `clear`: Removes the pages from the `page.views` list of flet. (optional)
* `share_data` : It is a boolean value, which is useful if you want to share data between pages, in a more restricted way. (optional)
* `protected_route`: Protects the route of the page, according to the configuration of the `login` decorator of the `FletEasy` class. (optional)
* `custom_params`: To add validation of parameters in the custom url using a list, where the key is the name of the parameter validation and the value is the custom function that must report a boolean value.
* `middleware` : It acts as an intermediary between different software components, intercepting and processing requests and responses. They allow adding functionalities to an application in a flexible and modular way. (optional)
Example:
```python
Pagesy("/test/{id:d}/user/{name:l}", test_page, protected_route=True)
```
"""
route: str
view: ViewHandler
title: str = None
clear: bool = False
share_data: bool = False
protected_route: bool = False
custom_params: Dict[str, Callable[[], bool]] = None
middleware: Middleware = None
class AddPagesy:
"""Creates an object to then add to the list of the `add_routes` method of the `FletEasy` class.
-> Requires the parameter:
* route_prefix: text string that will bind to the url of the `page` decorator, example(`/users`) this will encompass all urls of this class. (optional)
Example:
```python
users = fs.AddPagesy(route_prefix="/user")
# -> Urls to be created:
# * '/user/task'
# * '/user/information'
@users.page("/task")
async def task_page(data: fs.Datasy):
page = data.page
page.title = "Task"
return ft.View(
route="/users/task",
controls=[
ft.Text("Task"),
],
vertical_alignment=view.vertical_alignment,
horizontal_alignment=view.horizontal_alignment,
)
@users.page("/information")
async def information_page(data: fs.Datasy):
page = data.page
page.title = "Information"
return ft.View(
route="/users/information",
controls=[
ft.Text("Information"),
],
vertical_alignment=view.vertical_alignment,
horizontal_alignment=view.horizontal_alignment,
)
```
"""
def __init__(self, route_prefix: str = None):
self.route_prefix = route_prefix
self.__pages = deque()
def __decorator(self, data: Dict = None):
def decorator(func: Callable):
@wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
route = (
(
self.route_prefix
if data.get("route") == "/"
else self.route_prefix + data.get("route")
)
if self.route_prefix
else data.get("route")
)
self.__pages.append(
Pagesy(
route=route,
view=func,
title=data.get("title"),
clear=data.get("page_clear"),
share_data=data.get("share_data"),
protected_route=data.get("protected_route"),
custom_params=data.get("custom_params"),
middleware=data.get("middleware"),
)
)
return wrapper
return decorator
def page(
self,
route: str,
title: str = None,
page_clear: bool = False,
share_data: bool = False,
protected_route: bool = False,
custom_params: Dict[str, Any] = None,
middleware: Middleware = None,
):
"""Decorator to add a new page to the app, you need the following parameters:
* route: text string of the url, for example(`'/counter'`).
* `title` : Define the title of the page. (optional).
* clear: Removes the pages from the `page.views` list of flet. (optional)
* `share_data` : It is a boolean value, which is useful if you want to share data between pages, in a more restricted way. (optional)
* protected_route: Protects the route of the page, according to the configuration of the `login` decorator of the `FletEasy` class. (optional)
* custom_params: To add validation of parameters in the custom url using a list, where the key is the name of the parameter validation and the value is the custom function that must report a boolean value.
* `middleware` : It acts as an intermediary between different software components, intercepting and processing requests and responses. They allow adding functionalities to an application in a flexible and modular way. (optional)
-> The decorated function must receive a parameter, for example `data:fs.Datasy`.
Example:
```python
import flet as ft
import flet_easy as fs
counter = fs.AddPagesy(route_prefix="/counter")
@counter.page("/", title="Counter")
async def counter_page(data: fs.Datasy):
view = data.view
view.appbar.title = ft.Text("Counter")
return ft.View(
route="/counter",
controls=[
ft.Text("Counter"),
],
appbar=view.appbar,
vertical_alignment=view.vertical_alignment,
horizontal_alignment=view.horizontal_alignment,
)
```
"""
data = {
"route": route,
"title": title,
"page_clear": page_clear,
"share_data": share_data,
"protected_route": protected_route,
"custom_params": custom_params,
"middleware": middleware,
}
return self.__decorator(data)
def _add_pages(self, route: str = None) -> deque:
if route:
for page in self.__pages:
if page.route == "/":
page.route = route
else:
page.route = route + page.route
return self.__pages