Passed
Push — master ( bcb3c3...740c75 )
by Pau
04:01
created

bika.health.monkeys.macrowidget.__call__()   B

Complexity

Conditions 7

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 25
rs 8
c 0
b 0
f 0
cc 7
nop 4
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of SENAITE.HEALTH.
4
#
5
# SENAITE.HEALTH is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by the Free
7
# Software 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
# TODO Remove for compatibility with senaite.core v1.3.3
22
# Superseded by https://github.com/senaite/senaite.core/pull/1466
23
24
from AccessControl.unauthorized import Unauthorized
25
26
27
def __call__(self, mode, instance, context=None):
28
    self.bootstrap(instance)
29
    # If an attribute called macro_<mode> exists resolve that
30
    # before the generic macro, this lets other projects
31
    # create more partial widgets
32
    macro = getattr(self, 'macro_%s' % mode, self.macro)
33
    if macro == "bika_widgets/referencewidget":
34
        macro = "bika_health_widgets/referencewidget"
35
36
    # Now split the macro into optional parts using '|'
37
    # if the first part doesn't exist, the search continues
38
    paths = macro.split('|')
39
    if len(paths) == 1 and macro == self.macro:
40
        # Prepend the default (optional) customization element
41
        paths.insert(0, 'at_widget_%s' % self.macro.split('/')[-1])
42
    for path in paths:
43
        try:
44
            template = instance.restrictedTraverse(path=path)
45
            if template:
46
                return template.macros[mode]
47
        except (Unauthorized, AttributeError, KeyError):
48
            # This means we didn't have access or it doesn't exist
49
            pass
50
    raise AttributeError("Macro %s does not exist for %s" % (macro,
51
                                                             instance))