Passed
Push — 2.x ( e94308...699219 )
by Jordi
09:28
created

senaite.core.registry.subscribers   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 24
dl 0
loc 55
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A init_labels() 0 23 4
A update_label_types() 0 20 3
1
# -*- coding: utf-8 -*-
2
3
from bika.lims import api
4
from senaite.core.api import label as label_api
5
from senaite.core.registry import get_registry_record
6
7
labels_initialized = False
8
9
10
def update_label_types(object, event):
11
    """Update label enabled types
12
13
    NOTE: The settings are thread local!
14
15
    This means that in multi client environments the settings are not reflected
16
    in the other instances.
17
18
    A restart of these instances is required, so that the `init_labels` handler
19
    below marks the classes appropriately.
20
    """
21
    # get the types that support labels directly
22
    enabled_types = object.label_enabled_portal_types
23
    plone_utils = api.get_tool("plone_utils")
24
    friendly_types = plone_utils.getUserFriendlyTypes()
25
    for portal_type in friendly_types:
26
        if portal_type not in enabled_types:
27
            label_api.disable_labels_for_type(portal_type)
28
        else:
29
            label_api.enable_labels_for_type(portal_type)
30
31
32
def init_labels(site, event):
33
    """Initialize labels for enabled portal types after startup
34
35
    This is a multi-subscriber to `zope.component.interfaces.ISite` and
36
    `zope.traversing.interfaces.BeforeTraverseEvent`.
37
38
    This is required because the Zope Component Architecture (Site Manager) is
39
    set for each request and is required here to lookup the registry settings.
40
41
    Also see `zope.site.site.threadSiteSubscriber`.
42
43
    We use a global variable to ensure it is only run once per instance.
44
    """
45
    global labels_initialized
46
    if labels_initialized:
47
        return
48
    enabled_types = get_registry_record("label_enabled_portal_types")
49
    if not enabled_types:
50
        labels_initialized = True
51
        return
52
    for portal_type in enabled_types:
53
        label_api.enable_labels_for_type(portal_type)
54
    labels_initialized = True
55