From f12121e96ec293d7452b74d3025d601c55b0d2dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jordi=20Puiggen=C3=A9?= Date: Tue, 5 Nov 2024 11:30:35 +0100 Subject: [PATCH] Add keep_inactive parameter to setServices --- src/senaite/core/content/analysisprofile.py | 21 ++++----- src/senaite/core/content/sampletemplate.py | 21 ++++----- .../core/tests/doctests/AnalysisProfile.rst | 45 +++++++++++++++++++ .../core/tests/doctests/SampleTemplate.rst | 26 ++++++++++- 4 files changed, 92 insertions(+), 21 deletions(-) diff --git a/src/senaite/core/content/analysisprofile.py b/src/senaite/core/content/analysisprofile.py index 09a3f1169e..63f19e6524 100644 --- a/src/senaite/core/content/analysisprofile.py +++ b/src/senaite/core/content/analysisprofile.py @@ -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 @@ -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) diff --git a/src/senaite/core/content/sampletemplate.py b/src/senaite/core/content/sampletemplate.py index 2116cad351..b8faa4eb27 100644 --- a/src/senaite/core/content/sampletemplate.py +++ b/src/senaite/core/content/sampletemplate.py @@ -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 @@ -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) diff --git a/src/senaite/core/tests/doctests/AnalysisProfile.rst b/src/senaite/core/tests/doctests/AnalysisProfile.rst index c84bdf6ab9..93935e7b7a 100644 --- a/src/senaite/core/tests/doctests/AnalysisProfile.rst +++ b/src/senaite/core/tests/doctests/AnalysisProfile.rst @@ -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 diff --git a/src/senaite/core/tests/doctests/SampleTemplate.rst b/src/senaite/core/tests/doctests/SampleTemplate.rst index 4b995da3ec..cbdf4b2da3 100644 --- a/src/senaite/core/tests/doctests/SampleTemplate.rst +++ b/src/senaite/core/tests/doctests/SampleTemplate.rst @@ -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 @@ -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