Repository navigation
Expand file tree
/
Copy pathinputs.py
More file actions
280 lines (214 loc) · 6.96 KB
/
Copy pathinputs.py
File metadata and controls
280 lines (214 loc) · 6.96 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import abc
import json
from enum import Enum as EnumCLS
from typing import Any, List, Optional, Tuple, Type
from starlette.datastructures import UploadFile
from starlette.requests import Request
from tortoise import Model
from fastapi_admin.file_upload import FileUpload
from fastapi_admin.widgets import Widget
class Input(Widget):
template = "widgets/inputs/input.html"
def __init__(
self, help_text: Optional[str] = None, default: Any = None, null: bool = False, **context
):
super().__init__(null=null, help_text=help_text, **context)
self.default = default
async def parse_value(self, request: Request, value: Any):
"""
Parse value from frontend
:param value:
:return:
"""
return value
async def render(self, request: Request, value: Any):
if value is None:
value = self.default
return await super(Input, self).render(request, value)
class DisplayOnly(Input):
"""
Only display without input in edit or create
"""
class Text(Input):
input_type: Optional[str] = "text"
def __init__(
self,
help_text: Optional[str] = None,
default: Any = None,
null: bool = False,
placeholder: str = "",
disabled: bool = False,
):
super().__init__(
null=null,
default=default,
input_type=self.input_type,
placeholder=placeholder,
disabled=disabled,
help_text=help_text,
)
class Select(Input):
template = "widgets/inputs/select.html"
def __init__(
self,
help_text: Optional[str] = None,
default: Any = None,
null: bool = False,
disabled: bool = False,
):
super().__init__(help_text=help_text, null=null, default=default, disabled=disabled)
@abc.abstractmethod
async def get_options(self):
"""
return list of tuple with display and value
[("on",1),("off",2)]
:return: list of tuple with display and value
"""
async def render(self, request: Request, value: Any):
options = await self.get_options()
self.context.update(options=options)
return await super(Select, self).render(request, value)
class ForeignKey(Select):
def __init__(
self,
model: Type[Model],
default: Any = None,
null: bool = False,
disabled: bool = False,
help_text: Optional[str] = None,
):
super().__init__(help_text=help_text, default=default, null=null, disabled=disabled)
self.model = model
async def get_options(self):
ret = await self.get_queryset()
options = [(str(x), x.pk) for x in ret]
if self.context.get("null"):
options = [("", "")] + options
return options
async def get_queryset(self):
return await self.model.all()
class ManyToMany(Select):
template = "widgets/inputs/many_to_many.html"
def __init__(
self,
model: Type[Model],
disabled: bool = False,
help_text: Optional[str] = None,
):
super().__init__(help_text=help_text, disabled=disabled)
self.model = model
async def get_options(self):
ret = await self.get_queryset()
options = [dict(label=str(x), value=x.pk) for x in ret]
return options
async def get_queryset(self):
return await self.model.all()
async def render(self, request: Request, value: Any):
options = await self.get_options()
selected = list(map(lambda x: x.pk, value.related_objects if value else []))
for option in options:
if option.get("value") in selected:
option["selected"] = True
self.context.update(options=json.dumps(options))
return await super(Input, self).render(request, value)
class Enum(Select):
def __init__(
self,
enum: Type[EnumCLS],
default: Any = None,
enum_type: Type = int,
null: bool = False,
disabled: bool = False,
help_text: Optional[str] = None,
):
super().__init__(help_text=help_text, default=default, null=null, disabled=disabled)
self.enum = enum
self.enum_type = enum_type
async def parse_value(self, request: Request, value: Any):
return self.enum(self.enum_type(value))
async def get_options(self):
options = [(v.name, v.value) for v in self.enum]
if self.context.get("null"):
options = [("", "")] + options
return options
class Email(Text):
input_type = "email"
class Json(Input):
template = "widgets/inputs/json.html"
def __init__(
self,
help_text: Optional[str] = None,
null: bool = False,
options: Optional[dict] = None,
):
"""
options config to jsoneditor, see https://github.com/josdejong/jsoneditor
:param options:
"""
super().__init__(null=null, help_text=help_text)
if not options:
options = {}
self.context.update(options=options)
async def render(self, request: Request, value: Any):
if value:
value = json.dumps(value)
return await super().render(request, value)
class TextArea(Text):
template = "widgets/inputs/textarea.html"
input_type = "textarea"
class DateTime(Text):
input_type = "datetime"
class Date(Text):
input_type = "date"
class File(Input):
input_type = "file"
def __init__(
self,
upload: FileUpload,
default: Any = None,
null: bool = False,
disabled: bool = False,
help_text: Optional[str] = None,
):
super().__init__(
null=null,
default=default,
input_type=self.input_type,
disabled=disabled,
help_text=help_text,
)
self.upload = upload
async def parse_value(self, request: Request, value: Optional[UploadFile]):
if value and value.filename:
return await self.upload.upload(value)
return ""
class Image(File):
template = "widgets/inputs/image.html"
input_type = "file"
class Radio(Select):
template = "widgets/inputs/radio.html"
def __init__(
self,
options: List[Tuple[str, Any]],
help_text: Optional[str] = None,
default: Any = None,
disabled: bool = False,
):
super().__init__(default=default, disabled=disabled, help_text=help_text)
self.options = options
async def get_options(self):
return self.options
class RadioEnum(Enum):
template = "widgets/inputs/radio.html"
class Switch(Input):
template = "widgets/inputs/switch.html"
async def parse_value(self, request: Request, value: str):
if value == "on":
return True
return False
class Password(Text):
input_type = "password"
class Number(Text):
input_type = "number"
class Color(Text):
template = "widgets/inputs/color.html"