resolved most issues and simplified some code

feature/thumbnails
KarolG 2023-08-16 04:10:13 +02:00
rodzic a415889b8f
commit 6a8d5b735b
7 zmienionych plików z 45 dodań i 52 usunięć

Wyświetl plik

@ -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}')

Wyświetl plik

@ -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,

Wyświetl plik

@ -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)}

Wyświetl plik

@ -5,5 +5,3 @@ class StoreConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'store'
def ready(self):
import store.signals

Wyświetl plik

@ -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)

Wyświetl plik

@ -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)

Wyświetl plik

@ -3,20 +3,19 @@
<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 }}.40x60_q85_crop.jpg" class="d-block d-sm-none img-fluid rounded mx-auto" alt="{{item.title}}">
<img src="{{ item.main_image.image|thumbnail_url:'image_40_60' }}" class="d-block d-sm-none img-fluid rounded mx-auto" alt="{{item.title}}">
<img src="{{ item.main_image.image.url }}.60x90_q85_crop.jpg" class="d-none d-sm-block d-md-none img-fluid rounded mx-auto" alt="{{item.title}}">
<img src="{{ item.main_image.image|thumbnail_url:'image_60_90' }}" class="d-none d-sm-block d-md-none img-fluid rounded mx-auto" alt="{{item.title}}">
<img src="{{ item.main_image.image.url }}.80x120_q85_crop.jpg" class="d-none d-md-block d-lg-none img-fluid rounded mx-auto" alt="{{item.title}}">
<img src="{{ item.main_image.image|thumbnail_url:'image_80_120' }}" class="d-none d-md-block d-lg-none img-fluid rounded mx-auto" alt="{{item.title}}">
<img src="{{ item.main_image.image.url }}.120x180_q85_crop.jpg" class="d-none d-lg-block d-xl-none img-fluid rounded mx-auto" alt="{{item.title}}">
<img src="{{ item.main_image.image|thumbnail_url:'image_120_180' }}" class="d-none d-lg-block d-xl-none img-fluid rounded mx-auto" alt="{{item.title}}">
<img src="{{ item.main_image.image.url }}.160x240_q85_crop.jpg" 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">
<img src="{{ item.main_image.image|thumbnail_url:'image_160_240' }}" class="d-none d-xl-block img-fluid rounded mx-auto" alt="{{item.title}}">
</div>
<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>