meringue.thumbnail.shortcuts ¤
get_dummy_image_url ¤
get_dummy_image_url(
job_chain: JobChainType | None = None,
) -> str
Returns the dummy image url used instead of a thumbnail that can't be made (an error, a missing source file) or is not ready yet (it is being made by another process).
In DEBUG mode the url is made from THUMBNAIL_DUMMYIMAGE_TEMPLATE, otherwise from
THUMBNAIL_DUMMYIMAGE_URL ({static_url}images/noise.png by default). Both support
{static_url} (STATIC_URL at the moment of the call), {width} and {height}
(the final size of the job chain, 42x42 if there is none) placeholders.
Attributes:
-
job_chain–Thumbnail job chain (used to get the dummy size).
Returns:
-
str–Dummy image url.
Examples:
from meringue.thumbnail.dummy import get_dummy_image_url
get_dummy_image_url(["s:300x200", "resize"])
Source code in meringue/thumbnail/dummy.py
def get_dummy_image_url(job_chain: JobChainType | None = None) -> str:
"""
Returns the dummy image url used instead of a thumbnail that can't be made (an error, a missing
source file) or is not ready yet (it is being made by another process).
In `DEBUG` mode the url is made from `THUMBNAIL_DUMMYIMAGE_TEMPLATE`, otherwise from
`THUMBNAIL_DUMMYIMAGE_URL` (`{static_url}images/noise.png` by default). Both support
`{static_url}` (`STATIC_URL` at the moment of the call), `{width}` and `{height}`
(the final size of the job chain, `42x42` if there is none) placeholders.
Attributes:
job_chain: Thumbnail job chain (used to get the dummy size).
Returns:
Dummy image url.
Examples:
```py
from meringue.thumbnail.dummy import get_dummy_image_url
get_dummy_image_url(["s:300x200", "resize"])
```
"""
size = (42, 42)
final_size = re.compile(r"s:(\d+)x(\d+)")
for task in reversed(job_chain or []):
try:
size = final_size.search(task).groups()
break
except AttributeError:
pass
if settings.DEBUG:
template = m_settings.THUMBNAIL_DUMMYIMAGE_TEMPLATE
else:
template = m_settings.THUMBNAIL_DUMMYIMAGE_URL
return template.format(static_url=settings.STATIC_URL or "", width=size[0], height=size[1])
get_thumbnail ¤
get_thumbnail(
file_path: Path | ThumbnailSource | Any,
job_chain: JobChainType,
out_format: str = THUMBNAIL_DEFAULT_FORMAT,
locked_dummy: bool = True,
**kwargs
) -> str
Shortcut to make single image preview
Attributes:
-
file_path–Source image: a model
FieldFile(any storage, including S3) or an absolute local path. -
job_chain–Thumbnail job chain.
-
format–Output image format.
-
locked_dummy–Return the dummy image url instead of the thumbnail url while the thumbnail is being made by another process (the file may not exist yet).
-
**kwargs–Output image save extra params.
Returns:
-
str–Thumbnail url or the dummy image url if the thumbnail can't be made (the error is logged).
Source code in meringue/thumbnail/shortcuts.py
def get_thumbnail(
file_path: Path | ThumbnailSource | Any,
job_chain: JobChainType,
out_format: str = m_settings.THUMBNAIL_DEFAULT_FORMAT,
locked_dummy: bool = True,
**kwargs,
) -> str:
"""
Shortcut to make single image preview
Attributes:
file_path: Source image: a model `FieldFile` (any storage, including S3) or an
absolute local path.
job_chain: Thumbnail job chain.
format: Output image format.
locked_dummy: Return the dummy image url instead of the thumbnail url while the thumbnail
is being made by another process (the file may not exist yet).
**kwargs: Output image save extra params.
Returns:
Thumbnail url or the dummy image url if the thumbnail can't be made (the error is logged).
"""
if isinstance(file_path, Path) and not file_path.exists():
logger.error("File `%s` not found.", file_path)
return get_dummy_image_url(job_chain)
source_name = getattr(file_path, "name", file_path)
try:
thumbnailer = DefaultThumbnailer(file_path, job_chain=job_chain)
thumbnail = thumbnailer.get_thumbnail(out_format, **kwargs)
if thumbnail.locked and locked_dummy:
return get_dummy_image_url(job_chain)
return thumbnail.url
except FileNotFoundError:
logger.error("File `%s` not found.", source_name)
return get_dummy_image_url(job_chain)
except Exception:
# one broken image must not break the whole page
logger.exception("Failed to make a thumbnail for image `%s`", source_name)
return get_dummy_image_url(job_chain)