Code Duplication    Length = 33-35 lines in 2 locations

src/senaite/core/content/worksheettemplate.py 1 location

@@ 490-524 (lines=35) @@
487
        services_uids = self.getRawServices()
488
        return list(map(api.get_object, services_uids))
489
490
    @security.protected(permissions.ModifyPortalContent)
491
    def setServices(self, value):
492
        """Set services for the template
493
494
        This method accepts either a list of analysis service objects, a list
495
        of analysis service UIDs or a list of analysis profile service records
496
        containing the key `uid`:
497
498
        >>> self.setServices([<AnalysisService at ...>, ...])
499
        >>> self.setServices(['353e1d9bd45d45dbabc837114a9c41e6', '...', ...])
500
        >>> self.setServices([{'uid': '...'}, ...])
501
502
        Raises a TypeError if the value does not match any allowed type.
503
        """
504
        if not isinstance(value, list):
505
            value = [value]
506
507
        records = []
508
        for v in value:
509
            uid = ""
510
            if isinstance(v, dict):
511
                uid = api.get_uid(v.get("uid"))
512
            elif api.is_object(v):
513
                uid = api.get_uid(v)
514
            elif api.is_uid(v):
515
                uid = v
516
            else:
517
                raise TypeError(
518
                    "Expected object, uid or record, got %r" % type(v))
519
            records.append({
520
                "uid": uid,
521
            })
522
523
        mutator = self.mutator("services")
524
        mutator(self, records)
525
526
    # BBB: AT schema field property
527
    Services = property(getServices, setServices)

src/senaite/core/content/analysisprofile.py 1 location

@@ 267-299 (lines=33) @@
264
        service_uids = map(lambda r: r.get("uid"), records)
265
        return list(map(api.get_object, service_uids))
266
267
    @security.protected(permissions.ModifyPortalContent)
268
    def setServices(self, value):
269
        """Set services for the profile
270
271
        This method accepts either a list of analysis service objects, a list
272
        of analysis service UIDs or a list of analysis profile service records
273
        containing the keys `uid` and `hidden`:
274
275
        >>> self.setServices([<AnalysisService at ...>, ...])
276
        >>> self.setServices(['353e1d9bd45d45dbabc837114a9c41e6', '...', ...])
277
        >>> self.setServices([{'hidden': False, 'uid': '...'}, ...])
278
279
        Raises a TypeError if the value does not match any allowed type.
280
        """
281
        if not isinstance(value, list):
282
            value = [value]
283
        records = []
284
        for v in value:
285
            uid = None
286
            hidden = False
287
            if isinstance(v, dict):
288
                uid = v.get("uid")
289
                hidden = v.get("hidden", False)
290
            elif api.is_object(v):
291
                uid = api.get_uid(v)
292
            elif api.is_uid(v):
293
                uid = v
294
            else:
295
                raise TypeError(
296
                    "Expected object, uid or record, got %r" % type(v))
297
            records.append({"uid": uid, "hidden": hidden})
298
        mutator = self.mutator("services")
299
        mutator(self, records)
300
301
    # BBB: AT schema field property
302
    Service = Services = property(getServices, setServices)