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.Data

File sent from the client.

content: bytes

File content.

name: str

File name.

class gws.plugin.model_field.file.Config

Bases: gws.base.model.field.Config

Field for files stored in the database or the filesystem.

contentColumn: str = ''

Column name for the file content, if stored in the database.

nameColumn: str = ''

Column name for the file name, if stored in the database or filesystem.

pathColumn: str = ''

Column name for the file path, if stored in the filesystem.

class gws.plugin.model_field.file.FileInputProps(*args, **kwargs)

Bases: gws.Data

File uploaded from the client.

content: bytes

File content.

name: str

File name.

class gws.plugin.model_field.file.FileValue(*args, **kwargs)

Bases: gws.Data

Value of a file field.

content: bytes

File content, if loaded.

name: str

File name.

path: str

File path in the filesystem.

size: int

File size in bytes.

class gws.plugin.model_field.file.Object

Bases: gws.base.model.field.Object

File 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 contentColumn nor pathColumn is 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 webFile command. 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()
prop_to_python(feature, value, mc) → FileValue
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

class gws.plugin.model_field.file.ServerFileProps(*args, **kwargs)

Bases: gws.Data

File description sent to the client.

downloadUrl: str

URL to download the file, empty if there is no project.

extension: str

File extension derived from the MIME type.

label: str

Label to display, the file name.

previewUrl: str

URL of a thumbnail preview, empty if the file cannot be previewed.

size: int

File size in bytes.