Passed
Push — master ( d135c8...c0d002 )
by Ramon
05:03
created

is_received()   A

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nop 1
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of SENAITE.CORE
4
#
5
# Copyright 2018 by it's authors.
6
# Some rights reserved. See LICENSE.rst, CONTRIBUTORS.rst.
7
8
from plone.indexer import indexer
9
10
from bika.lims import api
11
from bika.lims.catalog import CATALOG_ANALYSIS_REQUEST_LISTING
12
from bika.lims.interfaces import IAnalysisRequest
13
14
15
@indexer(IAnalysisRequest)
16
def assigned_state(instance):
17
    """Returns `assigned` or `unassigned` depending on the state of the
18
    analyses the analysisrequest contains. Return `unassigned` if the Analysis
19
    Request has at least one analysis in `unassigned` state.
20
    Otherwise, returns `assigned`
21
    """
22
    analyses = instance.getAnalyses()
23
    if not analyses:
24
        return "unassigned"
25
    for analysis in analyses:
26
        analysis_object = api.get_object(analysis)
27
        if not analysis_object.getWorksheet():
28
            return "unassigned"
29
    return "assigned"
30
31
32
@indexer(IAnalysisRequest)
33
def listing_searchable_text(instance):
34
    """ Retrieves all the values of metadata columns in the catalog for
35
    wildcard searches
36
    :return: all metadata values joined in a string
37
    """
38
    entries = set()
39
    catalog = api.get_tool(CATALOG_ANALYSIS_REQUEST_LISTING)
40
    columns = catalog.schema()
41
    brains = catalog({"UID": api.get_uid(instance)})
42
    brain = brains[0] if brains else None
43
    for column in columns:
44
        brain_value = api.safe_getattr(brain, column, None)
45
        instance_value = api.safe_getattr(instance, column, None)
46
        parsed = api.to_searchable_text_metadata(brain_value or instance_value)
47
        entries.add(parsed)
48
49
    # add metadata of all descendants
50
    for descendant in instance.getDescendants():
51
        entries.add(listing_searchable_text(descendant)())
52
53
    # Concatenate all strings to one text blob
54
    return " ".join(entries)
55
56
57
@indexer(IAnalysisRequest)
58
def is_received(instance):
59
    """Returns whether the Analysis Request has been received
60
    """
61
    if instance.getDateReceived():
62
        return True
63
    return False
64