Skip to content

Commit

Permalink
dbio.status: add by_who property
Browse files Browse the repository at this point in the history
  • Loading branch information
RayPlante committed Jan 15, 2025
1 parent fc19299 commit 06d9238
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
6 changes: 3 additions & 3 deletions python/nistoar/midas/dap/service/mds3.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ def clear_data(self, id, part=None, message: str=None, _prec=None) -> bool:
part = ("/"+part) if part.startswith("pdr:") else ("."+part)
provact.add_subaction(Action(Action.DELETE, _prec.id+"#data"+part, self.who,
message))
_prec.status.act(self.STATUS_ACTION_CLEAR, "cleared "+what)
_prec.status.act(self.STATUS_ACTION_CLEAR, "cleared "+what, self.who.actor)

else:
nerd.authors.empty()
Expand All @@ -748,7 +748,7 @@ def clear_data(self, id, part=None, message: str=None, _prec=None) -> bool:
if not message:
message = "clearing all NERDm data"
provact = Action(Action.PATCH, _prec.id, self.who, message)
_prec.status.act(self.STATUS_ACTION_CLEAR, "cleared all NERDm data")
_prec.status.act(self.STATUS_ACTION_CLEAR, "cleared all NERDm data", self.who.actor)

except PartNotAccessible:
# client request error; don't record action
Expand All @@ -761,7 +761,7 @@ def clear_data(self, id, part=None, message: str=None, _prec=None) -> bool:
provact.message = "Failed to clear requested NERDm data"
self._record_action(provact)

