From 45d137a4e0c29e206ce346ce9425da4a621f3124 Mon Sep 17 00:00:00 2001 From: Vinicius Aguiar Date: Sun, 24 Sep 2023 16:38:34 -0300 Subject: [PATCH] chore: split utils folder between api and scripts (#402) --- basedosdados_api/settings/dev.py | 2 +- basedosdados_api/settings/prod.py | 2 +- basedosdados_api/utils.py | 18 ++++++++++++++++++ scripts/migrations/20230803_migrate_users.py | 2 +- scripts/migrations/20230807_migrate_careers.py | 2 +- utils/graphql.py => scripts/utils.py | 0 utils/__init__.py | 17 ----------------- 7 files changed, 22 insertions(+), 21 deletions(-) rename utils/graphql.py => scripts/utils.py (100%) delete mode 100644 utils/__init__.py diff --git a/basedosdados_api/settings/dev.py b/basedosdados_api/settings/dev.py index b1259fe0..3676cdf6 100644 --- a/basedosdados_api/settings/dev.py +++ b/basedosdados_api/settings/dev.py @@ -5,7 +5,7 @@ from django.utils.log import DEFAULT_LOGGING from basedosdados_api.settings.base import * # noqa -from utils import getenv +from basedosdados_api.utils import getenv INSTALLED_APPS += ["django_extensions"] # noqa: F405 diff --git a/basedosdados_api/settings/prod.py b/basedosdados_api/settings/prod.py index 8bef19f8..93585e7e 100644 --- a/basedosdados_api/settings/prod.py +++ b/basedosdados_api/settings/prod.py @@ -6,7 +6,7 @@ from google.oauth2 import service_account from basedosdados_api.settings.base import * # noqa -from utils import getadmins, getenv +from basedosdados_api.utils import getadmins, getenv DEBUG = False SECRET_KEY = getenv("DJANGO_SECRET_KEY") diff --git a/basedosdados_api/utils.py b/basedosdados_api/utils.py index 88903db4..5c8f95ff 100644 --- a/basedosdados_api/utils.py +++ b/basedosdados_api/utils.py @@ -1,10 +1,28 @@ # -*- coding: utf-8 -*- +from os import getenv as _getenv + from django.conf import settings DB_NAME = settings.DATABASES.get("default", {}).get("NAME", "") DB_ENGINE = settings.DATABASES.get("default", {}).get("ENGINE", "") +def getadmins(): + """Get admins from environment variable""" + admins = getenv("ADMINS") + if admins is None: + return [] + return [admin.split(",") for admin in admins.split(";")] + + +def getenv(var, default=None): + """Get environment variable or raise exception if not set""" + value = _getenv(var, default) + if value is None: + raise ValueError(f"Environment variable {var} not set") + return value + + def is_remote(): """Check if it is remote environment""" return "prod" in settings.SETTINGS_MODULE diff --git a/scripts/migrations/20230803_migrate_users.py b/scripts/migrations/20230803_migrate_users.py index 3f685ec3..83513fa2 100644 --- a/scripts/migrations/20230803_migrate_users.py +++ b/scripts/migrations/20230803_migrate_users.py @@ -4,7 +4,7 @@ from random import choice from string import ascii_letters, digits, punctuation -from utils.graphql import gql +from scripts.utils import gql query = """ query ($offset: Int!) { diff --git a/scripts/migrations/20230807_migrate_careers.py b/scripts/migrations/20230807_migrate_careers.py index 135916b7..5e14d3cd 100644 --- a/scripts/migrations/20230807_migrate_careers.py +++ b/scripts/migrations/20230807_migrate_careers.py @@ -2,7 +2,7 @@ from csv import DictReader from os import getenv -from utils.graphql import gql +from scripts.utils import gql query = """ query ($offset: Int!) { diff --git a/utils/graphql.py b/scripts/utils.py similarity index 100% rename from utils/graphql.py rename to scripts/utils.py diff --git a/utils/__init__.py b/utils/__init__.py deleted file mode 100644 index 18e1ca5b..00000000 --- a/utils/__init__.py +++ /dev/null @@ -1,17 +0,0 @@ -# -*- coding: utf-8 -*- -from os import getenv as _getenv - - -def getadmins(): - admins = getenv("ADMINS") - if admins is None: - return [] - return [admin.split(",") for admin in admins.split(";")] - - -def getenv(var, default=None): - """Get environment variable or raise exception if not set""" - value = _getenv(var, default) - if value is None: - raise ValueError(f"Environment variable {var} not set") - return value