gws.plugin.model_field.file¶
File field.
A field for files attached to features of a database model. The file content
is stored in a database column (contentColumn), optionally with the file
name in another column (nameColumn). A pathColumn can be configured
and is read, but storing and serving files from the filesystem is not
implemented yet. The model must have a primary key.
The field value is a FileValue. When features are selected, only the
content length is read, not the content itself. In the client props, a file
is described by a ServerFileProps object with a label, extension, size and
URLs of the webFile command for downloading the file and, for raster
images, for a thumbnail preview. Previews are generated on request and cached
as ephemeral content. Without a configured widget, the field uses a file
widget.
Example:
fields+ {
name "photo"
type "file"
contentColumn "photo_content"
nameColumn "photo_name"
}
Source code: gws.plugin.model_field.file
Package Contents¶
- class gws.plugin.model_field.file.ClientFileProps(*args, **kwargs)¶
Bases:
gws.DataFile sent from the client.
- class gws.plugin.model_field.file.Config¶
Bases:
gws.base.model.field.ConfigField for files stored in the database or the filesystem.
- class gws.plugin.model_field.file.FileInputProps(*args, **kwargs)¶
Bases:
gws.DataFile uploaded from the client.
- class gws.plugin.model_field.file.FileValue(*args, **kwargs)¶
Bases:
gws.DataValue of a file field.
- class gws.plugin.model_field.file.Object¶
Bases:
gws.base.model.field.ObjectFile field object.
Stores files attached to features in columns of a database model and describes them to the client for download and preview.
- attributeType¶
- contentColumn: gws.lib.sa.Column | None = None¶
Column for the file content.
- model: gws.DatabaseModel¶
The model of this field.
- nameColumn: gws.lib.sa.Column | None = None¶
Column for the file name.
- pathColumn: gws.lib.sa.Column | None = None¶
Column for the file path.
- activate()¶
- after_select(features, mc)¶
- before_create(feature, mc)¶
- before_select(mc)¶
- before_update(feature, mc)¶
- can_preview(mime_type) bool¶
Check if a thumbnail preview can be made for a MIME type.
- Parameters:
mime_type – The MIME type.
- Returns:
True for image types except SVG.
- configure_columns()¶
Resolve the configured content, path and name columns of the model.
The columns are SQLAlchemy objects. They are resolved after configuration and again on activation.
- Raises:
gws.ConfigurationError – If neither
contentColumnnorpathColumnis set, or the model has no primary key.
- configure_widget()¶
- from_props(feature, mc)¶
- from_record(feature, mc)¶
- get_mime_type(fv: FileValue) str¶
Determine the MIME type of a file from its path or name.
- Parameters:
fv – The file value.
- Returns:
The MIME type, or the generic binary type if it cannot be determined.
- handle_web_file_request(feature_uid: str, preview: bool, mc: gws.ModelContext) gws.ContentResponse | None¶
Serve the file of a feature, or its thumbnail preview.
Called by the
webFilecommand. Only files stored in the database are served. Thumbnails are PNG images, generated from the content and cached as ephemeral content keyed by the content checksum.- Parameters:
feature_uid – Uid of the feature.
preview – Return a thumbnail preview instead of the file.
mc – The model context.
- Returns:
The file content or the preview, or None if the user may not read the field, the feature or the file is not found, or the file cannot be previewed.
- Raises:
gws.NotFoundError – If the content for a preview cannot be loaded or the thumbnail cannot be created.
- load_value(attributes: dict, mc) FileValue | None¶
Create a file value from the attributes of a database record.
- Parameters:
attributes – Record attributes.
mc – The model context.
- Returns:
The file value, or None if no file columns are configured.
- post_configure()¶
- python_to_prop(feature, value, mc) ServerFileProps¶
- select_columns(mc)¶
Return the columns to add to a select statement.
The content column is not selected; only its length is, labeled
<name>_length.- Parameters:
mc – The model context.
- Returns:
A list of column expressions.
- to_props(feature, mc)¶
- to_record(feature, mc)¶
- class gws.plugin.model_field.file.Props¶
Bases:
gws.base.model.field.Props