file.models.file

Module Contents

Classes

_UploadedFileColumn

UploadedFileField

A customized version of Depot's uploaded file field. This version

SearchableFile

Files are not made available for elasticsearch by default. This is

File

A general file (image, document, pdf, etc), referenced in the database.

Functions

metadata_lock(→ Iterator[bool])

Locks the metadata from a filedepot.io.local.LocalStoredFile.

update_metadata_after_commit(→ None)

discard_metadata_on_rollback(→ None)

class file.models.file._UploadedFileColumn[source]

Bases: sqlalchemy.Column[depot.fields.upload.UploadedFile]

__set__(obj: object, value: depot.fields.upload.UploadedFile | depot.io.utils.FileIntent) None[source]
class file.models.file.UploadedFileField(filters=tuple(), upload_type=UploadedFile, upload_storage=None, *args, **kw)[source]

Bases: depot.fields.sqlalchemy.UploadedFileField

A customized version of Depot’s uploaded file field. This version stores its data in a JSONB field, instead of using text.

load_dialect_impl(dialect: sqlalchemy.engine.Dialect) TypeEngine[UploadedFile][source]

Return a TypeEngine object corresponding to a dialect.

This is an end-user override hook that can be used to provide differing types depending on the given dialect. It is used by the TypeDecorator implementation of type_engine() to help determine what type should ultimately be returned for a given TypeDecorator.

By default returns self.impl.

process_bind_param(value: UploadedFile | None, dialect: sqlalchemy.engine.Dialect) UploadedFile | None[source]

Receive a bound parameter value to be converted.

Subclasses override this method to return the value that should be passed along to the underlying TypeEngine object, and from there to the DBAPI execute() method.

The operation could be anything desired to perform custom behavior, such as transforming or serializing data. This could also be used as a hook for validating logic.

This operation should be designed with the reverse operation in mind, which would be the process_result_value method of this class.

Parameters:
  • value – Data to operate upon, of any type expected by this method in the subclass. Can be None.

  • dialect – the Dialect in use.

process_result_value(value: dict[str, Any] | None, dialect: sqlalchemy.engine.Dialect) UploadedFile | None[source]

Receive a result-row column value to be converted.

Subclasses should implement this method to operate on data fetched from the database.

Subclasses override this method to return the value that should be passed back to the application, given a value that is already processed by the underlying TypeEngine object, originally from the DBAPI cursor method fetchone() or similar.

The operation could be anything desired to perform custom behavior, such as transforming or serializing data. This could also be used as a hook for validating logic.

Parameters:
  • value – Data to operate upon, of any type expected by this method in the subclass. Can be None.

  • dialect – the Dialect in use.

This operation should be designed to be reversible by the “process_bind_param” method of this class.

class file.models.file.SearchableFile[source]

Bases: onegov.search.ORMSearchable

Files are not made available for elasticsearch by default. This is for security reasons - files are public by default but one has to know the url (a very long id).

Search might lead to a disclosure of all files, which is why files can only be searched if they are of a different polymorphic subclass and use this mixin.

property es_suggestion: str[source]

Returns suggest-as-you-type value of the document. The field used for this property should also be indexed, or the suggestion will lead to nowhere.

If a single string is returned, the completion input equals the completion output. (My Title -> My Title)

If an array of strings is returned, all values are possible inputs and the first value is the output. (My Title/Title My -> My Title)

property es_public: bool[source]

Returns True if the model is available to be found by the public. If false, only editors/admins will see this object in the search results.

es_properties[source]
class file.models.file.File[source]

Bases: onegov.core.orm.Base, onegov.core.orm.abstract.Associable, onegov.core.orm.mixins.TimestampMixin

A general file (image, document, pdf, etc), referenced in the database.

Thanks to the use of Depot files can be seemingly stored in the database (with transaction guarantees), without actually storing it in the database.

property access: str[source]
property file_id: str[source]

The file_id of the contained reference.

If virtual_file_id is not None, it is returned instead.

property claimed_extension: str[source]

Returns the extension as defined by the file name or by the content type (whatever is found first in this order).

Note that this extension could therefore not be correct. It is mainly meant for display purposes.

If you need to know the type of a file you should use the content type stored on the reference.

__tablename__ = 'files'[source]
id: Column[str][source]
name: Column[str][source]
note: Column[str | None][source]
order: Column[str][source]
published: Column[bool][source]
publish_date: Column[datetime | None][source]
publish_end_date: Column[datetime | None][source]
publication: Column[bool][source]
signed: Column[bool][source]
signature_metadata: Column[SignatureMetadata | None][source]
type: Column[str][source]
reference: _UploadedFileColumn[source]
checksum: Column[str | None][source]
extract: Column[str | None][source]
language: Column[str | None][source]
stats: Column[FileStats | None][source]
meta: Column[dict[str, Any]][source]
__mapper_args__[source]
__table_args__ = ()[source]
signature_timestamp() datetime | None[source]
signature_timestamp()[source]
reference_observer(reference: depot.fields.upload.UploadedFile) None[source]
name_observer(name: str) None[source]
get_thumbnail_id(size: str) UploadedFile | None[source]

Returns the thumbnail id with the given size (e.g. ‘small’).

_update_metadata() None[source]
_update_metadata(*, content_type: str = ..., filename: str = ...) None

Updates the underlying metadata with the give values. This operats on low-level interfaces of Depot and assumes local storage.

You should have a good reason for using this.

file.models.file.metadata_lock(metadata_path: _typeshed.StrPath, timeout: float = 0.0) Iterator[bool][source]

Locks the metadata from a filedepot.io.local.LocalStoredFile. Tries to acquire the lock repeatedly in a spin lock until timeout expires, it will return whether or not it managed to acquire the lock

file.models.file.update_metadata_after_commit(session: sqlalchemy.orm.Session) None[source]
file.models.file.discard_metadata_on_rollback(session: sqlalchemy.orm.Session, previous_transaction: sqlalchemy.orm.session.SessionTransaction) None[source]