Skip to content

Commit

Permalink
fix env vars dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
anish-work committed Jul 30, 2024
1 parent 4fd8608 commit 41a0900
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
15 changes: 11 additions & 4 deletions daras_ai_v2/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,18 @@
# django-storages settings for google-cloud
from google.oauth2 import service_account

GCS_CONFIG = config("GCS_CONFIG", default="")
GCS_PRIVATE_KEY = config("GCS_PRIVATE_KEY", default="")
GCS_CREDENTIALS = service_account.Credentials.from_service_account_info(
info={**json.loads(GCS_CONFIG), "private_key": GCS_PRIVATE_KEY}
)

GCS_CREDENTIALS = ""
# create credentials from env vars
try:
GCS_CONFIG = config("GCS_CONFIG")
except UndefinedValueError:
pass
else:
GCS_CREDENTIALS = service_account.Credentials.from_service_account_info(
info={**json.loads(GCS_CONFIG), "private_key": GCS_PRIVATE_KEY}
)

STORAGES["default"] = {
"BACKEND": "storages.backends.gcloud.GoogleCloudStorage",
Expand Down
16 changes: 9 additions & 7 deletions daras_ai_v2/static_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
from daras_ai_v2.settings import GCP_PROJECT, GCS_CREDENTIALS, GS_BUCKET_NAME


client = storage.Client(
GCP_PROJECT,
GCS_CREDENTIALS,
)
bucket = client.get_bucket(GS_BUCKET_NAME)
def gcs_bucket() -> "storage.storage.Bucket":
client = storage.Client(
GCP_PROJECT,
GCS_CREDENTIALS,
)
bucket = client.get_bucket(GS_BUCKET_NAME)
return bucket


def populate_imported_css(html: str, uid: str):
Expand Down Expand Up @@ -41,7 +43,7 @@ def get_all_styles(links: list, uid: str):
for link in links:
if not link.endswith(".css"): # ignore for css files
continue
blob = bucket.get_blob(f"{uid}/{link}")
blob = gcs_bucket().get_blob(f"{uid}/{link}")
blob = blob.download_as_string()
blob = blob.decode("utf-8")
blob = io.StringIO(blob).read()
Expand All @@ -57,7 +59,7 @@ def serve(page_slug: str, file_path: str = None):
return None

uid = static_page.uid

bucket = gcs_bucket()
def render_page():
if file_path:
return None
Expand Down

0 comments on commit 41a0900

Please sign in to comment.