Passed
Push — 2.x ( a93928...ced756 )
by Jordi
08:02 queued 01:02
created

GetSampleStickers.__call__()   B

Complexity

Conditions 5

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 29
rs 8.9093
c 0
b 0
f 0
cc 5
nop 2
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-2025 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
from bika.lims import api
22
from senaite.core import logger
23
from senaite.core.interfaces import IGetStickerTemplates
24
from senaite.core.vocabularies.stickers import get_sticker_templates
25
from zope.interface import implementer
26
27
28
@implementer(IGetStickerTemplates)
29
class GetSampleStickers(object):
30
    """Returns a list with of sticker templates for the sample
31
32
    Each item in the list is a dictionary with the following structure:
33
34
        {
35
            "id": <template_id>,
36
            "title": <teamplate_title>,
37
            "selected: True/False",
38
        }
39
    """
40
41
    def __init__(self, context):
42
        self.context = context
43
        self.sample_type = self.context.getSampleType()
44
45
    def __call__(self, request):
46
        # Stickers admittance are saved in sample type
47
        if not hasattr(self.context, "getSampleType"):
48
            logger.warning(
49
                "{} has no attribute 'getSampleType', so no sticker will be "
50
                "returned.". format(self.context.getId())
51
            )
52
            return []
53
54
        # get a copy of the admitted stickers set
55
        sticker_ids = set(self.sample_type.getAdmittedStickers())
56
        if not sticker_ids:
57
            return []
58
59
        default_template = self.default_template
60
        setup_default_sticker = self.get_setup_default_sticker()
61
        # ensure the setup default sticker is always contained
62
        sticker_ids.add(setup_default_sticker)
63
64
        result = []
65
        # Getting only existing templates and its info
66
        stickers = get_sticker_templates()
67
        for sticker in stickers:
68
            if sticker.get("id") in sticker_ids:
69
                sticker_info = sticker.copy()
70
                sticker_id = sticker.get("id")
71
                sticker_info["selected"] = sticker_id == default_template
72
                result.append(sticker_info)
73
        return result
74
75
    @property
76
    def default_template(self):
77
        """
78
        Gets the default sticker for that content type depending on the
79
        requested size.
80
81
        :return: An sticker ID as string
82
        """
83
        request = api.get_request()
84
        size = request.get("size", "")
85
        if size == "small":
86
            return self.sample_type.getDefaultSmallSticker()
87
        elif size == "large":
88
            return self.sample_type.getDefaultLargeSticker()
89
        # fall back to the default sticker from setup
90
        return self.get_setup_default_sticker()
91
92
    def get_setup_default_sticker(self):
93
        """Returns the default sticker from setup
94
        """
95
        setup = api.get_setup()
96
        return setup.getAutoStickerTemplate()
97