From 6a8d5b735be81fbb5a7eae7bc48bc00a1f2d3283 Mon Sep 17 00:00:00 2001 From: KarolG Date: Wed, 16 Aug 2023 04:10:13 +0200 Subject: [PATCH] resolved most issues and simplified some code --- artel/artel/celery.py | 5 ---- artel/artel/settings/base.py | 23 ++++++++++----- artel/artel/tasks.py | 28 ++++++------------- artel/store/apps.py | 2 -- artel/store/models.py | 12 ++++++++ artel/store/signals.py | 10 ------- .../store/partials/product_card.html | 17 ++++++----- 7 files changed, 45 insertions(+), 52 deletions(-) delete mode 100644 artel/store/signals.py diff --git a/artel/artel/celery.py b/artel/artel/celery.py index 887a017..3cdc484 100644 --- a/artel/artel/celery.py +++ b/artel/artel/celery.py @@ -15,8 +15,3 @@ app.config_from_object('django.conf:settings', namespace='CELERY') # Load task modules from all registered Django apps. app.autodiscover_tasks() - - -@app.task(bind=True) -def debug_task(self): - print(f'Request: {self.request!r}') diff --git a/artel/artel/settings/base.py b/artel/artel/settings/base.py index 4cc70e8..344dea3 100644 --- a/artel/artel/settings/base.py +++ b/artel/artel/settings/base.py @@ -218,19 +218,28 @@ EMAIL_USE_TLS = True 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 = "Europe/Warsaw" -CELERY_TASK_TRACK_STARTED = True -CELERY_TASK_TIME_LIMIT = 30 * 60 +CELERY_RESULT_BACKEND = os.environ.get("CELERY_RESULT_BACKEND") +CELERY_CACHE_BACKEND = os.environ.get("CELERY_CACHE_BACKEND") +CELERY_TIMEZONE = os.environ.get("CELERY_TIMEZONE") +CELERY_TASK_TRACK_STARTED = os.environ.get("CELERY_TASK_TRACK_STARTED") +CELERY_TASK_TIME_LIMIT = os.environ.get("CELERY_TASK_TIME_LIMIT") # CELERY_RESULT_BACKEND_DB = f'db+mysql+pymysql://{os.environ.get("MYSQL_USER")}:{os.environ.get("MYSQL_PASSWORD")}@db/{os.environ.get("MYSQL_DATABASE")}' CELERY_BROKER_URL = f'amqp://{os.environ.get("RABBITMQ_DEFAULT_USER")}:{os.environ.get("RABBITMQ_DEFAULT_PASS")}@rabbit//' -CELERY_TASK_RESULT_EXPIRES = 18000 +CELERY_TASK_RESULT_EXPIRES = os.environ.get("CELERY_TASK_RESULT_EXPIRES") +CELERY_ACCEPT_CONTENT = ['pickle'] #add this to your env # EASY_THUMBNAILS settings THUMBNAIL_DEFAULT_STORAGE = 'django.core.files.storage.FileSystemStorage' - +THUMBNAIL_ALIASES = { + '': { + 'image_40_60': {'size': (40, 60), 'crop': True}, + 'image_60_90': {'size': (60, 90), 'crop': True}, + 'image_80_120': {'size': (80, 120), 'crop': True}, + 'image_120_180': {'size': (120, 180), 'crop': True}, + 'image_160_240': {'size': (160, 240), 'crop': True}, + }, +} LOGGING = { "version": 1, diff --git a/artel/artel/tasks.py b/artel/artel/tasks.py index 35b1ab0..3233132 100644 --- a/artel/artel/tasks.py +++ b/artel/artel/tasks.py @@ -1,28 +1,18 @@ from celery import shared_task -from easy_thumbnails.files import get_thumbnailer +from easy_thumbnails.files import generate_all_aliases 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_path): +@shared_task(serializer="pickle") +def generate_thumbnails(model, pk, field): try: - thumbnailer = get_thumbnailer(image_path) - thumbnails = {} - for size_name, (width, height, crop) in THUMBNAIL_SIZES.items(): - thumbnail = thumbnailer.get_thumbnail({'size': (width, height), 'crop': crop}) - thumbnails[size_name] = thumbnail.url - return thumbnails + instance = model._default_manager.get(pk=pk) + fieldfile = getattr(instance, field) + generate_all_aliases(fieldfile, include_global=True) + return {"status": True} except Exception as e: - logger.exception(f"Error generating thumbnails for image_instance {image_path}: {e}") - return None + logger.exception("An error occurred while generating thumbnails.") + return {"status": False, "error": str(e)} diff --git a/artel/store/apps.py b/artel/store/apps.py index 9b4f0bd..5db39b5 100644 --- a/artel/store/apps.py +++ b/artel/store/apps.py @@ -5,5 +5,3 @@ class StoreConfig(AppConfig): default_auto_field = 'django.db.models.BigAutoField' name = 'store' - def ready(self): - import store.signals diff --git a/artel/store/models.py b/artel/store/models.py index e4a0698..72c2b9a 100644 --- a/artel/store/models.py +++ b/artel/store/models.py @@ -43,6 +43,10 @@ from mailings.models import ( from easy_thumbnails.fields import ThumbnailerImageField +from django.dispatch import receiver +from easy_thumbnails.signals import saved_file +from artel import tasks + logger = logging.getLogger(__name__) @@ -535,3 +539,11 @@ class OrderDocument(models.Model): template = Template(content) content = template.render(context) return pdfkit.from_string(content, False) + + + +@receiver(saved_file) +def generate_thumbnails_async(sender, fieldfile, **kwargs): + tasks.generate_thumbnails.delay( + model=sender, pk=fieldfile.instance.pk, + field=fieldfile.field.name) \ No newline at end of file diff --git a/artel/store/signals.py b/artel/store/signals.py deleted file mode 100644 index 2124bc6..0000000 --- a/artel/store/signals.py +++ /dev/null @@ -1,10 +0,0 @@ -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 generate_thumbnail(sender, instance, **kwargs): - image_path = instance.image.path - generate_thumbnails.delay(image_path) diff --git a/artel/store/templates/store/partials/product_card.html b/artel/store/templates/store/partials/product_card.html index 6ef90e0..6f71d50 100644 --- a/artel/store/templates/store/partials/product_card.html +++ b/artel/store/templates/store/partials/product_card.html @@ -3,20 +3,19 @@
{{item.title}}
- {{item.title}} + {{item.title}} - {{item.title}} + {{item.title}} - {{item.title}} + {{item.title}} - {{item.title}} + {{item.title}} - {{item.title}} - -