-
Notifications
You must be signed in to change notification settings - Fork 34
/
endpoints.py
1252 lines (1064 loc) · 44.3 KB
/
endpoints.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Creates a basic wrapper around a Piccolo model, turning it into an ASGI app.
"""
from __future__ import annotations
import inspect
import itertools
import json
import logging
import os
import typing as t
from dataclasses import dataclass
from datetime import timedelta
from functools import partial
from fastapi import FastAPI, File, Form, UploadFile
from piccolo.apps.user.tables import BaseUser
from piccolo.columns.base import Column
from piccolo.columns.column_types import (
ForeignKey,
Time,
Timestamp,
Timestamptz,
)
from piccolo.columns.reference import LazyTableReference
from piccolo.table import Table
from piccolo.utils.warnings import Level, colored_warning
from piccolo_api.change_password.endpoints import change_password
from piccolo_api.crud.endpoints import OrderBy, PiccoloCRUD
from piccolo_api.crud.hooks import Hook
from piccolo_api.crud.validators import Validators
from piccolo_api.csp.middleware import CSPConfig, CSPMiddleware
from piccolo_api.csrf.middleware import CSRFMiddleware
from piccolo_api.fastapi.endpoints import FastAPIKwargs, FastAPIWrapper
from piccolo_api.media.base import MediaStorage
from piccolo_api.media.local import LocalMediaStorage
from piccolo_api.openapi.endpoints import swagger_ui
from piccolo_api.rate_limiting.middleware import (
InMemoryLimitProvider,
RateLimitingMiddleware,
RateLimitProvider,
)
from piccolo_api.session_auth.endpoints import session_login, session_logout
from piccolo_api.session_auth.middleware import SessionsAuthBackend
from piccolo_api.session_auth.tables import SessionsBase
from pydantic import BaseModel, Field, ValidationError
from starlette.middleware import Middleware
from starlette.middleware.authentication import AuthenticationMiddleware
from starlette.middleware.exceptions import HTTPException
from starlette.requests import Request
from starlette.responses import HTMLResponse, JSONResponse
from starlette.staticfiles import StaticFiles
from .translations.data import TRANSLATIONS
from .translations.models import (
Translation,
TranslationListItem,
TranslationListResponse,
)
from .version import __VERSION__ as PICCOLO_ADMIN_VERSION
logger = logging.getLogger(__name__)
ASSET_PATH = os.path.join(os.path.dirname(__file__), "dist")
class UserResponseModel(BaseModel):
username: str
user_id: str
class MetaResponseModel(BaseModel):
piccolo_admin_version: str
site_name: str
class StoreFileResponseModel(BaseModel):
file_key: str = Field(description="For example `my_file-some-uuid.jpeg`.")
class GenerateFileURLRequestModel(BaseModel):
column_name: str
table_name: str
file_key: str = Field(description="For example `my_file-some-uuid.jpeg`.")
class GenerateFileURLResponseModel(BaseModel):
file_url: str = Field(description="A URL which the file is accessible on.")
class GroupedTableNamesResponseModel(BaseModel):
grouped: t.Dict[str, t.List[str]] = Field(default_factory=list)
ungrouped: t.List[str] = Field(default_factory=list)
@dataclass
class TableConfig:
"""
Gives the user more control over how a ``Table`` appears in the UI.
:param table_class:
The :class:`Table <piccolo.table.Table>` class to configure.
:param visible_columns:
If specified, only these columns will be shown in the list view of the
UI. This is useful when you have a lot of columns.
:param exclude_visible_columns:
You can specify this instead of ``visible_columns``, in which case all
of the ``Table`` columns except the ones specified will be shown in the
list view.
:param visible_filters:
If specified, only these columns will be shown in the filter sidebar
of the UI. This is useful when you have a lot of columns.
:param exclude_visible_filters:
You can specify this instead of ``visible_filters``, in which case all
of the ``Table`` columns except the ones specified will be shown in the
filter sidebar.
:param rich_text_columns:
You can specify ``rich_text_columns`` if you want a WYSIWYG editor
on certain Piccolo :class:`Text <piccolo.columns.column_types.Text>`
columns. Any columns not specified will use a standard HTML textarea
tag in the UI.
:param hooks:
These are passed directly to
:class:`PiccoloCRUD <piccolo_api.crud.endpoints.PiccoloCRUD>`, which
powers Piccolo Admin under the hood. It allows you to run custom logic
when a row is modified.
:param media_storage:
These columns will be used to store media. We don't directly store the
media in the database, but instead store a string, which is a unique
identifier, and can be used to retrieve a URL for accessing the file.
Piccolo Admin automatically renders a file upload widget for each media
column in the UI.
:param validators:
The :class:`Validators <piccolo_api.crud.endpoints.Validators>` are
passed directly to
:class:`PiccoloCRUD <piccolo_api.crud.endpoints.PiccoloCRUD>`, which
powers Piccolo Admin under the hood. It allows fine grained access
control over each API endpoint. For example, limiting which users can
``POST`` data::
from piccolo_api.crud.endpoints import PiccoloCRUD
from starlette.exceptions import HTTPException
from starlette.requests import Request
async def manager_only(
piccolo_crud: PiccoloCRUD,
request: Request
):
# The Piccolo `BaseUser` can be accessed from the request.
user = request.user.user
# Assuming we have another database table where we record
# users with certain permissions.
manager = await Manager.exists().where(manager.user == user)
if not manager:
# Raise a Starlette exception if we want to reject the
# request.
raise HTTPException(
status_code=403,
detail="Only managers are allowed to do this"
)
admin = create_admin(
tables=TableConfig(
Movie,
validators=Validators(post_single=[manager_only])
)
)
:param menu_group:
If specified, tables can be divided into groups in the table
menu. This is useful when you have many tables that you
can organize into groups for better visibility.
:param link_column:
In the list view of Piccolo Admin, we use the primary key to link to
the edit page. However, if the primary key column is hidden, due to
``visible_columns`` or ``exclude_visible_columns``, then we need to
specify an alternative column to use as the link.
:param order_by:
If specified, the rows are sorted by ``order_by``, otherwise
the default ``primary_key`` column is used to sort the rows.
:param time_resolution:
Controls the resolution of ``Time`` columns, and the time component
of ``Timestamp`` / ``Timestamptz`` columns. The units are given in
seconds. Some examples:
* 0.001 - the max resolution is 1 millisecond (this is the minimum
currently allowed by HTML input fields)
* 1 - the max resolution is 1 second (the default)
* 60 - the max resolution is 1 minute
"""
table_class: t.Type[Table]
visible_columns: t.Optional[t.List[Column]] = None
exclude_visible_columns: t.Optional[t.List[Column]] = None
visible_filters: t.Optional[t.List[Column]] = None
exclude_visible_filters: t.Optional[t.List[Column]] = None
rich_text_columns: t.Optional[t.List[Column]] = None
hooks: t.Optional[t.List[Hook]] = None
media_storage: t.Optional[t.Sequence[MediaStorage]] = None
validators: t.Optional[Validators] = None
menu_group: t.Optional[str] = None
link_column: t.Optional[Column] = None
order_by: t.Optional[t.List[OrderBy]] = None
time_resolution: t.Optional[
t.Dict[t.Union[Timestamp, Timestamptz, Time], t.Union[float, int]]
] = None
def __post_init__(self):
if self.visible_columns and self.exclude_visible_columns:
raise ValueError(
"Only specify `visible_columns` or "
"`exclude_visible_columns`."
)
if self.visible_filters and self.exclude_visible_filters:
raise ValueError(
"Only specify `visible_filters` or `exclude_visible_filters`."
)
if isinstance(self.link_column, ForeignKey):
raise ValueError(
"Don't use a foreign key column for `link_column`, as they "
"are already displayed as a link in the UI."
)
# Create a mapping for faster lookups
self.media_columns = (
{i.column: i for i in self.media_storage}
if self.media_storage
else None
)
def _get_columns(
self,
include_columns: t.Optional[t.List[Column]],
exclude_columns: t.Optional[t.List[Column]],
all_columns: t.List[Column],
) -> t.List[Column]:
if include_columns and not exclude_columns:
return include_columns
if exclude_columns and not include_columns:
column_names = [i._meta.name for i in exclude_columns]
return [i for i in all_columns if i._meta.name not in column_names]
return all_columns
def get_visible_columns(self) -> t.List[Column]:
return self._get_columns(
include_columns=self.visible_columns,
exclude_columns=self.exclude_visible_columns,
all_columns=self.table_class._meta.columns,
)
def get_visible_column_names(self) -> t.Tuple[str, ...]:
return tuple(i._meta.name for i in self.get_visible_columns())
def get_visible_filters(self) -> t.List[Column]:
return self._get_columns(
include_columns=self.visible_filters,
exclude_columns=self.exclude_visible_filters,
all_columns=self.table_class._meta.columns,
)
def get_visible_filter_names(self) -> t.Tuple[str, ...]:
return tuple(i._meta.name for i in self.get_visible_filters())
def get_rich_text_columns_names(self) -> t.Tuple[str, ...]:
return (
tuple(i._meta.name for i in self.rich_text_columns)
if self.rich_text_columns
else ()
)
def get_media_columns_names(self) -> t.Tuple[str, ...]:
return (
tuple(i._meta.name for i in self.media_columns)
if self.media_columns
else ()
)
def get_link_column(self) -> Column:
return self.link_column or self.table_class._meta.primary_key
def get_order_by(self) -> t.List[OrderBy]:
return self.order_by or [
OrderBy(column=self.table_class._meta.primary_key, ascending=True)
]
def get_time_resolution(self) -> t.Dict[str, t.Union[int, float]]:
return (
{
column._meta.name: resolution
for column, resolution in self.time_resolution.items()
}
if self.time_resolution
else {}
)
PydanticModel = t.TypeVar("PydanticModel", bound=BaseModel)
@dataclass
class FormConfig:
"""
Used to specify forms, which are passed into ``create_admin``.
:param name:
This will be displayed in the UI in the sidebar.
:param pydantic_model:
This determines which fields to display in the form, and is used to
deserialise the responses.
:param endpoint:
Your custom handler, which accepts two arguments - the FastAPI /
Starlette request object, in case you want to access the cookies /
headers / logged in user (via `request.user`). And secondly an instance
of the Pydantic model. If it returns a string, it will be shown to
the user in the UI as the success message. For example ``'Successfully
sent email'``. The endpoint can be a normal function or async function.
:param description:
An optional description which is shown in the UI to explain to the user
what the form is for.
Here's a full example:
.. code-block:: python
class MyModel(pydantic.BaseModel):
message: str = "hello world"
def my_endpoint(request: Request, data: MyModel):
print(f"I received {data.message}")
# If we're not happy with the data raise a ValueError
# The message inside the exception will be displayed in the UI.
raise ValueError("We were unable to process the form.")
# If we're happy with the data, just return a string, which
# will be displayed in the UI.
return "Successful."
config = FormConfig(
name="My Form",
pydantic_model=MyModel,
endpoint=my_endpoint
)
"""
def __init__(
self,
name: str,
pydantic_model: t.Type[PydanticModel],
endpoint: t.Callable[
[Request, PydanticModel],
t.Union[str, None, t.Coroutine],
],
description: t.Optional[str] = None,
):
self.name = name
self.pydantic_model = pydantic_model
self.endpoint = endpoint
self.description = description
self.slug = self.name.replace(" ", "-").lower()
class FormConfigResponseModel(BaseModel):
name: str
slug: str
description: t.Optional[str] = None
def handle_auth_exception(request: Request, exc: Exception):
return JSONResponse({"error": "Auth failed"}, status_code=401)
def superuser_validators(piccolo_crud: PiccoloCRUD, request: Request):
"""
We need to provide extra validation on certain tables (e.g. user and
sessions), so only superusers can perform certain actions, otherwise the
security of the application can be compromised.
"""
user: BaseUser = request.user.user
if user.superuser:
return
if request.method.upper() in ["PUT", "PATCH", "DELETE", "POST"]:
raise HTTPException(
status_code=405,
detail="Only superusers can perform these actions.",
)
async def log_error(request: Request, exc: HTTPException):
logger.exception("Server error")
raise exc
class AdminRouter(FastAPI):
"""
The root returns a single page app. The other URLs are REST endpoints.
"""
table: t.List[Table] = []
auth_table: t.Type[BaseUser] = BaseUser
template: str = ""
def __init__(
self,
*tables: t.Union[t.Type[Table], TableConfig],
forms: t.List[FormConfig] = [],
auth_table: t.Type[BaseUser] = BaseUser,
session_table: t.Type[SessionsBase] = SessionsBase,
session_expiry: timedelta = timedelta(hours=1),
max_session_expiry: timedelta = timedelta(days=7),
increase_expiry: t.Optional[timedelta] = timedelta(minutes=20),
page_size: int = 15,
read_only: bool = False,
rate_limit_provider: t.Optional[RateLimitProvider] = None,
production: bool = False,
site_name: str = "Piccolo Admin",
default_language_code: str = "auto",
translations: t.Optional[t.List[Translation]] = None,
allowed_hosts: t.Sequence[str] = [],
debug: bool = False,
sidebar_links: t.Dict[str, str] = {},
) -> None:
super().__init__(
title=site_name,
description=f"{site_name} documentation",
middleware=[
Middleware(CSRFMiddleware, allowed_hosts=allowed_hosts)
],
debug=debug,
exception_handlers={500: log_error},
docs_url=None,
redoc_url=None,
)
#######################################################################
# Convert any table arguments which are plain ``Table`` classes into
# ``TableConfig`` instances.
table_configs: t.List[TableConfig] = []
for table in tables:
if isinstance(table, TableConfig):
table_configs.append(table)
else:
table_configs.append(TableConfig(table_class=table))
self.table_configs = sorted(
table_configs,
key=lambda table_config: table_config.table_class._meta.tablename,
)
self.table_config_map = {
table_config.table_class._meta.tablename: table_config
for table_config in self.table_configs
}
#######################################################################
# Make sure columns are configured properly.
for table_config in table_configs:
table_class = table_config.table_class
for column in table_class._meta.columns:
if column._meta.secret and column._meta.required:
message = (
f"{table_class._meta.tablename}."
f"{column._meta._name} is using `secret` and "
f"`required` column args which are incompatible. "
f"You may encounter unexpected behavior when using "
f"this table within Piccolo Admin."
)
colored_warning(message, level=Level.high)
#######################################################################
# Make sure media storage is configured properly.
media_storage = [
i
for i in itertools.chain(
*[
table_config.media_storage or []
for table_config in table_configs
]
)
]
if len(media_storage) != len(set(media_storage)):
raise ValueError(
"Media storage is misconfigured - multiple columns are saving "
"to the same location."
)
#######################################################################
self.default_language_code = default_language_code
self.translations_map = {
translation.language_code.lower(): translation
for translation in (translations or TRANSLATIONS)
}
#######################################################################
self.auth_table = auth_table
self.site_name = site_name
self.forms = forms
self.read_only = read_only
self.sidebar_links = sidebar_links
self.form_config_map = {form.slug: form for form in self.forms}
with open(os.path.join(ASSET_PATH, "index.html")) as f:
self.template = f.read()
#######################################################################
private_app = FastAPI(
docs_url=None,
redoc_url=None,
debug=debug,
exception_handlers={500: log_error},
)
private_app.mount("/docs/", swagger_ui(schema_url="../openapi.json"))
for table_config in table_configs:
table_class = table_config.table_class
visible_column_names = table_config.get_visible_column_names()
visible_filter_names = table_config.get_visible_filter_names()
rich_text_columns_names = (
table_config.get_rich_text_columns_names()
)
media_columns_names = table_config.get_media_columns_names()
link_column_name = table_config.get_link_column()._meta.name
order_by = table_config.get_order_by()
time_resolution = table_config.get_time_resolution()
validators = table_config.validators
if table_class in (auth_table, session_table):
validators = validators or Validators()
validators.every = [superuser_validators, *validators.every]
FastAPIWrapper(
root_url=f"/tables/{table_class._meta.tablename}/",
fastapi_app=private_app,
piccolo_crud=PiccoloCRUD(
table=table_class,
read_only=read_only,
page_size=page_size,
schema_extra={
"visible_column_names": visible_column_names,
"visible_filter_names": visible_filter_names,
"rich_text_columns": rich_text_columns_names,
"media_columns": media_columns_names,
"link_column_name": link_column_name,
"order_by": tuple(i.to_dict() for i in order_by),
"time_resolution": time_resolution,
},
validators=validators,
hooks=table_config.hooks,
),
fastapi_kwargs=FastAPIKwargs(
all_routes={
"tags": [f"{table_class._meta.tablename.capitalize()}"]
},
),
)
private_app.add_api_route(
path="/tables/",
endpoint=self.get_table_list, # type: ignore
methods=["GET"],
response_model=t.List[str],
tags=["Tables"],
)
private_app.add_api_route(
path="/tables/grouped/",
endpoint=self.get_table_list_grouped, # type: ignore
methods=["GET"],
response_model=GroupedTableNamesResponseModel,
tags=["Tables"],
)
private_app.add_api_route(
path="/links/",
endpoint=self.get_sidebar_links, # type: ignore
methods=["GET"],
tags=["Links"],
)
private_app.add_api_route(
path="/forms/",
endpoint=self.get_forms, # type: ignore
methods=["GET"],
tags=["Forms"],
response_model=t.List[FormConfigResponseModel],
)
private_app.add_api_route(
path="/forms/{form_slug:str}/",
endpoint=self.get_single_form, # type: ignore
methods=["GET"],
tags=["Forms"],
)
private_app.add_api_route(
path="/forms/{form_slug:str}/schema/",
endpoint=self.get_single_form_schema, # type: ignore
methods=["GET"],
tags=["Forms"],
)
private_app.add_api_route(
path="/forms/{form_slug:str}/",
endpoint=self.post_single_form, # type: ignore
methods=["POST"],
tags=["Forms"],
)
private_app.add_api_route(
path="/user/",
endpoint=self.get_user, # type: ignore
methods=["GET"],
tags=["User"],
response_model=UserResponseModel,
)
private_app.add_route(
path="/change-password/",
route=change_password(
login_url="./../../public/login/",
session_table=session_table,
read_only=read_only,
),
methods=["POST"],
)
#######################################################################
# Media
private_app.add_api_route(
path="/media/",
endpoint=self.store_file, # type: ignore
methods=["POST"],
tags=["Media"],
response_model=StoreFileResponseModel,
)
private_app.add_api_route(
path="/media/generate-file-url/",
endpoint=self.generate_file_url, # type: ignore
methods=["POST"],
tags=["Media"],
response_model=GenerateFileURLResponseModel,
)
for table_config in self.table_configs:
if table_config.media_columns:
for (
column,
media_storage,
) in table_config.media_columns.items():
if isinstance(media_storage, LocalMediaStorage):
# We apply a restrictive CSP here to mitigate SVG
# files being used maliciously when viewed by admins
private_app.mount(
path=f"/media-files/{column._meta.table._meta.tablename}/{column._meta.name}/", # noqa: E501
app=CSPMiddleware(
StaticFiles(
directory=media_storage.media_path
),
config=CSPConfig(default_src="none"),
),
)
#######################################################################
public_app = FastAPI(
redoc_url=None,
docs_url=None,
debug=debug,
exception_handlers={500: log_error},
)
public_app.mount("/docs/", swagger_ui(schema_url="../openapi.json"))
if not rate_limit_provider:
rate_limit_provider = InMemoryLimitProvider(
limit=100, timespan=300
)
public_app.mount(
path="/login/",
app=RateLimitingMiddleware(
app=session_login(
auth_table=self.auth_table,
session_table=session_table,
session_expiry=session_expiry,
max_session_expiry=max_session_expiry,
redirect_to=None,
production=production,
),
provider=rate_limit_provider,
),
)
public_app.add_route(
path="/logout/",
route=session_logout(session_table=session_table),
methods=["POST"],
)
# We make the meta endpoint available without auth, because it contains
# the site name.
public_app.add_api_route(
"/meta/", endpoint=self.get_meta, tags=["Meta"] # type: ignore
)
# The translations are public, because we need them on the login page.
public_app.add_api_route(
"/translations/",
endpoint=self.get_translation_list, # type: ignore
methods=["GET"],
tags=["Translations"],
response_model=TranslationListResponse,
)
public_app.add_api_route(
"/translations/{language_code:str}/",
endpoint=self.get_translation, # type: ignore
methods=["GET"],
tags=["Translations"],
response_model=Translation,
)
#######################################################################
self.router.add_route(
path="/", endpoint=self.get_root, methods=["GET"]
)
self.mount(
path="/assets",
app=StaticFiles(directory=os.path.join(ASSET_PATH, "assets")),
)
auth_middleware = partial(
AuthenticationMiddleware,
backend=SessionsAuthBackend(
auth_table=auth_table,
session_table=session_table,
admin_only=True,
increase_expiry=increase_expiry,
),
on_error=handle_auth_exception,
)
self.mount(path="/api", app=auth_middleware(private_app))
self.mount(path="/public", app=public_app)
async def get_root(self, request: Request) -> HTMLResponse:
return HTMLResponse(self.template)
###########################################################################
def _get_media_storage(
self, table_name: str, column_name: str
) -> MediaStorage:
"""
Retrieve the ``MediaStorage`` for the given column.
:raises HTTPException:
If a matching ``MediaStorage`` can't be found.
"""
table_config = self.table_config_map.get(table_name)
if not table_config:
raise HTTPException(status_code=404, detail="No such table found.")
media_columns = table_config.media_columns
if media_columns is None:
raise HTTPException(
status_code=422,
detail="No media columns are configured for this table.",
)
try:
column = table_config.table_class._meta.get_column_by_name(
column_name
)
except ValueError:
raise HTTPException(
status_code=404,
detail="No such column found.",
)
media_storage = media_columns.get(column)
if not media_storage:
raise HTTPException(
status_code=422,
detail="This column is not configured as a media_column.",
)
return media_storage
async def store_file(
self,
request: Request,
table_name: str = Form(None),
column_name: str = Form(None),
file: UploadFile = File(...),
) -> StoreFileResponseModel:
"""
Stores in the file in the configured ``MediaStorage``, and returns a
unique key for identifying that file.
"""
if self.read_only:
raise HTTPException(
status_code=405, detail="Running in read-only mode."
)
media_storage = self._get_media_storage(
table_name=table_name, column_name=column_name
)
try:
file_key = await media_storage.store_file(
file_name=str(file.filename),
file=file.file,
user=request.user.user,
)
except ValueError as exception:
raise HTTPException(status_code=422, detail=str(exception))
return StoreFileResponseModel(file_key=file_key)
async def generate_file_url(
self, request: Request, model: GenerateFileURLRequestModel
) -> GenerateFileURLResponseModel:
"""
Returns a URL for accessing the given file.
We don't use a GET for this endpoint, as using a GET param to pass the
``file_key`` is too restrictive on which characters can be used.
"""
if self.read_only:
raise HTTPException(
status_code=405, detail="Running in read-only mode."
)
media_storage = self._get_media_storage(
table_name=model.table_name, column_name=model.column_name
)
file_url = await media_storage.generate_file_url(
file_key=model.file_key,
root_url=(
f"./api/media-files/{model.table_name}"
f"/{model.column_name}/"
),
user=request.user.user,
)
return GenerateFileURLResponseModel(file_url=file_url)
###########################################################################
def get_user(self, request: Request) -> UserResponseModel:
return UserResponseModel(
username=request.user.display_name,
user_id=str(request.user.user_id),
)
###########################################################################
# Custom forms
def get_forms(self) -> t.List[FormConfigResponseModel]:
"""
Returns a list of all forms registered with the admin.
"""
return [
FormConfigResponseModel(
name=form.name, slug=form.slug, description=form.description
)
for form in self.forms
]
def get_single_form(self, form_slug: str) -> FormConfigResponseModel:
"""
Returns the FormConfig for the given form.
"""
form = self.form_config_map.get(form_slug, None)
if form is None:
raise HTTPException(status_code=404, detail="No such form found")
else:
return FormConfigResponseModel(
name=form.name,
slug=form.slug,
description=form.description,
)
def get_single_form_schema(self, form_slug: str) -> t.Dict[str, t.Any]:
form_config = self.form_config_map.get(form_slug)
if form_config is None:
raise HTTPException(status_code=404, detail="No such form found")
else:
return form_config.pydantic_model.model_json_schema()
async def post_single_form(
self, request: Request, form_slug: str
) -> t.Any:
"""
Handles posting of custom forms.
"""
form_config = self.form_config_map.get(form_slug)
data = await request.json()
if form_config is None:
raise HTTPException(status_code=404, detail="No such form found")
try:
model_instance = form_config.pydantic_model(**data)
except ValidationError as exception:
# We use 'detail' as it mirrors what FastAPI returns for Pydantic
# errors, allowing us to use the same error display logic in the
# front end.
return JSONResponse(
{"detail": json.loads(exception.json())}, status_code=422
)
try:
endpoint = form_config.endpoint
if inspect.iscoroutinefunction(endpoint):
response = await endpoint( # type: ignore
request, model_instance
)
else:
response = endpoint(request, model_instance)
except ValueError as exception:
return JSONResponse(
{"custom_form_error": str(exception)}, status_code=422
)
message = (
response if isinstance(response, str) else "Successfully submitted"
)
return JSONResponse({"custom_form_success": message})
###########################################################################
def get_meta(self) -> MetaResponseModel:
return MetaResponseModel(
piccolo_admin_version=PICCOLO_ADMIN_VERSION,
site_name=self.site_name,
)
###########################################################################
def get_sidebar_links(self) -> t.Dict[str, str]:
"""
Returns the custom links registered with the admin.
"""
return self.sidebar_links
###########################################################################
def get_table_list(self) -> t.List[str]:
"""
Returns the list of table groups registered with the admin.
"""
return [i.table_class._meta.tablename for i in self.table_configs]
def get_table_list_grouped(self) -> GroupedTableNamesResponseModel:
"""
Returns a list of all apps with tables registered with the admin,
grouped using `menu_group`.
"""
response = GroupedTableNamesResponseModel()
group_names = sorted(
{i.menu_group for i in self.table_configs if i.menu_group}
)
response.grouped = {i: [] for i in group_names}
for table_config in self.table_configs:
menu_group = table_config.menu_group
if menu_group is None:
response.ungrouped.append(
table_config.table_class._meta.tablename
)
else:
response.grouped[menu_group].append(
table_config.table_class._meta.tablename
)