From f4027e231e6ac4ef107304dbd63db76b5f61d772 Mon Sep 17 00:00:00 2001 From: Vinicius Aguiar Date: Tue, 31 Oct 2023 22:03:05 -0300 Subject: [PATCH] chore: rename getenv utility to getenvp (#477) --- bd_api/settings/local.py | 16 ++++++++-------- bd_api/settings/remote.py | 40 +++++++++++++++++++-------------------- bd_api/utils.py | 10 +++++----- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/bd_api/settings/local.py b/bd_api/settings/local.py index d3995788..f2ba8a41 100644 --- a/bd_api/settings/local.py +++ b/bd_api/settings/local.py @@ -39,26 +39,26 @@ # https://docs.djangoproject.com/en/4.0/ref/settings/#email-backend EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend" EMAIL_HOST = getenv("EMAIL_HOST", "smtp.gmail.com") -EMAIL_HOST_USER = getenv("EMAIL_HOST_USER", "NOT SET") -EMAIL_HOST_PASSWORD = getenv("EMAIL_HOST_PASSWORD", "NOT SET") +EMAIL_HOST_USER = getenv("EMAIL_HOST_USER") +EMAIL_HOST_PASSWORD = getenv("EMAIL_HOST_PASSWORD") EMAIL_PORT = int(getenv("EMAIL_PORT", "587")) EMAIL_USE_TLS = bool(getenv("EMAIL_PORT", "True")) -SERVER_EMAIL = getenv("EMAIL_HOST_USER", "NOT SET") -DEFAULT_FROM_EMAIL = getenv("EMAIL_HOST_USER", "NOT SET") +SERVER_EMAIL = getenv("EMAIL_HOST_USER") +DEFAULT_FROM_EMAIL = getenv("EMAIL_HOST_USER") # Logging setup_logger(level="DEBUG", serialize=False) # Google Application Credentials -GOOGLE_APPLICATION_CREDENTIALS = getenv("GOOGLE_APPLICATION_CREDENTIALS", "") +GOOGLE_APPLICATION_CREDENTIALS = getenv("GOOGLE_APPLICATION_CREDENTIALS") # Google Cloud Storage ... # Stripe STRIPE_LIVE_MODE = bool(getenv("STRIPE_LIVE_MODE", "False")) -STRIPE_LIVE_SECRET_KEY = getenv("STRIPE_LIVE_SECRET_KEY", "") -STRIPE_TEST_SECRET_KEY = getenv("STRIPE_TEST_SECRET_KEY", "") -DJSTRIPE_WEBHOOK_SECRET = getenv("DJSTRIPE_WEBHOOK_SECRET", "") +STRIPE_LIVE_SECRET_KEY = getenv("STRIPE_LIVE_SECRET_KEY") +STRIPE_TEST_SECRET_KEY = getenv("STRIPE_TEST_SECRET_KEY") +DJSTRIPE_WEBHOOK_SECRET = getenv("DJSTRIPE_WEBHOOK_SECRET") DJSTRIPE_USE_NATIVE_JSONFIELD = True DJSTRIPE_FOREIGN_KEY_TO_FIELD = "id" diff --git a/bd_api/settings/remote.py b/bd_api/settings/remote.py index 65a8ba97..3626d76c 100644 --- a/bd_api/settings/remote.py +++ b/bd_api/settings/remote.py @@ -7,13 +7,13 @@ from bd_api.custom.logger import setup_logger from bd_api.settings.base import * # noqa -from bd_api.utils import getadmins, getenv +from bd_api.utils import getadmins, getenvp # SECURITY WARNING: don't run with debug turned on in production! DEBUG = False # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = getenv("DJANGO_SECRET_KEY") +SECRET_KEY = getenvp("DJANGO_SECRET_KEY") # CSRF # https://docs.djangoproject.com/en/4.2/ref/csrf/ @@ -32,11 +32,11 @@ DATABASES = { "default": { "ENGINE": "django.db.backends.postgresql_psycopg2", - "NAME": getenv("DB_NAME"), - "USER": getenv("DB_USER"), - "PASSWORD": getenv("DB_PASSWORD"), - "HOST": getenv("DB_HOST"), - "PORT": getenv("DB_PORT"), + "NAME": getenvp("DB_NAME"), + "USER": getenvp("DB_USER"), + "PASSWORD": getenvp("DB_PASSWORD"), + "HOST": getenvp("DB_HOST"), + "PORT": getenvp("DB_PORT"), } } @@ -44,23 +44,23 @@ # https://docs.djangoproject.com/en/4.0/ref/settings/#email-backend EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend" EMAIL_HOST = "smtp.gmail.com" -EMAIL_HOST_USER = getenv("EMAIL_HOST_USER") -EMAIL_HOST_PASSWORD = getenv("EMAIL_HOST_PASSWORD") -EMAIL_PORT = int(getenv("EMAIL_PORT", "587")) -EMAIL_USE_TLS = bool(getenv("EMAIL_PORT", "True")) -SERVER_EMAIL = getenv("EMAIL_HOST_USER") -DEFAULT_FROM_EMAIL = getenv("EMAIL_HOST_USER") +EMAIL_HOST_USER = getenvp("EMAIL_HOST_USER") +EMAIL_HOST_PASSWORD = getenvp("EMAIL_HOST_PASSWORD") +EMAIL_PORT = int(getenvp("EMAIL_PORT", "587")) +EMAIL_USE_TLS = bool(getenvp("EMAIL_PORT", "True")) +SERVER_EMAIL = getenvp("EMAIL_HOST_USER") +DEFAULT_FROM_EMAIL = getenvp("EMAIL_HOST_USER") # Logging setup_logger(level="INFO", serialize=True) # Google Application Credentials -GOOGLE_APPLICATION_CREDENTIALS = getenv("GOOGLE_APPLICATION_CREDENTIALS", "") +GOOGLE_APPLICATION_CREDENTIALS = getenvp("GOOGLE_APPLICATION_CREDENTIALS", "") # Google Cloud Storage -GS_SERVICE_ACCOUNT = getenv("GCP_SA") +GS_SERVICE_ACCOUNT = getenvp("GCP_SA") GS_CREDENTIALS = service_account.Credentials.from_service_account_info(loads(GS_SERVICE_ACCOUNT)) -GS_BUCKET_NAME = getenv("GCP_BUCKET_NAME") +GS_BUCKET_NAME = getenvp("GCP_BUCKET_NAME") GS_EXPIRATION = timedelta(seconds=604800) STORAGES = { "default": { @@ -72,9 +72,9 @@ } # Stripe -STRIPE_LIVE_MODE = bool(getenv("STRIPE_LIVE_MODE")) -STRIPE_LIVE_SECRET_KEY = getenv("STRIPE_LIVE_SECRET_KEY") -STRIPE_TEST_SECRET_KEY = getenv("STRIPE_TEST_SECRET_KEY") -DJSTRIPE_WEBHOOK_SECRET = getenv("DJSTRIPE_WEBHOOK_SECRET") +STRIPE_LIVE_MODE = bool(getenvp("STRIPE_LIVE_MODE")) +STRIPE_LIVE_SECRET_KEY = getenvp("STRIPE_LIVE_SECRET_KEY") +STRIPE_TEST_SECRET_KEY = getenvp("STRIPE_TEST_SECRET_KEY") +DJSTRIPE_WEBHOOK_SECRET = getenvp("DJSTRIPE_WEBHOOK_SECRET") DJSTRIPE_USE_NATIVE_JSONFIELD = True DJSTRIPE_FOREIGN_KEY_TO_FIELD = "id" diff --git a/bd_api/utils.py b/bd_api/utils.py index 00fe9279..833f85c5 100644 --- a/bd_api/utils.py +++ b/bd_api/utils.py @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- -from os import getenv as _getenv +from os import getenv -API_URL = _getenv("BASE_URL_API", "https://localhost:8080") -SETTINGS = _getenv("DJANGO_SETTINGS_MODULE", "bd_api.settings") +API_URL = getenv("BASE_URL_API", "https://localhost:8080") +SETTINGS = getenv("DJANGO_SETTINGS_MODULE", "bd_api.settings") def getadmins(): @@ -13,9 +13,9 @@ def getadmins(): return [admin.split(",") for admin in admins.split(";")] -def getenv(var, default=None): +def getenvp(var, default=None): """Get environment variable or raise exception if not set""" - value = _getenv(var, default) + value = getenv(var, default) if value is None: raise ValueError(f"Environment variable {var} not set") return value