Passed
Pull Request — 2.x (#1872)
by Jordi
04:50
created

senaite.core.catalog.indexer.sample   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 32
dl 0
loc 56
rs 10
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A is_received() 0 7 2
A listing_searchable_text() 0 19 2
A assigned_state() 0 15 4
1
# -*- coding: utf-8 -*-
2
3
from bika.lims import api
4
from bika.lims.interfaces import IAnalysisRequest
5
from plone.indexer import indexer
6
from senaite.core.catalog import SAMPLE_CATALOG
7
from senaite.core.catalog.utils import get_searchable_text_tokens
8
from senaite.core.interfaces import ISampleCatalog
9
10
11
@indexer(IAnalysisRequest)
12
def assigned_state(instance):
13
    """Returns `assigned` or `unassigned` depending on the state of the
14
    analyses the analysisrequest contains. Return `unassigned` if the Analysis
15
    Request has at least one analysis in `unassigned` state.
16
    Otherwise, returns `assigned`
17
    """
18
    analyses = instance.getAnalyses()
19
    if not analyses:
20
        return "unassigned"
21
    for analysis in analyses:
22
        analysis_object = api.get_object(analysis)
23
        if not analysis_object.getWorksheet():
24
            return "unassigned"
25
    return "assigned"
26
27
28
@indexer(IAnalysisRequest)
29
def is_received(instance):
30
    """Returns whether the Analysis Request has been received
31
    """
32
    if instance.getDateReceived():
33
        return True
34
    return False
35
36
37
@indexer(IAnalysisRequest, ISampleCatalog)
38
def listing_searchable_text(instance):
39
    """Retrieves all the values of metadata columns in the catalog for
40
    wildcard searches
41
    :return: all metadata values joined in a string
42
    """
43
    entries = set()
44
    catalog = SAMPLE_CATALOG
45
46
    # add searchable text tokens for the root sample
47
    tokens = get_searchable_text_tokens(instance, catalog)
48
    entries.update(tokens)
49
50
    # add searchable text tokens for descendant samples
51
    for descendant in instance.getDescendants():
52
        tokens = get_searchable_text_tokens(descendant, catalog)
53
        entries.update(tokens)
54
55
    return u" ".join(list(entries))
56