Skip to content

Commit

Permalink
Enable swapping the image processor via a setting
Browse files Browse the repository at this point in the history
To simplify customizations the image processort can be swapped.
Previously, it was necessary to override the PictureFieldFile.
Now, all that needs to be done is setting a custom value for
the `PICTURES["PROCESSOR"]` setting.
  • Loading branch information
codingjoe committed May 24, 2023
1 parent ecce607 commit 9f0c956
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ PICTURES = {
"CONTAINER_WIDTH": 1200,
"FILE_TYPES": ["WEBP"],
"PIXEL_DENSITIES": [1, 2],
"USE_PLACEHOLDERS": True
"USE_PLACEHOLDERS": True,
"QUEUE_NAME": "pictures",
"PROCESSOR": "pictures.tasks.process_picture",

}
```

Expand Down Expand Up @@ -207,6 +210,11 @@ If you have either Dramatiq or Celery installed, we will default to async
image processing. You will need workers to listen to the `pictures` queue.
You can override the queue name, via the `PICTURES["QUEUE_NAME"]` setting.

You can also override the processor, via the `PICTURES["PROCESSOR"]` setting.
The default processor is `pictures.tasks.process_picture`. It takes a single
argument, the `PictureFileFile` instance. You can use this to override the
processor, should you need to do some custom processing.

## Migrations

Django doesn't support file field migrations, but we do.
Expand Down
1 change: 1 addition & 0 deletions pictures/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def get_settings():
"PIXEL_DENSITIES": [1, 2],
"USE_PLACEHOLDERS": settings.DEBUG,
"QUEUE_NAME": "pictures",
"PROCESSOR": "pictures.tasks.process_picture",
**getattr(settings, "PICTURES", {}),
},
)
5 changes: 2 additions & 3 deletions pictures/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

__all__ = ["PictureField", "PictureFieldFile"]

from django.utils.module_loading import import_string

from pictures import conf, utils

Expand Down Expand Up @@ -98,9 +99,7 @@ def save(self, name, content, save=True):

def save_all(self):
if self:
from . import tasks

tasks.process_picture(self)
import_string(conf.get_settings().PROCESSOR)(self)

def delete(self, save=True):
self.delete_all()
Expand Down

0 comments on commit 9f0c956

Please sign in to comment.