Passed
Push — 2.x ( 2459cb...f048b4 )
by Ramon
06:30
created

bika.lims.subscribers.objectmodified   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 32
dl 0
loc 65
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B ObjectModifiedEventHandler() 0 38 8
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-2021 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
from bika.lims import api
22
from Products.CMFCore.utils import getToolByName
23
from senaite.core.catalog import ANALYSIS_CATALOG
24
from senaite.core.catalog import SETUP_CATALOG
25
26
27
def ObjectModifiedEventHandler(obj, event):
28
    """ Various types need automation on edit.
29
    """
30
    if not hasattr(obj, 'portal_type'):
31
        return
32
33
    if obj.portal_type == 'Contact':
34
        # Verify that the Contact details are the same as the Plone user.
35
        contact_username = obj.Schema()['Username'].get(obj)
36
        if contact_username:
37
            contact_email = obj.Schema()['EmailAddress'].get(obj)
38
            contact_fullname = obj.Schema()['Fullname'].get(obj)
39
            mt = getToolByName(obj, 'portal_membership')
40
            member = mt.getMemberById(contact_username)
41
            if member:
42
                properties = {'username': contact_username,
43
                              'email': contact_email,
44
                              'fullname': contact_fullname}
45
                member.setMemberProperties(properties)
46
47
    elif obj.portal_type == 'AnalysisCategory':
48
        # If the analysis category's Title is modified, we must
49
        # re-index all services and analyses that refer to this title.
50
        uid = obj.UID()
51
52
        # re-index all analysis services
53
        query = dict(category_uid=uid, portal_type="AnalysisService")
54
        brains = api.search(query, SETUP_CATALOG)
55
        for brain in brains:
56
            obj = api.get_object(brain)
57
            obj.reindexObject()
58
59
        # re-index analyses
60
        query = dict(getCategoryUID=uid)
61
        brains = api.search(query, ANALYSIS_CATALOG)
62
        for brain in brains:
63
            obj = api.get_object(brain)
64
            obj.reindexObject()
65