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

Filter only qgis files for editor #208

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
42 changes: 3 additions & 39 deletions mergin/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,7 @@
Returns:
bool: True if the file change should be disallowed, False otherwise.
"""
disallowed_added_changes: Callable[[dict], bool] = 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: Callable[[dict], bool] = (
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: Callable[[dict], bool] = (
lambda change: is_qgis_file(change["path"]) or is_mergin_config(change["path"]) or is_versioned_file(change["path"])
)
_disallowed_changes: Callable[[dict], bool] = lambda change: is_qgis_file(change["path"])


def is_editor_enabled(mc, project_info: dict) -> bool:
Expand All @@ -65,14 +34,9 @@ def _apply_editor_filters(changes: dict[str, list[dict]]) -> dict[str, list[dict
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))
changes["updated"] = list(filterfalse(_disallowed_changes, updated))
return changes


Expand Down Expand Up @@ -106,4 +70,4 @@ def prevent_conflicted_copy(path: str, mc, project_info: dict) -> bool:
Returns:
bool: True if the file path should be prevented from ceating conflicted copy, False otherwise.
"""
return is_editor_enabled(mc, project_info) and any([is_qgis_file(path), is_mergin_config(path)])
return is_editor_enabled(mc, project_info) and any([is_qgis_file(path)])
59 changes: 18 additions & 41 deletions mergin/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2545,28 +2545,7 @@ def test_editor(mc: MerginClient):
"removed": [{"path": "/folder/project.removed.qgs"}],
}
qgs_changeset = filter_changes(mc, project_info, qgs_changeset)
assert sum(len(v) for v in qgs_changeset.values()) == 0

mergin_config_changeset = {
"added": [{"path": "/.mergin/mergin-config.json"}],
"updated": [{"path": "/.mergin/mergin-config.json"}],
"removed": [{"path": "/.mergin/mergin-config.json"}],
}
mergin_config_changeset = filter_changes(mc, project_info, mergin_config_changeset)
assert sum(len(v) for v in mergin_config_changeset.values()) == 0

gpkg_changeset = {
"added": [{"path": "/.mergin/data.gpkg"}],
"updated": [
{"path": "/.mergin/conflict-data.gpkg"},
{"path": "/.mergin/data.gpkg", "diff": {}},
],
"removed": [{"path": "/.mergin/data.gpkg"}],
}
gpkg_changeset = filter_changes(mc, project_info, gpkg_changeset)
assert sum(len(v) for v in gpkg_changeset.values()) == 2
assert gpkg_changeset["added"][0]["path"] == "/.mergin/data.gpkg"
assert gpkg_changeset["updated"][0]["path"] == "/.mergin/data.gpkg"
assert sum(len(v) for v in qgs_changeset.values()) == 2


def test_editor_push(mc: MerginClient, mc2: MerginClient):
Expand All @@ -2591,24 +2570,21 @@ def test_editor_push(mc: MerginClient, mc2: MerginClient):
qgs_file_name = "test.qgs"
txt_file_name = "test.txt"
gpkg_file_name = "base.gpkg"
files_to_push = [qgs_file_name, txt_file_name, gpkg_file_name]
files_to_push = [txt_file_name, gpkg_file_name]
for file in files_to_push:
shutil.copy(os.path.join(TEST_DATA_DIR, file), project_dir)
# it's possible to push allowed files if editor
mc2.push_project(project_dir)
project_info = mc2.project_info(test_project_fullname)
assert len(project_info.get("files")) == len(files_to_push) - 1 # ggs is not pushed
assert len(project_info.get("files")) == len(files_to_push) # ggs is not pushed
# find pushed files in server
assert any(file["path"] == qgs_file_name for file in project_info.get("files")) is False
assert any(file["path"] == txt_file_name for file in project_info.get("files")) is True
assert any(file["path"] == gpkg_file_name for file in project_info.get("files")) is True
pull_changes, push_changes, push_changes_summary = mc.project_status(project_dir)
assert not sum(len(v) for v in pull_changes.values())
assert sum(len(v) for v in push_changes.values()) == 1
# ggs is still waiting to push
assert any(file["path"] == qgs_file_name for file in push_changes.get("added")) is True
assert not sum(len(v) for v in push_changes.values())

# editor is trying to psuh row to gpkg file -> it's possible
# editor is trying to push row to gpkg file -> it's possible
shutil.copy(
os.path.join(TEST_DATA_DIR, "inserted_1_A.gpkg"),
os.path.join(project_dir, gpkg_file_name),
Expand All @@ -2619,19 +2595,20 @@ def test_editor_push(mc: MerginClient, mc2: MerginClient):
assert any(file["path"] == gpkg_file_name for file in project_info.get("files")) is True
assert any(file["path"] == gpkg_file_name for file in push_changes.get("updated")) is False

# editor is trying to insert tables to gpkg file
shutil.copy(
os.path.join(TEST_DATA_DIR, "two_tables.gpkg"),
os.path.join(project_dir, gpkg_file_name),
)
mc2.push_project(project_dir)
# add qgis file as editor
shutil.copy(os.path.join(TEST_DATA_DIR, qgs_file_name), project_dir)
with pytest.raises(ClientError, match=f"You do not have permissions for this project"):
mc2.push_project(project_dir)
mc.push_project(project_dir)

# editor is trying to update qgis file
with open(os.path.join(project_dir, qgs_file_name), "a") as f:
f.write("Editor is here!")
project_info = mc2.project_info(test_project_fullname)
pull_changes, push_changes, push_changes_summary = mc.project_status(project_dir)
assert not sum(len(v) for v in pull_changes.values())
# gpkg was filter by editor_handler in push_project, because new tables added
assert sum(len(v) for v in push_changes.values()) == 2
# ggs and gpkg are still waiting to push
assert any(file["path"] == qgs_file_name for file in push_changes.get("added")) is True
assert any(file["path"] == gpkg_file_name for file in push_changes.get("updated")) is True
# ggs is still waiting to push
assert any(file["path"] == qgs_file_name for file in push_changes.get("updated")) is True

# push as owner do cleanup local changes and preparation to conflicited copy simulate
mc.push_project(project_dir)

Expand Down
Loading