-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ndu
committed
Dec 18, 2024
1 parent
08aa962
commit b7e0fa5
Showing
9 changed files
with
104 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
server/apps/research/migrations/0017_alter_article_thumb.py
This file was deleted.
Oops, something went wrong.
59 changes: 59 additions & 0 deletions
59
server/apps/research/migrations/0018_alter_article_thumb.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import apps.research.models.article | ||
import cloudinary.models | ||
import cloudinary.uploader | ||
from django.db import migrations | ||
from django.core.files.storage import default_storage | ||
import os | ||
|
||
|
||
def transfer_to_cloudinary(apps, schema_editor): | ||
Article = apps.get_model("research", "Article") | ||
|
||
for instance in Article.objects.all(): | ||
if instance.thumb: | ||
try: | ||
if hasattr(instance.thumb, "public_id"): | ||
continue | ||
|
||
if hasattr(instance.thumb, "url"): | ||
file_path = instance.thumb.path | ||
|
||
if default_storage.exists(file_path): | ||
with open(file_path, "rb") as file: | ||
upload_result = cloudinary.uploader.upload( | ||
file, folder="coverImage", resource_type="image" | ||
) | ||
|
||
instance.thumb = upload_result["public_id"] | ||
instance.save() | ||
|
||
# delete the local file | ||
# default_storage.delete(file_path) | ||
|
||
except Exception as e: | ||
print(f"Info for Article {instance.id}: {str(e)}") | ||
|
||
|
||
def reverse_transfer(apps, schema_editor): | ||
pass | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("research", "0017_article_gpt_summary_alter_article_summary"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="article", | ||
name="thumb", | ||
field=cloudinary.models.CloudinaryField( | ||
blank=True, | ||
default=apps.research.models.article.get_default_thumb, | ||
max_length=255, | ||
verbose_name="image", | ||
), | ||
), | ||
migrations.RunPython(transfer_to_cloudinary, reverse_transfer), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from decouple import config | ||
|
||
CLOUDINARY_DOMAIN = "https://res.cloudinary.com/dc2iz5j1c" | ||
|
||
CLOUDINARY_STORAGE = { | ||
'CLOUD_NAME': config('CLOUDINARY_CLOUD_NAME'), | ||
'API_KEY': config('CLOUDINARY_API_KEY'), | ||
'API_SECRET': config('CLOUDINARY_API_SECRET') | ||
} |