Added logger and attempted to do it all on signals
rodzic
8a4b4ade61
commit
4d8198edbf
|
@ -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")}'
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 = [
|
||||
|
|
|
@ -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 = []
|
|
@ -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:
|
||||
|
|
|
@ -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)
|
|
@ -1,15 +1,22 @@
|
|||
{% load static %}
|
||||
{% load thumbnail %}
|
||||
<div class="card h-100" >
|
||||
<div class="card-header text-truncate">{{item.title}}</div>
|
||||
<div class="card-body p-0">
|
||||
<img src="{{item.main_image.image.url}}"
|
||||
class="img-fluid img-thumbnail rounded mx-auto d-block mt-2 mb-2" style="width: 13rem; height: 15rem;" alt="{{item.title}}">
|
||||
<div class="card-header text-truncate">{{item.title}}</div>
|
||||
<div class="card-body p-0">
|
||||
<img src="{{ item.product.main_image.image_40_60 }}" class="d-block d-sm-none img-fluid rounded mx-auto" alt="{{item.title}}">
|
||||
|
||||
<div class="card-footer row d-flex m-0">
|
||||
<div class="col text-center">
|
||||
<img src="{{ item.product.main_image.image_60_90 }}" class="d-none d-sm-block d-md-none img-fluid rounded mx-auto" alt="{{item.title}}">
|
||||
|
||||
<img src="{{ item.product.main_image.image_80_120 }}" class="d-none d-md-block d-lg-none img-fluid rounded mx-auto" alt="{{item.title}}">
|
||||
|
||||
<img src="{{ template.main_image.image.url.image_120_180 }}" class="d-none d-lg-block d-xl-none img-fluid rounded mx-auto" alt="{{item.title}}">
|
||||
|
||||
<img src="{{ template.main_image.image.url.image_160_240 }}" class="d-none d-xl-block img-fluid rounded mx-auto" alt="{{item.title}}">
|
||||
|
||||
<div class="card-footer row d-flex m-0">
|
||||
<div class="col text-center">
|
||||
<a href="{% url 'product-configure' item.id %}" class="btn btn-primary">Konfiguruj</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
Ładowanie…
Reference in New Issue