From ec671312db10ed84070ea212e2fcbf6acd4b25c6 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 29 Sep 2020 09:42:05 +0200 Subject: [PATCH] add decouple to config the system --- .gitignore | 3 +++ README.rst | 5 +++++ ereuse_devicehub/config.py | 12 +++++++++++- examples/env.example | 4 ++++ requirements.txt | 2 ++ 5 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 examples/env.example diff --git a/.gitignore b/.gitignore index 91073ab6..69f6d4be 100644 --- a/.gitignore +++ b/.gitignore @@ -113,3 +113,6 @@ ENV/ .vscode/ .DS_Store /app.py + +# Environment +.env diff --git a/README.rst b/README.rst index c163c31a..1eba57c5 100644 --- a/README.rst +++ b/README.rst @@ -37,6 +37,11 @@ Create a PostgreSQL database called *devicehub* by running - In MacOS: ``bash examples/create-db.sh devicehub dhub``, and password ``ereuse``. +Configure project using environment file (you can use provided example as quickstart): +.. code:: bash + + $ cp examples/env.example .env + Using the `dh` tool for set up with one or multiple inventories. Create the tables in the database by executing: diff --git a/ereuse_devicehub/config.py b/ereuse_devicehub/config.py index c8ff9e44..6a176f6e 100644 --- a/ereuse_devicehub/config.py +++ b/ereuse_devicehub/config.py @@ -1,6 +1,7 @@ from distutils.version import StrictVersion from itertools import chain from typing import Set +from decouple import config from teal.auth import TokenAuth from teal.config import Config @@ -29,7 +30,16 @@ class DevicehubConfig(Config): import_resource(versions)), ) PASSWORD_SCHEMES = {'pbkdf2_sha256'} # type: Set[str] - SQLALCHEMY_DATABASE_URI = 'postgresql://dhub:ereuse@localhost/devicehub' # type: str + DB_USER = config('DB_USER', 'dhub') + DB_PASSWORD = config('DB_PASSWORD', 'ereuse') + DB_HOST = config('DB_HOST', 'localhost') + DB_DATABASE = config('DB_DATABASE', 'devicehub') + SQLALCHEMY_DATABASE_URI = 'postgresql://{user}:{pw}@{host}/{db}'.format( + user=DB_USER, + pw=DB_PASSWORD, + host=DB_HOST, + db=DB_DATABASE, + ) # type: str MIN_WORKBENCH = StrictVersion('11.0a1') # type: StrictVersion """The minimum version of ereuse.org workbench that this devicehub accepts. we recommend not changing this value. diff --git a/examples/env.example b/examples/env.example new file mode 100644 index 00000000..e0c83b5b --- /dev/null +++ b/examples/env.example @@ -0,0 +1,4 @@ +DB_USER='dhub' +DB_PASSWORD='ereuse' +DB_HOST='localhost' +DB_DATABASE='devicehub' diff --git a/requirements.txt b/requirements.txt index 046e8df9..299a9050 100644 --- a/requirements.txt +++ b/requirements.txt @@ -34,3 +34,5 @@ weasyprint==44 psycopg2-binary==2.8.3 sortedcontainers==2.1.0 tqdm==4.32.2 +python-decouple==3.3 +python-dotenv==0.14.0