Skip to content

Commit

Permalink
Add keep_inactive parameter to setServices
Browse files Browse the repository at this point in the history
  • Loading branch information
xispa committed Nov 5, 2024
1 parent df4bf14 commit f12121e
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 21 deletions.
21 changes: 11 additions & 10 deletions src/senaite/core/content/analysisprofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def getServices(self, active_only=True):
return list(services)

@security.protected(permissions.ModifyPortalContent)
def setServices(self, value):
def setServices(self, value, keep_inactive=True):
"""Set services for the profile
This method accepts either a list of analysis service objects, a list
Expand Down Expand Up @@ -298,15 +298,16 @@ def setServices(self, value):
"Expected object, uid or record, got %r" % type(v))
records.append({"uid": uid, "hidden": hidden})

# always keep inactive services so they come up again when reactivated
uids = [record.get("uid") for record in records]
for record in self.getRawServices():
uid = record.get("uid")
if uid in uids:
continue
obj = api.get_object_by_uid(uid)
if not api.is_active(obj):
records.append(record)
if keep_inactive:
# keep inactive services so they come up again when reactivated
uids = [record.get("uid") for record in records]
for record in self.getRawServices():
uid = record.get("uid")
if uid in uids:
continue
obj = api.get_object(uid)
if not api.is_active(obj):
records.append(record)

mutator = self.mutator("services")
mutator(self, records)
Expand Down
21 changes: 11 additions & 10 deletions src/senaite/core/content/sampletemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ def getServices(self, active_only=True):
return list(services)

@security.protected(permissions.ModifyPortalContent)
def setServices(self, value):
def setServices(self, value, keep_inactive=True):
"""Set services for the template
This method accepts either a list of analysis service objects, a list
Expand Down Expand Up @@ -510,15 +510,16 @@ def setServices(self, value):
"part_id": part_id,
})

# always keep inactive services so they come up again when reactivated
uids = [record.get("uid") for record in records]
for record in self.getRawServices():
uid = record.get("uid")
if uid in uids:
continue
obj = api.get_object_by_uid(uid)
if not api.is_active(obj):
records.append(record)
if keep_inactive:
# keep inactive services so they come up again when reactivated
uids = [record.get("uid") for record in records]
for record in self.getRawServices():
uid = record.get("uid")
if uid in uids:
continue
obj = api.get_object(uid)
if not api.is_active(obj):
records.append(record)

mutator = self.mutator("services")
mutator(self, records)
Expand Down
45 changes: 45 additions & 0 deletions src/senaite/core/tests/doctests/AnalysisProfile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,48 @@ Setting the profile works up to this state:
>>> services = get_services(ar3)
>>> set(map(api.get_uid, services)).issuperset(service_uids1 + [Au.UID()])
True


Inactive services
.................

Inactive services are not returned by default:

>>> Fe in profile1.getServices()
True

>>> success = do_action_for(Fe, "deactivate")
>>> api.get_workflow_status_of(Fe)
'inactive'

>>> Fe in profile1.getServices()
False

But kept just in case we re-activate the service later:

>>> api.get_uid(Fe) in profile1.getServiceUIDs()
True

By default, inactive services are kept as raw data when the value is set:

>>> profile1.setServices([])
>>> Cu in profile1.getServices()
False
>>> Fe in profile1.getServices()
False
>>> api.get_uid(Cu) in profile1.getServiceUIDs()
False
>>> api.get_uid(Fe) in profile1.getServiceUIDs()
True

Unless we use `keep_inactive=False`:

>>> profile1.setServices([], keep_inactive=False)
>>> Cu in profile1.getServices()
False
>>> Fe in profile1.getServices()
False
>>> api.get_uid(Cu) in profile1.getServiceUIDs()
False
>>> api.get_uid(Fe) in profile1.getServiceUIDs()
False
26 changes: 25 additions & 1 deletion src/senaite/core/tests/doctests/SampleTemplate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ Test get/set methods:
Services
^^^^^^^^

Anbalysis Services can be assigned to the Template, so that they are
Analysis Services can be assigned to the Template, so that they are
automatically added when the sample is created.

Each service can be configured for a specific partition and if it should be
Expand Down Expand Up @@ -348,3 +348,27 @@ But are kept as raw data, just in case we re-activate the service later:

>>> api.get_uid(Fe) in template1.getAnalysisServiceUIDs()
True

By default, inactive services are kept as raw data when the value is set:

>>> template1.setServices([])
>>> Cu in template1.getServices()
False
>>> Fe in template1.getServices()
False
>>> api.get_uid(Cu) in template1.getAnalysisServiceUIDs()
False
>>> api.get_uid(Fe) in template1.getAnalysisServiceUIDs()
True

Unless we use `keep_inactive=False`:

>>> template1.setServices([], keep_inactive=False)
>>> Cu in template1.getServices()
False
>>> Fe in template1.getServices()
False
>>> api.get_uid(Cu) in template1.getAnalysisServiceUIDs()
False
>>> api.get_uid(Fe) in template1.getAnalysisServiceUIDs()
False

0 comments on commit f12121e

Please sign in to comment.