-
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.
Merge pull request #213 from 2077-Collective/feat/autosave-image-to-c…
…loudinary chore(feat): autosave thumbnail images to cloudinary
- Loading branch information
Showing
11 changed files
with
467 additions
and
48 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
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
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
Oops, something went wrong.