Passed
Push — 2.x ( 8a073a...44914a )
by Ramon
05:26
created

senaite.core.datamanagers.analysisservice   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 98
Duplicated Lines 34.69 %

Importance

Changes 0
Metric Value
wmc 14
eloc 49
dl 34
loc 98
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A AnalysisServiceDataManager.fields() 0 3 1
A AnalysisServiceDataManager.set() 0 28 4
A AnalysisServiceDataManager.is_field_writeable() 0 6 2
A AnalysisServiceDataManager.get_field_by_name() 13 13 2
A AnalysisServiceDataManager.get() 21 21 4
A AnalysisServiceDataManager.is_field_readable() 0 4 1

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