_prec.status.act(self.STATUS_ACTION_CLEAR, "Failed to clear NERDm data")
_prec.status.act(self.STATUS_ACTION_CLEAR, "Failed to clear NERDm data", self.who.actor)
_prec.set_state(status.EDIT)
_prec.data = self._summarize(nerd)
self._try_save(_prec)
Expand Down
22 changes: 11 additions & 11 deletions python/nistoar/midas/dbio/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def create_record(self, name, data=None, meta=None) -> ProjectRecord:
elif not prec.meta:
prec.meta = self._new_metadata_for(shoulder)
prec.data = self._new_data_for(prec.id, prec.meta)
prec.status.act(self.STATUS_ACTION_CREATE, "draft created")
prec.status.act(self.STATUS_ACTION_CREATE, "draft created", self.who.actor)
if data:
self.update_data(prec.id, data, message=None, _prec=prec) # this will call prec.save()
else:
Expand Down Expand Up @@ -528,7 +528,7 @@ def _save_data(self, indata: Mapping, prec: ProjectRecord,

# update the record status according to the inputs
if action:
prec.status.act(action, message)
prec.status.act(action, message, self.who.actor)
elif message is not None:
prec.message = message
prec.status.set_state(status.EDIT)
Expand Down Expand Up @@ -571,7 +571,7 @@ def clear_data(self, id: str, part: str=None, message: str=None, prec=None) -> b
prec.data = initdata
if message is None:
message = "reset draft to initial defaults"
prec.status.act(self.STATUS_ACTION_CLEAR, message)
prec.status.act(self.STATUS_ACTION_CLEAR, message, self.who.actor)

else:
# clearing only part of the data
Expand All @@ -594,7 +594,7 @@ def clear_data(self, id: str, part: str=None, message: str=None, prec=None) -> b

if message is None:
message = "reset %s to initial defaults" % part
prec.status.act(self.STATUS_ACTION_UPDATE, message)
prec.status.act(self.STATUS_ACTION_UPDATE, message, self.who.actor)

# prep the provenance record
tgt = prec.id
Expand Down Expand Up @@ -673,7 +673,7 @@ def finalize(self, id, message=None, as_version=None, _prec=None) -> status.Reco
raise NotEditable(id)

stat.set_state(status.PROCESSING)
stat.act(self.STATUS_ACTION_FINALIZE, "in progress")
stat.act(self.STATUS_ACTION_FINALIZE, "in progress", self.who.actor)
_prec.save()

try:
Expand All @@ -684,7 +684,7 @@ def finalize(self, id, message=None, as_version=None, _prec=None) -> status.Reco
self._record_action(Action(Action.PROCESS, _prec.id, self.who, emsg,
{"name": "finalize", "errors": ex.errors}))
stat.set_state(status.EDIT)
stat.act(self.STATUS_ACTION_FINALIZE, ex.format_errors())
stat.act(self.STATUS_ACTION_FINALIZE, ex.format_errors(), self.who.actor)
self._try_save(_prec)
raise

Expand All @@ -694,7 +694,7 @@ def finalize(self, id, message=None, as_version=None, _prec=None) -> status.Reco
self._record_action(Action(Action.PROCESS, _prec.id, self.who, emsg,
{"name": "finalize", "errors": [emsg]}))
stat.set_state(status.EDIT)
stat.act(self.STATUS_ACTION_FINALIZE, emsg)
stat.act(self.STATUS_ACTION_FINALIZE, emsg, self.who.actor)
self._try_save(_prec)
raise

Expand All @@ -704,7 +704,7 @@ def finalize(self, id, message=None, as_version=None, _prec=None) -> status.Reco

if reset_state:
stat.set_state(status.READY)
stat.act(self.STATUS_ACTION_FINALIZE, message or defmsg)
stat.act(self.STATUS_ACTION_FINALIZE, message or defmsg, self.who.actor)
_prec.save()

self.log.info("Finalized %s record %s (%s) for %s",
Expand Down Expand Up @@ -822,7 +822,7 @@ def submit(self, id: str, message: str=None, _prec=None) -> status.RecordStatus:
self._record_action(Action(Action.PROCESS, _prec.id, self.who, emsg,
{"name": "submit", "errors": ex.errors}))
stat.set_state(status.EDIT)
stat.act(self.STATUS_ACTION_SUBMIT, ex.format_errors())
stat.act(self.STATUS_ACTION_SUBMIT, ex.format_errors(), self.who.actor)
self._try_save(_prec)
raise

Expand All @@ -831,7 +831,7 @@ def submit(self, id: str, message: str=None, _prec=None) -> status.RecordStatus:
self._record_action(Action(Action.PROCESS, _prec.id, self.who, emsg,
{"name": "submit", "errors": [emsg]}))
stat.set_state(status.EDIT)
stat.act(self.STATUS_ACTION_SUBMIT, emsg)
stat.act(self.STATUS_ACTION_SUBMIT, emsg, self.who.actor)
self._try_save(_prec)
raise

Expand All @@ -840,7 +840,7 @@ def submit(self, id: str, message: str=None, _prec=None) -> status.RecordStatus:
self.dbcli.record_action(Action(Action.PROCESS, _prec.id, self.who, defmsg, {"name": "submit"}))

stat.set_state(status.SUBMITTED)
stat.act(self.STATUS_ACTION_SUBMIT, message or defmsg)
stat.act(self.STATUS_ACTION_SUBMIT, message or defmsg, self.who.actor)
_prec.save()

self.log.info("Submitted %s record %s (%s) for %s",
Expand Down
12 changes: 11 additions & 1 deletion python/nistoar/midas/dbio/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
_modified_p = "modified"
_created_p = "created"
_message_p = "message"
_bywho_p = "byWho"

# Common record actions
#
Expand Down Expand Up @@ -139,6 +140,14 @@ def action(self) -> str:
"""
return self._data[_action_p]

@property
def by_who(self) -> str:
"""
the identifier of the agent that committed the last action applied to the record. If None,
the agent is unknown.
"""
return self._data.get(_bywho_p)

@property
def modified(self) -> float:
"""
Expand Down Expand Up @@ -168,7 +177,7 @@ def message(self) -> str:
def message(self, val):
self._data[_message_p] = val

def act(self, action: str, message: str="", when: float=0):
def act(self, action: str, message: str="", who: str=None, when: float=0):
"""
record the application of a particular action on the record
:param str action: the name of the action being applied
Expand All @@ -190,6 +199,7 @@ def act(self, action: str, message: str="", when: float=0):
self._data[_modified_p] = when
if self._data[_created_p] < 1:
self._data[_created_p] = when
self._data[_bywho_p] = who

def set_state(self, state, when: float=-1):
"""
Expand Down
15 changes: 14 additions & 1 deletion python/tests/nistoar/midas/dbio/test_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def test_act(self):
self.assertEqual(stat.id, "goob")
self.assertEqual(stat.state, status.EDIT)
self.assertEqual(stat.action, status.ACTION_CREATE)
self.assertIsNone(stat.by_who)
self.assertEqual(stat.message, "")
self.assertGreater(stat.since, 0)
self.assertGreater(stat.modified, 0)
Expand All @@ -36,6 +37,7 @@ def test_act(self):
stat.act(Action.PATCH, "made updates")
self.assertEqual(stat.state, status.EDIT)
self.assertEqual(stat.action, Action.PATCH)
self.assertIsNone(stat.by_who)
self.assertEqual(stat.message, "made updates")
self.assertEqual(stat.modified, 0)
self.assertGreater(stat.created, 0)
Expand All @@ -46,14 +48,25 @@ def test_act(self):
stat.act(Action.PUT)
self.assertEqual(stat.state, status.EDIT)
self.assertEqual(stat.action, Action.PUT)
self.assertIsNone(stat.by_who)
self.assertEqual(stat.message, "")
self.assertEqual(stat.modified, 0)
self.assertNotEqual(stat.since_date, "pending")
self.assertEqual(stat.modified_date, "pending")

stat.act(Action.COMMENT, "Whoa", -1)
stat.act(Action.COMMENT, "Whoa", when=-1)
self.assertEqual(stat.state, status.EDIT)
self.assertEqual(stat.action, Action.COMMENT)
self.assertIsNone(stat.by_who)
self.assertEqual(stat.message, "Whoa")
self.assertGreater(stat.modified, stat.since)
self.assertNotEqual(stat.since_date, "pending")
self.assertNotEqual(stat.modified_date, "pending")

stat.act(Action.COMMENT, "Whoa", "nstr1", -1)
self.assertEqual(stat.state, status.EDIT)
self.assertEqual(stat.action, Action.COMMENT)
self.assertEqual(stat.by_who, "nstr1")
self.assertEqual(stat.message, "Whoa")
self.assertGreater(stat.modified, stat.since)
self.assertNotEqual(stat.since_date, "pending")
Expand Down

0 comments on commit 06d9238

Please sign in to comment.