From 4d8198edbf084f204c5ad1323670b7d6d70df207 Mon Sep 17 00:00:00 2001 From: KarolG Date: Tue, 8 Aug 2023 03:48:04 +0200 Subject: [PATCH] Added logger and attempted to do it all on signals --- artel/artel/settings/base.py | 2 +- artel/artel/tasks.py | 35 +++++++++++++------ artel/store/apps.py | 3 ++ .../0007_alter_productimage_image.py | 2 +- .../migrations/0013_merge_20230808_0030.py | 12 +++++++ artel/store/models.py | 3 +- artel/store/signals.py | 10 ++++++ .../store/partials/product_card.html | 21 +++++++---- 8 files changed, 67 insertions(+), 21 deletions(-) create mode 100644 artel/store/migrations/0013_merge_20230808_0030.py create mode 100644 artel/store/signals.py diff --git a/artel/artel/settings/base.py b/artel/artel/settings/base.py index 536bf0e..4cc70e8 100644 --- a/artel/artel/settings/base.py +++ b/artel/artel/settings/base.py @@ -220,7 +220,7 @@ DEFAULT_FROM_EMAIL = os.environ.get('DEFAULT_FROM_EMAIL', 'artel-sklep@tepewu.pl # CELERY settings CELERY_RESULT_BACKEND = 'django-db' CELERY_CACHE_BACKEND = 'django-cache' -CELERY_TIMEZONE = "YourTimeZone" +CELERY_TIMEZONE = "Europe/Warsaw" CELERY_TASK_TRACK_STARTED = True CELERY_TASK_TIME_LIMIT = 30 * 60 # CELERY_RESULT_BACKEND_DB = f'db+mysql+pymysql://{os.environ.get("MYSQL_USER")}:{os.environ.get("MYSQL_PASSWORD")}@db/{os.environ.get("MYSQL_DATABASE")}' diff --git a/artel/artel/tasks.py b/artel/artel/tasks.py index dc0ad4a..3a928a5 100644 --- a/artel/artel/tasks.py +++ b/artel/artel/tasks.py @@ -1,16 +1,31 @@ from celery import shared_task from easy_thumbnails.files import get_thumbnailer +import logging + + +logger = logging.getLogger(__name__) + +THUMBNAIL_SIZES = { + 'image_40_60': (40, 60, True), + 'image_60_90': (60, 90, True), + 'image_80_120': (80, 120, True), + 'image_120_180': (120, 180, True), + 'image_160_240': (160, 240, True), +} @shared_task -def generate_thumbnails(image_instance): - thumbnailer = get_thumbnailer(image_instance) - small_thumbnail = thumbnailer.get_thumbnail({'size': (40, 60), 'crop': True}) - medium_thumbnail = thumbnailer.get_thumbnail({'size': (80, 120), 'crop': True}) - large_thumbnail = thumbnailer.get_thumbnail({'size': (160, 240), 'crop': True}) +def generate_thumbnails(image_url): + try: + thumbnails = {} + thumbnailer = get_thumbnailer(image_url) - return { - 'small': small_thumbnail.url, - 'medium': medium_thumbnail.url, - 'large': large_thumbnail.url, - } + for size_name, (width, height, crop) in THUMBNAIL_SIZES.items(): + thumbnail = thumbnailer.get_thumbnail({'size': (width, height), 'crop': crop}) + thumbnail_url = thumbnail.url + thumbnails[size_name] = thumbnail_url + + return thumbnails + except Exception as e: + logger.exception(f"Error generating thumbnails for image_instance {image_url}: {e}") + return None diff --git a/artel/store/apps.py b/artel/store/apps.py index 41658c1..9b4f0bd 100644 --- a/artel/store/apps.py +++ b/artel/store/apps.py @@ -4,3 +4,6 @@ from django.apps import AppConfig class StoreConfig(AppConfig): default_auto_field = 'django.db.models.BigAutoField' name = 'store' + + def ready(self): + import store.signals diff --git a/artel/store/migrations/0007_alter_productimage_image.py b/artel/store/migrations/0007_alter_productimage_image.py index 204688e..12d6eee 100644 --- a/artel/store/migrations/0007_alter_productimage_image.py +++ b/artel/store/migrations/0007_alter_productimage_image.py @@ -6,7 +6,7 @@ import easy_thumbnails.fields class Migration(migrations.Migration): dependencies = [ - ("store", "0006_documenttemplate_created_at_orderdocument_sent"), + ("store", "0006_remove_orderdocument_sent"), ] operations = [ diff --git a/artel/store/migrations/0013_merge_20230808_0030.py b/artel/store/migrations/0013_merge_20230808_0030.py new file mode 100644 index 0000000..10a67cd --- /dev/null +++ b/artel/store/migrations/0013_merge_20230808_0030.py @@ -0,0 +1,12 @@ +# Generated by Django 4.1.10 on 2023-08-08 00:30 + +from django.db import migrations + + +class Migration(migrations.Migration): + dependencies = [ + ("store", "0007_alter_productimage_image"), + ("store", "0012_deliverymethod_order_uuid_product_uuid_and_more"), + ] + + operations = [] diff --git a/artel/store/models.py b/artel/store/models.py index 17fb285..e4a0698 100644 --- a/artel/store/models.py +++ b/artel/store/models.py @@ -42,14 +42,13 @@ from mailings.models import ( ) from easy_thumbnails.fields import ThumbnailerImageField -from artel.tasks import generate_thumbnails logger = logging.getLogger(__name__) class BaseImageModel(models.Model): - image = models.ImageField() + image = ThumbnailerImageField() is_main = models.BooleanField(default=False) class Meta: diff --git a/artel/store/signals.py b/artel/store/signals.py new file mode 100644 index 0000000..b630588 --- /dev/null +++ b/artel/store/signals.py @@ -0,0 +1,10 @@ +from django.db.models.signals import post_save +from django.dispatch import receiver +from store.models import ProductTemplateImage +from artel.tasks import generate_thumbnails + + +@receiver(post_save, sender=ProductTemplateImage) +def create_thumbnail_on_image_creation(sender, instance, created, **kwargs): + if created: + generate_thumbnails.delay(instance.image.url) diff --git a/artel/store/templates/store/partials/product_card.html b/artel/store/templates/store/partials/product_card.html index 7482250..7c98af3 100644 --- a/artel/store/templates/store/partials/product_card.html +++ b/artel/store/templates/store/partials/product_card.html @@ -1,15 +1,22 @@ {% load static %} {% load thumbnail %}
-
{{item.title}}
-
- {{item.title}} +
{{item.title}}
+
+ {{item.title}} -