Skip to content

Commit

Permalink
articles: files - cleans path from bucket and base dir
Browse files Browse the repository at this point in the history
Signed-off-by: pamfilos <[email protected]>
  • Loading branch information
pamfilos committed Apr 25, 2024
1 parent 1856935 commit 75ceea2
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions scoap3/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import pycountry
from django.core.exceptions import MultipleObjectsReturned, ValidationError
from django.core.files.storage import storages
from django.core.files.storage import storages, default_storage
from django.core.validators import URLValidator
from elasticsearch import ConnectionError, ConnectionTimeout, Elasticsearch
from sentry_sdk import capture_exception
Expand All @@ -28,6 +28,18 @@
logger = logging.getLogger(__name__)


def get_default_storage_path():
if hasattr(default_storage, "bucket"):
bucket_name = default_storage.bucket.name
media_path = default_storage.location
return f"{bucket_name}/{media_path}"

return ""


DEFAULT_STORAGE_PATH = get_default_storage_path()


def _rename_keys(data, replacements):
for item in data:
for old_key, new_key in replacements:
Expand Down Expand Up @@ -113,7 +125,8 @@ def _create_article_file(data, article):
for file in data.get("files", {}):
article_id = article.id
file_path = data["files"][file]
file_path = file_path.split("/", 1)[1] if "/" in file_path else file_path
if DEFAULT_STORAGE_PATH:
file_path = file_path.replace(DEFAULT_STORAGE_PATH, "")
article = Article.objects.get(pk=article_id)
article_file_data = {"article_id": article, "file": file_path}
ArticleFile.objects.get_or_create(**article_file_data)
Expand Down

0 comments on commit 75ceea2

Please sign in to comment.