Skip to content

Commit

Permalink
fix: update google credentials config (#380)
Browse files Browse the repository at this point in the history
  • Loading branch information
vncsna authored Sep 11, 2023
1 parent 97b231d commit 414ea43
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
6 changes: 2 additions & 4 deletions basedosdados_api/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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": {}}
Expand Down Expand Up @@ -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 = {
Expand Down
13 changes: 2 additions & 11 deletions basedosdados_api/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand Down Expand Up @@ -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": [
Expand Down Expand Up @@ -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"
2 changes: 2 additions & 0 deletions basedosdados_api/settings/dev.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 15 additions & 0 deletions basedosdados_api/settings/prod.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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"

0 comments on commit 414ea43

Please sign in to comment.