Passed
Pull Request — 2.x (#1872)
by Jordi
06:13
created

senaite.core.catalog.indexer.senaitesetup   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 17
eloc 77
dl 0
loc 159
rs 10
c 0
b 0
f 0

14 Functions

Rating   Name   Duplication   Size   Complexity  
A price_total() 0 5 1
A department_title() 0 6 1
A to_keywords_list() 0 6 3
A category_uid() 0 6 1
A department_id() 0 6 2
A instrumenttype_title() 0 6 1
A sampletype_uid() 0 11 1
A price() 0 5 1
A instrument_title() 0 9 1
A sampletype_title() 0 9 1
A method_available_uid() 0 13 1
A department_uid() 0 6 1
A listing_searchable_text() 0 22 1
A point_of_capture() 0 5 1
1
# -*- coding: utf-8 -*-
2
3
from bika.lims import api
4
from bika.lims.interfaces import IAnalysisService
5
from bika.lims.interfaces import IHaveAnalysisCategory
6
from bika.lims.interfaces import IHaveDepartment
7
from bika.lims.interfaces import IHaveInstrument
8
from bika.lims.interfaces import IHavePrice
9
from bika.lims.interfaces import IInstrument
10
from bika.lims.interfaces import ISampleTypeAwareMixin
11
from plone.indexer import indexer
12
from Products.CMFCore.interfaces import IContentish
13
from senaite.core.catalog import SETUP_CATALOG
14
from senaite.core.catalog.utils import get_searchable_text_tokens
15
from senaite.core.interfaces import ISetupCatalog
16
17
18
@indexer(ISampleTypeAwareMixin, ISetupCatalog)
19
def sampletype_uid(instance):
20
    """Returns the list of SampleType UIDs the instance is assigned to
21
22
    This is a KeywordIndex, so it will be indexed as a list, even if only one
23
    SampleType can be assigned to the instance. Moreover, if the instance has
24
    no SampleType assigned, it returns a tuple with a None value. This allows
25
    searches for `MissingValue` entries too.
26
    """
27
    sample_type = instance.getSampleType()
28
    return to_keywords_list(sample_type, api.get_uid)
29
30
31
@indexer(ISampleTypeAwareMixin, ISetupCatalog)
32
def sampletype_title(instance):
33
    """Returns a list of titles from SampleType the instance is assigned to
34
35
    If the instance has no sample type assigned, it returns a tuple with
36
    a None value. This allows searches for `MissingValue` entries too.
37
    """
38
    sample_type = instance.getSampleType()
39
    return to_keywords_list(sample_type, api.get_title)
40
41
42
@indexer(IAnalysisService, ISetupCatalog)
43
def method_available_uid(instance):
44
    """Returns a list of Method UIDs that are available for this instance
45
46
    If the instance (AnalysisService) has InstrumentEntryOfResults set to True,
47
    it returns the methods available from the instruments capable to perform
48
    the service, as well as the methods set manually to the analysis.
49
    Otherwise, it returns the methods assigned manually only.
50
51
    If the instance has no available method assigned, it returns a tuple with
52
    a None value. This allows searches for `MissingValue` entries too.
53
    """
54
    return instance.getAvailableMethodUIDs() or (None, )
55
56
57
@indexer(IHaveInstrument, ISetupCatalog)
58
def instrument_title(instance):
59
    """Returns a list of titles from Instrument the instance is assigned to
60
61
    If the instance has no instrument assigned, it returns a tuple with
62
    a None value. This allows searches for `MissingValue` entries too.
63
    """
64
    instrument = instance.getInstrument()
65
    return to_keywords_list(instrument, api.get_title)
66
67
68
@indexer(IHavePrice, ISetupCatalog)
69
def price(instance):
70
    """Returns the price of the instance
71
    """
72
    return instance.getPrice()
73
74
75
@indexer(IHavePrice, ISetupCatalog)
76
def price_total(instance):
77
    """Returns the total price of the instance
78
    """
79
    return instance.getTotalPrice()
80
81
82
@indexer(IInstrument, ISetupCatalog)
83
def instrumenttype_title(instance):
84
    """Returns a list of Instrument Type titles the instance is assigned to
85
    """
86
    instrument_type = instance.getInstrumentType()
87
    return to_keywords_list(instrument_type, api.get_title)
88
89
90
@indexer(IHaveDepartment, ISetupCatalog)
91
def department_uid(instance):
92
    """Returns a list of Department UIDs the instance is assigned to
93
    """
94
    department = instance.getDepartment()
95
    return to_keywords_list(department, api.get_uid)
96
97
98
@indexer(IHaveDepartment, ISetupCatalog)
99
def department_title(instance):
100
    """Returns the title of the Department the instance is assigned to
101
    """
102
    department = instance.getDepartment()
103
    return to_keywords_list(department, api.get_title)
104
105
106
@indexer(IHaveDepartment, ISetupCatalog)
107
def department_id(instance):
108
    """Returns the ID of the Department the instance is assigned to
109
    """
110
    department = instance.getDepartment()
111
    return to_keywords_list(department, lambda dep: dep.getDepartmentID())
112
113
114
@indexer(IAnalysisService, ISetupCatalog)
115
def point_of_capture(instance):
116
    """Returns the point of capture of the instance
117
    """
118
    return instance.getPointOfCapture()
119
120
121
@indexer(IContentish, ISetupCatalog)
122
def listing_searchable_text(instance):
123
    """ Retrieves all the values of metadata columns in the catalog for
124
    wildcard searches
125
    :return: all metadata values joined in a string
126
    """
127
    exclude = ["getObjPositionInParent", ]
128
129
    # Additional non-metadata fields to include in the index
130
    include = ["getCalculation"
131
               "getDepartment",
132
               "getInstrument",
133
               "getInstrumentType",
134
               "getSamplePoint"
135
               "getSampleType",
136
               "getSupplier",
137
               "getManufacturer", ]
138
139
    tokens = get_searchable_text_tokens(instance, SETUP_CATALOG,
140
                                        exclude_field_names=exclude,
141
                                        include_field_names=include)
142
    return u" ".join(tokens)
143
144
145
@indexer(IHaveAnalysisCategory, ISetupCatalog)
146
def category_uid(instance):
147
    """Returns a list of Category UIDs the instance is assigned to
148
    """
149
    category = instance.getCategory()
150
    return to_keywords_list(category, api.get_uid)
151
152
153
def to_keywords_list(obj, func):
154
    if isinstance(obj, (list, tuple)):
155
        return map(func, obj)
156
    elif obj:
157
        return [func(obj)]
158
    return [None]
159