Passed
Push — master ( be702a...def89a )
by Jordi
04:03
created

mailto_link_from_ccemails()   A

Complexity

Conditions 2

Size

Total Lines 13
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 13
rs 9.8
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
# SENAITE.CORE is free software: you can redistribute it and/or modify it under
6
# the terms of the GNU General Public License as published by the Free Software
7
# Foundation, version 2.
8
#
9
# This program is distributed in the hope that it will be useful, but WITHOUT
10
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12
# details.
13
#
14
# You should have received a copy of the GNU General Public License along with
15
# this program; if not, write to the Free Software Foundation, Inc., 51
16
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
#
18
# Copyright 2018-2019 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
from bika.lims.interfaces import IAnalysisRequest
22
from bika.lims.interfaces import IJSONReadExtender
23
from bika.lims.jsonapi import get_include_fields
24
from bika.lims.jsonapi import load_brain_metadata
25
from bika.lims.jsonapi import load_field_values
26
from bika.lims.vocabularies import CatalogVocabulary
27
from bika.lims.workflow import get_workflow_actions
28
from invoice import InvoiceCreate
29
from zope.component import adapts
30
from zope.interface import implements
31
32
# This AnalysisRequestViewView import must be here, above all the ones that are
33
# now below it.  Don't reorganise imports carelessly without taking care to read
34
# this comment twice.
35
from .view import AnalysisRequestViewView
36
37
from .add2 import AnalysisRequestAddView  # noqa: F401
38
from .add2 import AnalysisRequestManageView  # noqa: F401
39
from .add2 import ajaxAnalysisRequestAddView  # noqa: F401
40
from .analysisrequests import AnalysisRequestsView
41
from .invoice import InvoicePrintView
42
from .invoice import InvoiceView
43
from .manage_analyses import AnalysisRequestAnalysesView
44
from .manage_results import AnalysisRequestManageResultsView
45
from .published_results import AnalysisRequestPublishedResults
46
47
48
class ClientContactVocabularyFactory(CatalogVocabulary):
49
50
    def __call__(self):
51
        parent = self.context.aq_parent
52
        return super(ClientContactVocabularyFactory, self).__call__(
53
            portal_type='Contact',
54
            path={'query': "/".join(parent.getPhysicalPath()),
55
                  'level': 0}
56
        )
57
58
59
class JSONReadExtender(object):
60
61
    """- Adds the full details of all analyses to the AR.Analyses field
62
    """
63
64
    implements(IJSONReadExtender)
65
    adapts(IAnalysisRequest)
66
67
    def __init__(self, context):
68
        self.context = context
69
70
    def ar_analysis_values(self):
71
        ret = []
72
        analyses = self.context.getAnalyses(is_active=True)
73
        for proxy in analyses:
74
            analysis = proxy.getObject()
75
            if proxy.review_state == 'retracted':
76
                # these are scraped up when Retested analyses are found below.
77
                continue
78
            # things that are manually inserted into the analysis.
79
            # These things will be included even if they are not present in
80
            # include_fields in the request.
81
            method = analysis.getMethod()
82
            analysis_data = {
83
                "Uncertainty": analysis.getUncertainty(),
84
                "Method": method.Title() if method else '',
85
                "Unit": analysis.getUnit(),
86
            }
87
            # Place all schema fields ino the result.
88
            analysis_data.update(load_brain_metadata(proxy, []))
89
            # Place all schema fields ino the result.
90
            analysis_data.update(load_field_values(analysis, []))
91
            # call any adapters that care to modify the Analysis data.
92
            # adapters = getAdapters((analysis, ), IJSONReadExtender)
93
            # for name, adapter in adapters:
94
            #     adapter(request, analysis_data)
95
            if not self.include_fields or "transitions" in self.include_fields:
96
                analysis_data['transitions'] = get_workflow_actions(analysis)
97
            retest_of = analysis.getRetestOf()
98
            if retest_of:
99
                prevs = [{'created': str(retest_of.created()),
100
                          'Result': retest_of.getResult(),
101
                          'InterimFields': retest_of.getInterimFields()}]
102
                analysis_data['Previous Results'] = prevs
103
            ret.append(analysis_data)
104
        return ret
105
106
    def __call__(self, request, data):
107
        self.request = request
108
        self.include_fields = get_include_fields(request)
109
        if not self.include_fields or "Analyses" in self.include_fields:
110
            data['Analyses'] = self.ar_analysis_values()
111
112
113
class mailto_link_from_contacts(object):
114
    """Custom header table field adapter
115
116
    see: bika.lims.browser.header_table
117
    """
118
119
    def __init__(self, context):
120
        self.context = context
121
122
    def __call__(self, field):
123
        contacts = field.get(self.context)
124
        if not type(contacts) in (list, tuple):
125
            contacts = [contacts, ]
126
        ret = []
127
        for contact in contacts:
128
            if contact:
129
                mailto = "<a href='mailto:%s'>%s</a>" % (
130
                    contact.getEmailAddress(), contact.getFullname())
131
                ret.append(mailto)
132
        return ",".join(ret)
133
134
135
class mailto_link_from_ccemails(object):
136
    """Custom header table field adapter
137
138
    see: bika.lims.browser.header_table
139
    """
140
141
    def __init__(self, context):
142
        self.context = context
143
144
    def __call__(self, field):
145
        ccemails = field.get(self.context)
146
        addresses = ccemails.split(",")
147
        ret = []
148
        for address in addresses:
149
            address = address.strip()
150
            mailto = "<a href='mailto:%s'>%s</a>" % (
151
                address, address)
152
            ret.append(mailto)
153
        return ",".join(ret)
154