diff --git a/basedosdados_api/api/v1/views.py b/basedosdados_api/api/v1/views.py index 1c7b4360..79fcdeb7 100644 --- a/basedosdados_api/api/v1/views.py +++ b/basedosdados_api/api/v1/views.py @@ -4,7 +4,7 @@ import json from django.conf import settings -from django.core.files.storage import get_storage_class +from django.core.files.storage import default_storage as storage from django.http import HttpResponseBadRequest, JsonResponse, QueryDict from elasticsearch import Elasticsearch from haystack.forms import ModelSearchForm @@ -27,8 +27,6 @@ def get(self, request, *args, **kwargs): # As counts are paginated, we need to get the total number of results agg_page_size = 1000 - storage = get_storage_class() - if not q: # If query is empty, query all datasets query = {"match_all": {}} @@ -296,7 +294,7 @@ def get(self, request, *args, **kwargs): cleaned_results["organization"] = [] for _, org in enumerate(organization): if "picture" in org: - picture = storage().url(org["picture"]) + picture = storage.url(org["picture"]) else: picture = "" d = { diff --git a/basedosdados_api/settings/base.py b/basedosdados_api/settings/base.py index a7c99eb6..622ffce7 100644 --- a/basedosdados_api/settings/base.py +++ b/basedosdados_api/settings/base.py @@ -11,9 +11,8 @@ https://docs.djangoproject.com/en/4.1/ref/settings/ """ -import os from datetime import timedelta -from os import getenv +from os import getenv, path from pathlib import Path # Build paths inside the project like this: BASE_DIR / 'subdir'. @@ -78,7 +77,7 @@ TEMPLATES = [ { "BACKEND": "django.template.backends.django.DjangoTemplates", - "DIRS": [os.path.join(BASE_DIR, "templates")], + "DIRS": [path.join(BASE_DIR, "templates")], "APP_DIRS": True, "OPTIONS": { "context_processors": [ @@ -452,11 +451,3 @@ CSRF_COOKIE_HTTPONLY = False DATA_UPLOAD_MAX_NUMBER_FIELDS = 20000 - -# Google Application Credentials -GOOGLE_APPLICATION_CREDENTIALS = getenv("GOOGLE_APPLICATION_CREDENTIALS", "") - -# Google Cloud Storage -GCS_EXPIRATION = timedelta(seconds=604800) -GCS_BUCKET_NAME = getenv("GCP_BUCKET_NAME") -DEFAULT_FILE_STORAGE = "storages.backends.gcloud.GoogleCloudStorage" diff --git a/basedosdados_api/settings/dev.py b/basedosdados_api/settings/dev.py index 1141272f..b1259fe0 100644 --- a/basedosdados_api/settings/dev.py +++ b/basedosdados_api/settings/dev.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- +from pathlib import Path + from django.utils.log import DEFAULT_LOGGING from basedosdados_api.settings.base import * # noqa diff --git a/basedosdados_api/settings/prod.py b/basedosdados_api/settings/prod.py index c0f6eae9..8bef19f8 100644 --- a/basedosdados_api/settings/prod.py +++ b/basedosdados_api/settings/prod.py @@ -1,5 +1,10 @@ # -*- coding: utf-8 -*- +from datetime import timedelta +from json import loads + +from google.oauth2 import service_account + from basedosdados_api.settings.base import * # noqa from utils import getadmins, getenv @@ -29,3 +34,13 @@ EMAIL_USE_TLS = bool(getenv("EMAIL_PORT", "True")) SERVER_EMAIL = getenv("EMAIL_HOST_USER") DEFAULT_FROM_EMAIL = getenv("EMAIL_HOST_USER") + +# Google Application Credentials +GOOGLE_APPLICATION_CREDENTIALS = getenv("GOOGLE_APPLICATION_CREDENTIALS", "") + +# Google Cloud Storage +GS_SERVICE_ACCOUNT = getenv("GCP_SA") +GS_CREDENTIALS = service_account.Credentials.from_service_account_info(loads(GS_SERVICE_ACCOUNT)) +GS_BUCKET_NAME = getenv("GCP_BUCKET_NAME") +GS_EXPIRATION = timedelta(seconds=604800) +DEFAULT_FILE_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"