|
1 | 1 | import asyncio |
2 | 2 | import os |
3 | 3 |
|
4 | | -from dotenv import find_dotenv, load_dotenv |
| 4 | +import yaml |
5 | 5 | from hypercorn import Config |
6 | 6 | from hypercorn.asyncio import serve |
7 | 7 | from piccolo.apps.user.tables import BaseUser |
8 | 8 | from piccolo.engine import PostgresEngine |
9 | 9 | from piccolo.engine.sqlite import SQLiteEngine |
10 | 10 | from piccolo.table import create_db_tables |
11 | 11 | from piccolo.table_reflection import TableStorage |
12 | | -from piccolo_admin import create_admin |
| 12 | +from piccolo_admin.endpoints import TableConfig, create_admin |
13 | 13 | from piccolo_api.encryption.providers import XChaCha20Provider |
14 | 14 | from piccolo_api.mfa.authenticator.provider import AuthenticatorProvider |
15 | 15 | from piccolo_api.mfa.authenticator.tables import ( |
16 | 16 | AuthenticatorSecret as AuthenticatorSecret_, |
17 | 17 | ) |
18 | 18 | from piccolo_api.session_auth.tables import SessionsBase |
19 | 19 |
|
20 | | -DB = SQLiteEngine() |
21 | | - |
| 20 | +with open("config.yaml") as stream: |
| 21 | + try: |
| 22 | + admin_config = yaml.safe_load(stream) |
| 23 | + BASE_CONFIG = admin_config.get("tables") |
| 24 | + except yaml.YAMLError as exc: |
| 25 | + raise exc |
22 | 26 |
|
23 | | -load_dotenv(find_dotenv()) |
| 27 | +DB = SQLiteEngine() |
24 | 28 |
|
25 | 29 |
|
26 | 30 | class Sessions(SessionsBase, db=DB): |
@@ -67,29 +71,106 @@ async def main(): |
67 | 71 | storage = TableStorage(engine=db) |
68 | 72 | await storage.reflect(schema_name="public") |
69 | 73 |
|
70 | | - # This tuple IS unique |
71 | | - # however auto_include_related within |
72 | | - # create_admin makes it non unique TableConfigs |
73 | | - found_tables = storage.tables.values() |
74 | | - |
75 | | - for table_class in found_tables: |
76 | | - table_class._meta._db = db |
| 74 | + # additional configuration of admin tables |
| 75 | + if BASE_CONFIG is not None: |
| 76 | + tables_to_show = [table.lower() for table in BASE_CONFIG] |
| 77 | + found_tables = [ |
| 78 | + table |
| 79 | + for table in storage.tables.values() |
| 80 | + if table._meta.tablename in tables_to_show |
| 81 | + ] |
| 82 | + admin_tables = [] |
| 83 | + for table in found_tables: |
| 84 | + capitalize_table_name = table._meta.tablename.capitalize() |
| 85 | + # visible columns |
| 86 | + try: |
| 87 | + visible_columns = [ |
| 88 | + column |
| 89 | + for column in table._meta.columns |
| 90 | + if column._meta.name |
| 91 | + in BASE_CONFIG[capitalize_table_name].get( |
| 92 | + "visible_columns", None |
| 93 | + ) |
| 94 | + ] |
| 95 | + except TypeError: |
| 96 | + visible_columns = None |
| 97 | + # visible filters |
| 98 | + try: |
| 99 | + visible_filters = [ |
| 100 | + column |
| 101 | + for column in table._meta.columns |
| 102 | + if column._meta.name |
| 103 | + in BASE_CONFIG[capitalize_table_name].get( |
| 104 | + "visible_filters", None |
| 105 | + ) |
| 106 | + ] |
| 107 | + except TypeError: |
| 108 | + visible_filters = None |
| 109 | + # rich text columns |
| 110 | + try: |
| 111 | + rich_text_columns = [ |
| 112 | + column |
| 113 | + for column in table._meta.columns |
| 114 | + if column._meta.name |
| 115 | + in BASE_CONFIG[capitalize_table_name].get( |
| 116 | + "rich_text_columns", None |
| 117 | + ) |
| 118 | + ] |
| 119 | + except TypeError: |
| 120 | + rich_text_columns = None |
| 121 | + # link column |
| 122 | + try: |
| 123 | + link_column = [ |
| 124 | + column |
| 125 | + for column in table._meta.columns |
| 126 | + if column._meta.name |
| 127 | + == BASE_CONFIG[capitalize_table_name].get( |
| 128 | + "link_column", None |
| 129 | + ) |
| 130 | + ][0] |
| 131 | + except IndexError: |
| 132 | + link_column = None |
| 133 | + # menu_group |
| 134 | + menu_group = BASE_CONFIG[capitalize_table_name].get( |
| 135 | + "menu_group", None |
| 136 | + ) |
| 137 | + |
| 138 | + admin_tables.append( |
| 139 | + TableConfig( |
| 140 | + table_class=table, |
| 141 | + visible_columns=visible_columns, |
| 142 | + visible_filters=visible_filters, |
| 143 | + rich_text_columns=rich_text_columns, |
| 144 | + link_column=link_column, |
| 145 | + menu_group=menu_group, |
| 146 | + ) |
| 147 | + ) |
| 148 | + else: |
| 149 | + admin_tables = storage.tables.values() |
| 150 | + |
| 151 | + for table in admin_tables: |
| 152 | + if isinstance(table, TableConfig): |
| 153 | + table.table_class._meta._db = db |
| 154 | + else: |
| 155 | + table._meta._db = db |
| 156 | + |
| 157 | + # create new encription key for MFA |
| 158 | + encryption_key = XChaCha20Provider.get_new_key() |
77 | 159 |
|
78 | 160 | app = create_admin( |
79 | | - found_tables, |
| 161 | + admin_tables, |
80 | 162 | auth_table=User, |
81 | 163 | session_table=Sessions, |
82 | 164 | auto_include_related=False, |
83 | 165 | mfa_providers=[ |
84 | 166 | AuthenticatorProvider( |
85 | 167 | encryption_provider=XChaCha20Provider( |
86 | | - encryption_key=os.environb[b"ENCRIPTION_KEY"] |
87 | | - .decode("unicode-escape") |
88 | | - .encode("latin-1") |
| 168 | + encryption_key=encryption_key, |
89 | 169 | ), |
90 | 170 | secret_table=AuthenticatorSecret, |
91 | 171 | ), |
92 | 172 | ], |
| 173 | + sidebar_links=admin_config.get("sidebar_links", None), |
93 | 174 | ) |
94 | 175 |
|
95 | 176 | # Server |
|
0 commit comments