8. Lock and timestamp
Locking, unlocking, and timestamping entries, and how to safely change a locked entry.
The short version
python
with elab.items.entry(item_id).unlocked() as entry:
entry.fields["purity"] = "99.9%"
entry.files.upload("coa.pdf")The lock is removed when entering the with block and restored when leaving it (also on exceptions). If the entry is not locked, nothing happens.
unlocked() options
python
# Always lock when done, regardless of the original state (to finalize entries)
with elab.experiments.entry(experiment_id).unlocked(relock=True) as entry:
entry.patch(body=new_body)relock | When leaving the with block |
|---|---|
None (default) | Restore the original state (re-lock if it was locked) |
True | Lock regardless of the original state |
False | Do not lock |
Calling lock / unlock / timestamp directly
python
# Experiment
elab.experiments.entry(experiment_id).timestamp()
elab.experiments.entry(experiment_id).lock()
elab.experiments.entry(experiment_id).unlock()
# Resource / Database item
elab.items.entry(item_id).timestamp()
elab.items.entry(item_id).lock()
elab.items.entry(item_id).unlock()
elab.items.entry(item_id).is_locked() # whether it is locked now (True / False)Typically you run lock() or timestamp() after updating metadata, attaching files, adding images, creating links, and so on.
About force (the lock action toggles in eLabFTW 6.x)
By default (force=True), lock() / unlock() use the forcelock / forceunlock actions, so calling them repeatedly gives the same result.
With force=False, the current lock state is checked before sending the lock action. This is because in eLabFTW 6.x the lock action toggles the state, and 6.0.6 has no unlock action (it fails with Invalid update target.).
Pitfalls
- A timestamp cannot be undone. Check the target before running it.
Next: 9. HTML tables and steps.