gws.plugin.qfieldcloud.patcher

Applies changes and file uploads from QField to the database.

Source code: gws.plugin.qfieldcloud.patcher

Module Contents

class gws.plugin.qfieldcloud.patcher.Args

Bases: gws.Data

Arguments for the patcher.

baseDir: str

Base directory (not used by the patcher).

caps: gws.plugin.qfieldcloud.caps.Caps

Capabilities of the QField project.

changes: list[Change]

Changes to apply.

db: gws.DatabaseProvider

Database provider of the models, used for the transaction.

fileContent: bytes

Content of an uploaded file.

filePath: str

Path of an uploaded file, as sent by QField.

project: gws.Project | None

GWS project context.

qfcProject: gws.plugin.qfieldcloud.core.QfcProject

QField project.

user: gws.User

User who sent the changes.

class gws.plugin.qfieldcloud.patcher.Change

Bases: gws.Data

A single change, extracted from a QField delta.

layerUid: str

Id of the QGIS layer.

newAtts: dict

New attribute values.

oldAtts: dict

Old attribute values, used to find the primary key of updated and deleted features.

type: ChangeType

Change type.

uid: str

Delta uuid.

wkt: str

New geometry as WKT, or an empty string.

class gws.plugin.qfieldcloud.patcher.ChangeType

Bases: gws.Enum

Type of a change, as in the delta method.

create = 'create'

Create a new feature.

delete = 'delete'

Delete a feature.

patch = 'patch'

Update an existing feature.

class gws.plugin.qfieldcloud.patcher.Object

Patcher, applies changes and file uploads from QField.

Changes are converted to model operations, grouped by model, and passed to the model methods create_feature, update_feature and delete_feature. All changes are applied in one transaction: if any change fails, including changes for unknown or non-editable layers and updates or deletions of features that do not exist, nothing is written.

args: Args

Patcher arguments.

caps: gws.plugin.qfieldcloud.caps.Caps

Capabilities of the QField project.

ops_by_model: dict[str, list[Operation]]

Operations by GeoPackage name of the model.

project: gws.Project | None

GWS project context.

qfcProject: gws.plugin.qfieldcloud.core.QfcProject

QField project.

root: gws.Root

Root object.

user: gws.User

User who sent the changes.

apply_changes(root: gws.Root, args: Args) → bool

Apply the changes in args.changes.

The changes are applied in one transaction, which is rolled back if any change fails. All models must use the database provider args.db.

Parameters:
  • root – Root object.

  • args – Patcher arguments.

Returns:

True if any operations were committed, False if there was nothing to apply.

Raises:

gws.Error – If a change refers to an unknown layer or a missing feature. Database errors are raised as is.

apply_upload(root: gws.Root, args: Args) → bool

Apply the file upload in args.filePath and args.fileContent.

Parameters:
  • root – Root object.

  • args – Patcher arguments.

Returns:

True if a feature for the file was found and updated.

commit_upload(path: str, content: bytes) → bool

Write uploaded file content into the first feature that refers to the file.

Parameters:
  • path – File path, as sent by QField.

  • content – File content.

Returns:

True if a feature was found and updated.

commit_upload_for_model(me: gws.plugin.qfieldcloud.caps.ModelEntry, path: str, content: bytes) → bool

Write uploaded file content into a feature of a model.

Looks for a file field with a nameColumn whose value equals the path, and writes the content into the field’s contentColumn.

Parameters:
  • me – Model entry.

  • path – File path, as sent by QField.

  • content – File content.

Returns:

True if a feature was found and updated.

find_uid_for_path(me: gws.plugin.qfieldcloud.caps.ModelEntry, ff: gws.plugin.model_field.file.Object, path: str, mc: gws.ModelContext) → str | None

Find the feature whose file name column equals the path.

Parameters:
  • me – Model entry.

  • ff – File field.

  • path – File path, as sent by QField.

  • mc – Model context.

Returns:

Primary key of the feature, or None if not found.

get_feature(me: gws.plugin.qfieldcloud.caps.ModelEntry, pk: str) → gws.Feature | None

Read a feature by its primary key.

Parameters:
  • me – Model entry.

  • pk – Primary key.

Returns:

The feature, or None if not found.

perform_operations_for_model(me: gws.plugin.qfieldcloud.caps.ModelEntry, ops: list[Operation])

Perform operations for a model, in the transaction opened by apply_changes.

Parameters:
  • me – Model entry.

  • ops – Operations to perform.

prepare(args: Args)

Store the arguments in the patcher.

Parameters:

args – Patcher arguments.

prepare_change(cc: Change)

Convert a change to an operation and add it to ops_by_model.

The fid_gws attribute is renamed back to fid (see the packager). For creations, an automatic primary key is removed from the attributes.

Parameters:

cc – Change.

Raises:

gws.Error – If the layer is not found or not editable, the geometry field is not found, or the feature to update or delete does not exist.

class gws.plugin.qfieldcloud.patcher.Operation

Bases: gws.Data

A model operation to be committed.

feature: gws.Feature

Feature to create, update or delete.

type: gws.ModelOperation

Operation type.