Passed
Push — 2.x ( 810c55...946bce )
by Ramon
06:22
created

senaite.core.registry.factories   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 23
dl 0
loc 63
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A WSTemplatesPrintFactory.worksheet_print_templates_order() 0 27 5
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-2024 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
from bika.lims.config import WS_TEMPLATES_ADDON_DIR
22
23
from senaite.core.interfaces import ISenaiteRegistryFactory
24
25
from plone.resource.utils import iterDirectoriesOfType
26
from plone.registry.recordsproxy import RecordsProxy
27
28
from zope.interface import implementer
29
30
31
@implementer(ISenaiteRegistryFactory)
32
class WSTemplatesPrintFactory(RecordsProxy):
33
    """ Proxy for IWorksheetViewRegistry
34
    """
35
36
    @property
37
    def worksheet_print_templates_order(self):
38
        """ Computing getter for this registry field.
39
            Updating the list of templates based on data from the registry
40
            and new templates founded in configured directories
41
            and not saved in the registry.
42
43
        :returns: The ordered list of templates
44
        """
45
46
        all_templates = []
47
        directory_iterator = iterDirectoriesOfType(WS_TEMPLATES_ADDON_DIR)
48
        directories = sorted(directory_iterator, key=lambda d: d.__name__)
49
        for resource in directories:
50
            prefix = resource.__name__
51
            templates = [tpl for tpl in resource.listDirectory() if tpl.endswith(".pt")]
52
            for template in sorted(templates):
53
                all_templates.append("{0}:{1}".format(prefix, template))
54
55
        # Get the ordered list of templates from the parent class
56
        order = self.__getattr__("worksheet_print_templates_order") or []
57
58
        def sort_templates(item):
59
            return order.index(item) if item in order else len(order)
60
61
        templates = sorted(all_templates, key=sort_templates)
62
        return list(filter(None, templates))
63