From ebaba672ab379e9f61f40f391fb9782c37e3da12 Mon Sep 17 00:00:00 2001 From: Sergey Karpuk Date: Tue, 19 Jul 2022 18:52:08 +0300 Subject: [PATCH] use env vars --- .gitignore | 2 ++ djaif/middleware.py | 3 ++- djaif/settings.py | 23 ++++++++--------------- poetry.lock | 14 +++++++++++++- pyproject.toml | 1 + 5 files changed, 26 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 9e5e4a8..9daf8c3 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ __pycache__/ /db.sqlite3 media/ *.egg-info/ +.env +.DS_Store diff --git a/djaif/middleware.py b/djaif/middleware.py index e48499a..82382dd 100644 --- a/djaif/middleware.py +++ b/djaif/middleware.py @@ -1,4 +1,5 @@ from django.contrib.auth import authenticate, login +from djaif.settings import SUPERUSER, PASSWORD def auto_login(get_response): @@ -6,7 +7,7 @@ def middleware(request): # noqa: WPS430 if not request.user.is_authenticated: user = authenticate( # noqa: S106 - username='admin', password='admin', + username=SUPERUSER, password=PASSWORD, ) login(request, user) diff --git a/djaif/settings.py b/djaif/settings.py index 3e6621a..f4dcaa6 100644 --- a/djaif/settings.py +++ b/djaif/settings.py @@ -1,16 +1,8 @@ -""" -Django settings for djaif project. - -Generated by 'django-admin startproject' using Django 3.0.5. - -For more information on this file, see -https://docs.djangoproject.com/en/3.0/topics/settings/ +import os +from dotenv import load_dotenv -For the full list of settings and their values, see -https://docs.djangoproject.com/en/3.0/ref/settings/ -""" -import os +load_dotenv() # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -19,11 +11,12 @@ # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ -# SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'wpw=^28n)_)+ho#&z$5u12z50ny$3f%+8ld#)q2^a+87e_$o1o' +DJANGO_SETTINGS_MODULE = 'djaif.settings' -# SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +SECRET_KEY = os.getenv('SECRET_KEY') +DEBUG = os.getenv('DEBUG', 'False') == 'True' +SUPERUSER = os.getenv('SUPERUSER', 'admin') +PASSWORD = os.getenv('PASSWORD', 'admin') ALLOWED_HOSTS = [] diff --git a/poetry.lock b/poetry.lock index afe4b9e..483ea34 100644 --- a/poetry.lock +++ b/poetry.lock @@ -494,6 +494,17 @@ category = "dev" optional = false python-versions = ">=3.6" +[[package]] +name = "python-dotenv" +version = "0.20.0" +description = "Read key-value pairs from a .env file and set them as environment variables" +category = "main" +optional = false +python-versions = ">=3.5" + +[package.extras] +cli = ["click (>=5.0)"] + [[package]] name = "pyyaml" version = "6.0" @@ -633,7 +644,7 @@ typing_extensions = ">=3.6,<4.0" [metadata] lock-version = "1.1" python-versions = "^3.8" -content-hash = "58ec8c836ed4fdc7f2758015b0255e7bb823c159d71b224403c991d7db5b873c" +content-hash = "e95d05acd7bbe1135e0940f75a2d2c0b493c3f78b71ce02256705b9b7a8a5b37" [metadata.files] appdirs = [] @@ -784,6 +795,7 @@ pygments = [ {file = "Pygments-2.12.0-py3-none-any.whl", hash = "sha256:dc9c10fb40944260f6ed4c688ece0cd2048414940f1cea51b8b226318411c519"}, {file = "Pygments-2.12.0.tar.gz", hash = "sha256:5eb116118f9612ff1ee89ac96437bb6b49e8f04d8a13b514ba26f620208e26eb"}, ] +python-dotenv = [] pyyaml = [ {file = "PyYAML-6.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:d4db7c7aef085872ef65a8fd7d6d09a14ae91f691dec3e87ee5ee0539d516f53"}, {file = "PyYAML-6.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:9df7ed3b3d2e0ecfe09e14741b857df43adb5a3ddadc919a2d94fbdf78fea53c"}, diff --git a/pyproject.toml b/pyproject.toml index 90fa035..a3a0ff0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,6 +10,7 @@ django = "^4.0.6" Pillow = "^9.2.0" graphviz = "^0.20" django-extensions = "^3.2.0" +python-dotenv = "^0.20.0" [tool.poetry.dev-dependencies] wemake-python-styleguide = "^0.14.0"