Skip to main content

Asynchronous thumbnail generation for sorl-thumbnail using Celery

Project description

Django Sorl Async Thumbnails

Asynchronous thumbnail generation for sorl-thumbnail using Celery.

Installation

pip install django-sorl-async-thumbnails

Configuration

  1. Add sorl_async_thumbnails to INSTALLED_APPS:
INSTALLED_APPS = [
    ...
    'sorl.thumbnail',
    'sorl_async_thumbnails',
    ...
]
  1. Configure Celery in your project.

  2. Add settings to settings.py:

# Default settings
THUMBNAIL_BACKEND = 'sorl_async_thumbnails.backend.AsyncThumbnailBackend' #Set AsyncThumbnailBackend as backend for sorl.thumbnails
ASYNC_THUMBNAILS_BY_DEFAULT = False  # Generate thumbnails asynchronously by default
ASYNC_THUMBNAILS_PLACEHOLDER = True  # Use placeholder during generation
ASYNC_THUMBNAILS_PLACEHOLDER_STATICFILE = 'placeholder.jpg'  # Path to placeholder image
ASYNC_THUMBNAILS_GENERATE_PLACEHOLDER_THUMB = True  # Generate thumbnail for placeholder
ASYNC_THUMBNAILS_STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'  # Storage for placeholder

Usage

To generate thumbnails in synchronous mode regardless of settings, use the create_async=False keyword

Example

# In your model
from sorl.thumbnail import get_thumbnail 

class MyModel(models.Model):
    image = models.ImageField(upload_to='images/')
    
    def get_thumbnail(self):
        backend = AsyncThumbnailBackend()
        # If ASYNC_THUMBNAILS_BY_DEFAULT is True, this will generate thumbnail asynchronously
        return get_thumbnail(self.image, '100x100')
    
    def get_thumbnail_sync(self):
        backend = AsyncThumbnailBackend()
        # Force synchronous generation
        return get_thumbnail(self.image, '100x100', create_async=False)
    
    def get_thumbnail_async(self):
        backend = AsyncThumbnailBackend()
        # Force asynchronous generation
        return get_thumbnail(self.image, '100x100', create_async=True)

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_sorl_async_thumbnails-0.1.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_sorl_async_thumbnails-0.1.1-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file django_sorl_async_thumbnails-0.1.1.tar.gz.

File metadata

File hashes

Hashes for django_sorl_async_thumbnails-0.1.1.tar.gz
Algorithm Hash digest
SHA256 75b726547bb3af789e952d10658243c044709cb8e58589a4920fe473fffbcbc4
MD5 f56ca60fc4a9ad447fcd226fa9e1ace2
BLAKE2b-256 af0908ab97424127565f1a8ec0c2be33ad9be1272e03aa5bec96a5464d10c5f1

See more details on using hashes here.

File details

Details for the file django_sorl_async_thumbnails-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_sorl_async_thumbnails-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 84e287cd174920ed93cc9a84c01c6dfa9506497a5d5b2c485f56cd68ccdeebae
MD5 96594725c7a73aa397686bf7684e202b
BLAKE2b-256 7fe93bc8abc9d8091dc95e62e0e429bc52adea9765db435e5160a90e36e86c52

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page