file.filters

Classes

ConditionalFilter

A depot filter that's only run if a condition is met. The condition

OnlyIfImage

A conditional filter that runs the passed filter only if the

OnlyIfPDF

A conditional filter that runs the passed filter only if the

WithThumbnailFilter

Uploads a thumbnail together with the file.

WithPDFThumbnailFilter

Uploads a preview thumbnail as PNG together with the file.

Module Contents

class file.filters.ConditionalFilter(filter: depot.fields.interfaces.FileFilter)[source]

Bases: depot.fields.interfaces.FileFilter

A depot filter that’s only run if a condition is met. The condition is defined by overriding the :meth:meets_condition returns True.

filter[source]
abstract meets_condition(uploaded_file: depot.fields.upload.UploadedFile) bool[source]
on_save(uploaded_file: depot.fields.upload.UploadedFile) None[source]

Filters are required to provide their own implementation

class file.filters.OnlyIfImage(filter: depot.fields.interfaces.FileFilter)[source]

Bases: ConditionalFilter

A conditional filter that runs the passed filter only if the uploaded file is an image.

meets_condition(uploaded_file: depot.fields.upload.UploadedFile) bool[source]
class file.filters.OnlyIfPDF(filter: depot.fields.interfaces.FileFilter)[source]

Bases: ConditionalFilter

A conditional filter that runs the passed filter only if the uploaded file is a pdf.

meets_condition(uploaded_file: depot.fields.upload.UploadedFile) bool[source]
class file.filters.WithThumbnailFilter(name: str, size: tuple[int, int], format: str)[source]

Bases: depot.fields.interfaces.FileFilter

Uploads a thumbnail together with the file.

Takes for granted that the file is an image.

The resulting uploaded file will provide an additional property thumbnail_name, which will contain the id and the path to the thumbnail. The name is replaced with the name given to the filter.

Warning

Requires Pillow library

Note: This has been copied from Depot and adjusted for our use. Changes include a different storage format, no storage of the url and replacement of thumbnails instead of recreation (if possible).

quality = 90[source]
name[source]
size[source]
format[source]
generate_thumbnail(fp: IO[bytes]) tuple[io.BytesIO, tuple[str, str]][source]
store_thumbnail(uploaded_file: depot.fields.upload.UploadedFile, fp: IO[bytes], thumbnail_size: tuple[str, str] | None = None) None[source]
on_save(uploaded_file: depot.fields.upload.UploadedFile) None[source]

Filters are required to provide their own implementation

class file.filters.WithPDFThumbnailFilter(name: str, size: tuple[int, int], format: str)[source]

Bases: WithThumbnailFilter

Uploads a preview thumbnail as PNG together with the file.

This is basically the PDF implementation for WithThumbnailFilter.

Warning

Requires the presence of ghostscript (gs binary) on the PATH.

downscale_factor = 4[source]
generate_preview(fp: SupportsRead[bytes]) io.BytesIO[source]
generate_thumbnail(fp: SupportsRead[bytes]) tuple[io.BytesIO, tuple[str, str]][source]