Passed
Pull Request — 2.x (#1778)
by Ramon
05:34
created

senaite.core.datamanagers.sample   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 81
Duplicated Lines 25.93 %

Importance

Changes 0
Metric Value
wmc 11
eloc 41
dl 21
loc 81
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A SampleDataManager.is_field_readable() 0 4 1
A SampleDataManager.is_field_writeable() 0 4 1
A SampleDataManager.set() 0 28 4
A SampleDataManager.fields() 0 3 1
A SampleDataManager.get() 21 21 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
# -*- coding: utf-8 -*-
2
3
from AccessControl import Unauthorized
4
from bika.lims import api
5
from bika.lims.interfaces import IAnalysisRequest
6
from Products.Archetypes.utils import mapply
7
from senaite.core import logger
8
from senaite.core.datamanagers import DataManager
9
from zope.component import adapter
10
11
12
@adapter(IAnalysisRequest)
13
class SampleDataManager(DataManager):
14
    """Data Manager for Samples
15
    """
16
17
    @property
18
    def fields(self):
19
        return api.get_fields(self.context)
20
21
    def is_field_readable(self, field):
22
        """Checks if the field is readable
23
        """
24
        return field.checkPermission("get", self.context)
25
26
    def is_field_writeable(self, field):
27
        """Checks if the field is writeable
28
        """
29
        return field.checkPermission("set", self.context)
30
31 View Code Duplication
    def get(self, name):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
32
        """Get sample field
33
        """
34
        # schema field
35
        field = self.fields.get(name)
36
37
        # check if the field exists
38
        if field is None:
39
            raise AttributeError("Field '{}' not found".format(name))
40
41
        # Check the permission of the field
42
        if not self.is_field_readable(field):
43
            raise Unauthorized("Field '{}' not readable!".format(name))
44
45
        # return the value with the field accessor
46
        if hasattr(field, "getAccessor"):
47
            accessor = field.getAccessor(self.context)
48
            return accessor()
49
        else:
50
            # Set the value on the field directly
51
            return field.get(self.context)
52
53
    def set(self, name, value):
54
        """Set sample field or analysis result
55
        """
56
        # set of updated objects
57
        updated_objects = set()
58
59
        # schema field
60
        field = self.fields.get(name)
61
62
        if field is None:
63
            raise AttributeError("Field '{}' not found".format(name))
64
65
        # Check the permission of the field
66
        if not self.is_field_writeable(field):
67
            logger.error("Field '{}' not writeable!".format(name))
68
            return []
69
        # get the field mutator (works only for AT content types)
70
        if hasattr(field, "getMutator"):
71
            mutator = field.getMutator(self.context)
72
            mapply(mutator, value)
73
        else:
74
            # Set the value on the field directly
75
            field.set(self.context, value)
76
77
        updated_objects.add(self.context)
78
79
        # return a unified list of the updated objects
80
        return list(updated_objects)
81