Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor sync handling #207

Merged
merged 5 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions mergin/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ def set_project_access(self, project_path, access):
:param project_path: project full name (<namespace>/<name>)
:param access: dict <readersnames, editorsnames, writersnames, ownersnames> -> list of str username we want to give access to (editorsnames are only supported on servers at version 2024.4.0 or later)
"""
if "editorsnames" in access and not is_version_acceptable(self.server_version(), "2024.4"):
if "editorsnames" in access and not self.has_editor_support():
raise NotImplementedError("Editors are only supported on servers at version 2024.4.0 or later")

if not self._user_info:
Expand All @@ -790,7 +790,7 @@ def add_user_permissions_to_project(self, project_path, usernames, permission_le
Editor permission_level is only supported on servers at version 2024.4.0 or later.
"""
if permission_level not in ["owner", "reader", "writer", "editor"] or (
permission_level == "editor" and not is_version_acceptable(self.server_version(), "2024.4")
permission_level == "editor" and not self.has_editor_support()
):
raise ClientError("Unsupported permission level")

Expand Down Expand Up @@ -1235,3 +1235,9 @@ def download_files(
job = download_files_async(self, project_dir, file_paths, output_paths, version=version)
pull_project_wait(job)
download_files_finalize(job)

def has_editor_support(self):
"""
Returns whether the server version is acceptable for editor support.
"""
return is_version_acceptable(self.server_version(), "2024.4.0")
18 changes: 11 additions & 7 deletions mergin/client_pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from .common import CHUNK_SIZE, ClientError
from .merginproject import MerginProject
from .utils import save_to_file, get_versions_with_file_changes
from .utils import save_to_file


# status = download_project_async(...)
Expand Down Expand Up @@ -340,7 +340,7 @@ def __init__(
mp,
project_info,
basefiles_to_patch,
user_name,
mc,
):
self.project_path = project_path
self.pull_changes = (
Expand All @@ -358,7 +358,7 @@ def __init__(
self.basefiles_to_patch = (
basefiles_to_patch # list of tuples (relative path within project, list of diff files in temp dir to apply)
)
self.user_name = user_name
self.mc = mc

def dump(self):
print("--- JOB ---", self.total_size, "bytes")
Expand All @@ -372,6 +372,10 @@ def dump(self):
print("- {} {} {} {}".format(item.file_path, item.version, item.part_index, item.size))
print("--- END ---")

def apply_changes(self):
"""Apply changes to the project directory with MerginProject.apply_pull_changes"""
return self.mp.apply_pull_changes(self.pull_changes, self.temp_dir, self.project_info, self.mc)


def pull_project_async(mc, directory):
"""
Expand Down Expand Up @@ -494,7 +498,7 @@ def pull_project_async(mc, directory):
mp,
server_info,
basefiles_to_patch,
mc.username(),
mc,
)

# start download
Expand Down Expand Up @@ -570,7 +574,7 @@ def merge(self):
raise ClientError("Download of file {} failed. Please try it again.".format(self.dest_file))


def pull_project_finalize(job):
def pull_project_finalize(job: PullJob):
"""
To be called when pull in the background is finished and we need to do the finalization (merge chunks etc.)

Expand Down Expand Up @@ -623,7 +627,7 @@ def pull_project_finalize(job):
raise ClientError("Cannot patch basefile {}! Please try syncing again.".format(basefile))

try:
conflicts = job.mp.apply_pull_changes(job.pull_changes, job.temp_dir, job.user_name)
conflicts = job.apply_changes()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to keep the direct call to job.mp.apply_pull_changes() instead of introducing a new method in PullJob (it is meant just as a structure to keep everything that is needed for a pull)

except Exception as e:
job.mp.log.error("Failed to apply pull changes: " + str(e))
job.mp.log.info("--- pull aborted")
Expand Down Expand Up @@ -721,7 +725,7 @@ def download_diffs_async(mc, project_directory, file_path, versions):
mp,
server_info,
{},
mc.username(),
mc,
)

# start download
Expand Down
18 changes: 14 additions & 4 deletions mergin/client_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from .common import UPLOAD_CHUNK_SIZE, ClientError
from .merginproject import MerginProject
from .editor import filter_changes


class UploadJob:
Expand Down Expand Up @@ -66,7 +67,11 @@ def upload_blocking(self, mc, mp):
mp.log.debug(f"Uploading {self.file_path} part={self.chunk_index}")

headers = {"Content-Type": "application/octet-stream"}
resp = mc.post("/v1/project/push/chunk/{}/{}".format(self.transaction_id, self.chunk_id), data, headers)
resp = mc.post(
"/v1/project/push/chunk/{}/{}".format(self.transaction_id, self.chunk_id),
data,
headers,
)
resp_dict = json.load(resp)
mp.log.debug(f"Upload finished: {self.file_path}")
if not (resp_dict["size"] == len(data) and resp_dict["checksum"] == checksum.hexdigest()):
Expand All @@ -91,12 +96,12 @@ def push_project_async(mc, directory):
mp.log.info(f"--- start push {project_path}")

try:
server_info = mc.project_info(project_path)
project_info = mc.project_info(project_path)
except ClientError as err:
mp.log.error("Error getting project info: " + str(err))
mp.log.info("--- push aborted")
raise
server_version = server_info["version"] if server_info["version"] else "v0"
server_version = project_info["version"] if project_info["version"] else "v0"

mp.log.info(f"got project info: local version {local_version} / server version {server_version}")

Expand All @@ -116,6 +121,7 @@ def push_project_async(mc, directory):
)

changes = mp.get_push_changes()
changes = filter_changes(mc, project_info, changes)
mp.log.debug("push changes:\n" + pprint.pformat(changes))

tmp_dir = tempfile.TemporaryDirectory(prefix="mergin-py-client-")
Expand Down Expand Up @@ -143,7 +149,11 @@ def push_project_async(mc, directory):
data = {"version": local_version, "changes": changes}

try:
resp = mc.post(f"/v1/project/push/{project_path}", data, {"Content-Type": "application/json"})
resp = mc.post(
f"/v1/project/push/{project_path}",
data,
{"Content-Type": "application/json"},
)
except ClientError as err:
mp.log.error("Error starting transaction: " + str(err))
mp.log.info("--- push aborted")
Expand Down
94 changes: 94 additions & 0 deletions mergin/editor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
from itertools import filterfalse

from .utils import is_mergin_config, is_qgis_file, is_versioned_file

EDITOR_ROLE_NAME = "editor"

"""
Determines whether a given file change should be disallowed based on the file path.

Returns:
bool: True if the file change should be disallowed, False otherwise.
"""
_disallowed_added_changes = lambda change: is_qgis_file(change["path"]) or is_mergin_config(change["path"])
"""
Determines whether a given file change should be disallowed from being updated.

The function checks the following conditions:
- If the file path matches a QGIS file
- If the file path matches a Mergin configuration file
- If the file is versioned and the change does not have a diff

Returns:
bool: True if the change should be disallowed, False otherwise.
"""
_disallowed_updated_changes = (
lambda change: is_qgis_file(change["path"])
or is_mergin_config(change["path"])
or (is_versioned_file(change["path"]) and change.get("diff") is None)
)
"""
Determines whether a given file change should be disallowed from being removed.

The function checks if the file path of the change matches any of the following conditions:
- The file is a QGIS file (e.g. .qgs, .qgz)
- The file is a Mergin configuration file (mergin-config.json)
- The file is a versioned file (.gpkg, .sqlite)

If any of these conditions are met, the change is considered disallowed from being removed.
"""
_disallowed_removed_changes = (
lambda change: is_qgis_file(change["path"]) or is_mergin_config(change["path"]) or is_versioned_file(change["path"])
)
wonder-sk marked this conversation as resolved.
Show resolved Hide resolved


def is_editor_enabled(mc, project_info: dict) -> bool:
"""
The function checks if the server supports editor access, and if the current user's project role matches the expected role name for editors.
"""
server_support = mc.has_editor_support()
project_role = project_info.get("role")

return server_support and project_role == EDITOR_ROLE_NAME


def _apply_editor_filters(changes: dict[str, list[dict]]) -> dict[str, list[dict]]:
"""
Applies editor-specific filters to the changes dictionary, removing any changes to files that are not in the editor's list of allowed files.

Args:
changes (dict[str, list[dict]]): A dictionary containing the added, updated, and removed changes.

Returns:
dict[str, list[dict]]: The filtered changes dictionary.
"""
added = changes.get("added", [])
updated = changes.get("updated", [])
removed = changes.get("removed", [])

# filter out files that are not in the editor's list of allowed files
changes["added"] = list(filterfalse(_disallowed_added_changes, added))
changes["updated"] = list(filterfalse(_disallowed_updated_changes, updated))
changes["removed"] = list(filterfalse(_disallowed_removed_changes, removed))
return changes


def filter_changes(mc, project_info: dict, changes: dict[str, list[dict]]) -> dict[str, list[dict]]:
"""
Filters the given changes dictionary based on the editor's enabled state.

If the editor is not enabled, the changes dictionary is returned as-is. Otherwise, the changes are passed through the `_apply_editor_filters` method to apply any configured filters.

Args:
changes (dict[str, list[dict]]): A dictionary mapping file paths to lists of change dictionaries.

Returns:
dict[str, list[dict]]: The filtered changes dictionary.
"""
if not is_editor_enabled(mc, project_info):
return changes
return _apply_editor_filters(changes)


def prevent_conflicted_copy(path: str, mc, project_info: dict) -> bool:
wonder-sk marked this conversation as resolved.
Show resolved Hide resolved
return is_editor_enabled(mc, project_info) and any([is_qgis_file(path), is_mergin_config(path)])
Loading
Loading