Skip to content

Commit

Permalink
Merge pull request #691 from aarhusstadsarkiv/dev-matca
Browse files Browse the repository at this point in the history
v2.1.0
  • Loading branch information
clausjuhl authored Aug 8, 2024
2 parents b55691b + d4ea040 commit 3077347
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 2 deletions.
2 changes: 1 addition & 1 deletion digiarch/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.0.2"
__version__ = "2.1.0"
56 changes: 56 additions & 0 deletions digiarch/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,57 @@ def app_edit_rename(
handle_end(ctx, database, exception, logger)


@app_edit.command("lock", no_args_is_help=True, short_help="Lock files.")
@argument("root", nargs=1, type=ClickPath(exists=True, file_okay=False, writable=True, resolve_path=True))
@argument_ids(True)
@argument("reason", nargs=1, type=str, required=True)
@option("--lock/--unlock", is_flag=True, default=True, show_default=True, help="Lock or unlock files.")
@pass_context
def app_edit_lock(
ctx: Context,
root: str,
ids: tuple[str],
id_type: str,
id_files: bool,
reason: str,
lock: bool,
) -> None:
"""
Lock files from being edited by reidentify.
To unlock files, use the --unlock option.
"""
database_path: Path = Path(root) / "_metadata" / "files.db"

if not database_path.is_file():
raise FileNotFoundError(database_path)

program_name: str = ctx.find_root().command.name
logger: Logger = setup_logger(program_name, files=[database_path.parent / f"{program_name}.log"], streams=[stdout])

if id_type in ("warnings",):
where: str = f"{id_type} like '%\"' || ? || '\"%'"
elif id_type.endswith("-like"):
id_type = id_type.removesuffix("-like")
where: str = f"{id_type} like ?"
else:
where: str = f"{id_type} = ?"

with FileDB(database_path) as database:
handle_start(ctx, database, logger)

with ExceptionManager(BaseException) as exception:
for file_id in ids:
for file in database.files.select(where=where, parameters=[str(file_id)]):
event = HistoryEntry.command_history(ctx, "file.lock", file.uuid, [file.lock, lock], reason)
file.lock = lock
database.files.update(file)
database.history.insert(event)
event.log(INFO)

handle_end(ctx, database, exception, logger)


@app_edit.command("rollback", no_args_is_help=True, short_help="Roll back edits.")
@argument("root", nargs=1, type=ClickPath(exists=True, file_okay=False, writable=True, resolve_path=True))
@argument(
Expand Down Expand Up @@ -1112,6 +1163,11 @@ def app_edit_rollback(ctx: Context, root: str, time_from: datetime, time_to: dat
except DatabaseError:
file.get_absolute_path().rename(file.get_absolute_path().with_name(new_name))
file.relative_path = file.relative_path.with_name(new_name)
elif event_command == f"{program_name}.{app_edit.name}.{app_edit_lock.name}":
file = database.files.select(where="uuid = ?", parameters=[str(event.uuid)]).fetchone()
if file:
file.lock = event.data[0]
database.files.update(file)

if file:
command.uuid = file.uuid
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "digiarch"
version = "2.0.2"
version = "2.1.0"
description = "Tools for the Digital Archive Project at Aarhus Stadsarkiv"
authors = ["Aryan Muhammadi Landi <[email protected]>", "Nina Jensen <[email protected]>", "Aarhus Stadsarkiv <[email protected]>"]
license = "GPL-3.0"
Expand Down
37 changes: 37 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from digiarch.cli import app
from digiarch.cli import app_edit
from digiarch.cli import app_edit_action
from digiarch.cli import app_edit_lock
from digiarch.cli import app_edit_remove
from digiarch.cli import app_edit_rename
from digiarch.cli import app_edit_rollback
Expand Down Expand Up @@ -559,6 +560,42 @@ def test_edit_remove_ids_file(tests_folder: Path, files_folder: Path, files_fold
assert history_edit.reason == test_reason


def test_edit_lock(tests_folder: Path, files_folder: Path, files_folder_copy: Path):
database_path: Path = files_folder / "_metadata" / "files.db"
database_path_copy: Path = files_folder_copy / database_path.relative_to(files_folder)
database_path_copy.parent.mkdir(parents=True, exist_ok=True)
copy(database_path, database_path_copy)

with FileDB(database_path_copy) as database:
files: list[File] = list(database.files.select(order_by=[("random()", "asc")], limit=3))

test_reason: str = "lock"

args: list[str] = [
app_edit.name,
app_edit_lock.name,
str(files_folder_copy),
"--uuid",
*(str(f.uuid) for f in files),
test_reason,
]

app.main(args, standalone_mode=False)

with FileDB(database_path_copy) as database:
for file in files:
file_new: Optional[File] = database.files.select(where="uuid = ?", parameters=[str(file.uuid)]).fetchone()
assert file_new is not None
assert file_new.lock is True

history_edit: Optional[HistoryEntry] = database.history.select(
where="uuid = ? and operation like ? || '%'",
parameters=[str(file.uuid), "digiarch.edit.lock:"],
).fetchone()
assert history_edit is not None
assert history_edit.reason == test_reason


# noinspection DuplicatedCode
def test_edit_rollback_action(tests_folder: Path, files_folder: Path, files_folder_copy: Path):
database_path: Path = files_folder / "_metadata" / "files.db"
Expand Down

0 comments on commit 3077347

Please sign in to comment.