|
1 | 1 | import asyncio |
2 | 2 | import os |
3 | 3 |
|
| 4 | +import yaml |
4 | 5 | from hypercorn import Config |
5 | 6 | from hypercorn.asyncio import serve |
6 | 7 | from piccolo.apps.user.tables import BaseUser |
7 | 8 | from piccolo.engine import PostgresEngine |
8 | 9 | from piccolo.engine.sqlite import SQLiteEngine |
9 | 10 | from piccolo.table import create_db_tables |
10 | 11 | from piccolo.table_reflection import TableStorage |
11 | | -from piccolo_admin import create_admin |
| 12 | +from piccolo_admin.endpoints import TableConfig, create_admin |
12 | 13 | from piccolo_api.encryption.providers import XChaCha20Provider |
13 | 14 | from piccolo_api.mfa.authenticator.provider import AuthenticatorProvider |
14 | 15 | from piccolo_api.mfa.authenticator.tables import ( |
15 | 16 | AuthenticatorSecret as AuthenticatorSecret_, |
16 | 17 | ) |
17 | 18 | from piccolo_api.session_auth.tables import SessionsBase |
18 | 19 |
|
| 20 | +with open("config.yaml") as stream: |
| 21 | + try: |
| 22 | + admin_config = yaml.safe_load(stream) |
| 23 | + BASE_CONFIG = admin_config["tables"] |
| 24 | + except yaml.YAMLError as exc: |
| 25 | + raise exc |
| 26 | + |
19 | 27 | DB = SQLiteEngine() |
20 | 28 |
|
21 | 29 |
|
@@ -60,32 +68,52 @@ async def main(): |
60 | 68 | extensions=tuple(), |
61 | 69 | ) |
62 | 70 |
|
63 | | - tables_to_show = [ |
64 | | - table_to_show.lower().strip() |
65 | | - for table_to_show in os.environ["TABLES_TO_SHOW"].split(",") |
66 | | - ] |
67 | | - |
68 | 71 | storage = TableStorage(engine=db) |
69 | 72 | await storage.reflect(schema_name="public") |
70 | 73 |
|
71 | | - # tables to show in admin |
72 | | - if len(tables_to_show) == 1: |
73 | | - found_tables = storage.tables.values() |
74 | | - else: |
| 74 | + # additional configuration of admin tables |
| 75 | + if BASE_CONFIG is not None: |
| 76 | + tables_to_show = [table.lower() for table in BASE_CONFIG] |
75 | 77 | found_tables = [ |
76 | 78 | table |
77 | 79 | for table in storage.tables.values() |
78 | 80 | if table._meta.tablename in tables_to_show |
79 | 81 | ] |
| 82 | + admin_tables = [] |
| 83 | + for table in found_tables: |
| 84 | + capitalize_table_name = table._meta.tablename.capitalize() |
| 85 | + admin_tables.append( |
| 86 | + TableConfig( |
| 87 | + table_class=table, |
| 88 | + visible_columns=[ |
| 89 | + column |
| 90 | + for column in table._meta.columns |
| 91 | + if column._meta.name |
| 92 | + in BASE_CONFIG[capitalize_table_name]["visible_columns"] |
| 93 | + ], |
| 94 | + visible_filters=[ |
| 95 | + column |
| 96 | + for column in table._meta.columns |
| 97 | + if column._meta.name |
| 98 | + in BASE_CONFIG[capitalize_table_name]["visible_filters"] |
| 99 | + ], |
| 100 | + menu_group=BASE_CONFIG[capitalize_table_name]["menu_group"], |
| 101 | + ) |
| 102 | + ) |
| 103 | + else: |
| 104 | + admin_tables = storage.tables.values() |
80 | 105 |
|
81 | | - for table_class in found_tables: |
82 | | - table_class._meta._db = db |
| 106 | + for table in admin_tables: |
| 107 | + if isinstance(table, TableConfig): |
| 108 | + table.table_class._meta._db = db |
| 109 | + else: |
| 110 | + table._meta._db = db |
83 | 111 |
|
84 | 112 | # create new encription key for MFA |
85 | 113 | encryption_key = XChaCha20Provider.get_new_key() |
86 | 114 |
|
87 | 115 | app = create_admin( |
88 | | - found_tables, |
| 116 | + admin_tables, |
89 | 117 | auth_table=User, |
90 | 118 | session_table=Sessions, |
91 | 119 | auto_include_related=False, |
|
0 commit comments