Skip to content

Commit 52eb004

Browse files
authored
Merge pull request #2 from sinisaos/enviroment_variables
change the use of environment variables
2 parents de2d7b0 + 9b20001 commit 52eb004

File tree

7 files changed

+179
-29
lines changed

7 files changed

+179
-29
lines changed

app/.env.example renamed to .env.example

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ DB_PORT=5432
66
USERNAME="piccolo"
77
PASSWORD="piccolo123"
88
9-
ENCRIPTION_KEY="\xb7(\xa5\xa6\xa4&\xeb\x8eI\xfe_Y\x16\x12\xf4\xf4\xa8|\xc6#\xd1\x02\xa2s\x03]\xea\x12\xb9\xf1\xa2\xb3"

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
FROM python:3.11
1+
FROM python:3.11-slim
22

33
WORKDIR /code
44

55
COPY ./requirements.txt /code/requirements.txt
66

7+
COPY ./config.yaml /code/config.yaml
8+
79
RUN pip install --no-cache-dir -r /code/requirements.txt
810

911
COPY ./app /code/app
10-
11-
CMD ["python", "app/main.py"]

README.md

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Experimenting with using a dockerized Piccolo Admin with an existing (legacy) database.
1+
Dockerized Piccolo Admin to use existing (legacy) database.
22

33
### Usage
44

@@ -11,20 +11,77 @@ git clone https://github.com/piccolo-orm/piccolo-admin-docker.git
1111
Creating an `.env` file.
1212

1313
```bash
14-
cd app
1514
cp .env.example .env && rm .env.example
16-
cd ..
1715
```
18-
Creating a Docker image.
16+
17+
Run the Docker container.
1918

2019
```bash
21-
docker build -t piccolo_admin .
20+
docker-compose up -d
2221
```
2322

24-
Running a Docker image (use the `--network=host` flag for an existing database from the local machine).
23+
After site is running log in as admin user on [localhost:8000](http://localhost:8000) and use legacy database.
24+
25+
Stop the Docker container.
2526

2627
```bash
27-
docker run -d --network=host --name admin_container piccolo_admin
28+
docker-compose down
2829
```
2930

30-
After site is running log in as admin user on [localhost:8000](http://localhost:8000/admin/) and use legacy database.
31+
### Additional Piccolo Admin configuration
32+
33+
Piccolo Admin has a flexible UI with lots of configuration options to display only the columns you want your users to see. More information on Piccolo Admin [docs](https://piccolo-admin.readthedocs.io/en/latest/index.html).
34+
35+
After Piccolo Admin is started with all the tables from the existing database, we can do additional configuration through the `config.yaml` file.
36+
37+
Example of `config.yaml`:
38+
39+
```yaml
40+
tables:
41+
# An example of additional Piccolo Admin configuration
42+
Actor:
43+
visible_columns:
44+
- first_name
45+
visible_filters:
46+
- actor_id
47+
- first_name
48+
menu_group: Movies
49+
link_column: first_name
50+
Address:
51+
visible_columns:
52+
- address_id
53+
- address
54+
- city_id
55+
visible_filters:
56+
- address_id
57+
- address
58+
- city_id
59+
menu_group: Location
60+
rich_text_columns: address
61+
City:
62+
visible_columns:
63+
- city_id
64+
- city
65+
visible_filters:
66+
- city_id
67+
- city
68+
menu_group: Location
69+
Country:
70+
visible_columns:
71+
- country_id
72+
- country
73+
visible_filters:
74+
- country_id
75+
- country
76+
menu_group: Location
77+
78+
sidebar_links:
79+
Piccolo Admin: https://piccolo-admin.readthedocs.io/en/latest/index.html
80+
Piccolo ORM: https://piccolo-orm.readthedocs.io/en/latest/index.html
81+
```
82+
83+
For these changes to take effect, you must stop the container and rebuild it with.
84+
85+
```bash
86+
docker-compose up -d --build
87+
```

app/main.py

Lines changed: 97 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
import asyncio
22
import os
33

4-
from dotenv import find_dotenv, load_dotenv
4+
import yaml
55
from hypercorn import Config
66
from hypercorn.asyncio import serve
77
from piccolo.apps.user.tables import BaseUser
88
from piccolo.engine import PostgresEngine
99
from piccolo.engine.sqlite import SQLiteEngine
1010
from piccolo.table import create_db_tables
1111
from piccolo.table_reflection import TableStorage
12-
from piccolo_admin import create_admin
12+
from piccolo_admin.endpoints import TableConfig, create_admin
1313
from piccolo_api.encryption.providers import XChaCha20Provider
1414
from piccolo_api.mfa.authenticator.provider import AuthenticatorProvider
1515
from piccolo_api.mfa.authenticator.tables import (
1616
AuthenticatorSecret as AuthenticatorSecret_,
1717
)
1818
from piccolo_api.session_auth.tables import SessionsBase
1919

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
2226

23-
load_dotenv(find_dotenv())
27+
DB = SQLiteEngine()
2428

2529

2630
class Sessions(SessionsBase, db=DB):
@@ -67,29 +71,106 @@ async def main():
6771
storage = TableStorage(engine=db)
6872
await storage.reflect(schema_name="public")
6973

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()
77159

78160
app = create_admin(
79-
found_tables,
161+
admin_tables,
80162
auth_table=User,
81163
session_table=Sessions,
82164
auto_include_related=False,
83165
mfa_providers=[
84166
AuthenticatorProvider(
85167
encryption_provider=XChaCha20Provider(
86-
encryption_key=os.environb[b"ENCRIPTION_KEY"]
87-
.decode("unicode-escape")
88-
.encode("latin-1")
168+
encryption_key=encryption_key,
89169
),
90170
secret_table=AuthenticatorSecret,
91171
),
92172
],
173+
sidebar_links=admin_config.get("sidebar_links", None),
93174
)
94175

95176
# Server

config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
tables:
2+
# Additional tables configurations go here
3+
sidebar_links:
4+
# External links go here

docker-compose.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
services:
2+
web:
3+
build: .
4+
command: python app/main.py
5+
volumes:
6+
- .:/app
7+
env_file:
8+
- .env
9+
network_mode: "host"

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ hypercorn
44
piccolo
55
piccolo-admin
66
piccolo_api[authenticator,pynacl]
7-
python-dotenv
7+
pyyaml

0 commit comments

Comments
 (0)