Passed
Push — master ( 5e306e...e48f16 )
by Jordi
04:40
created

bika.lims.subscribers.objectmodified   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 38
dl 0
loc 55
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
C ObjectModifiedEventHandler() 0 44 11
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of SENAITE.CORE
4
#
5
# Copyright 2018 by it's authors.
6
# Some rights reserved. See LICENSE.rst, CONTRIBUTORS.rst.
7
8
from Products.CMFCore.utils import getToolByName
9
10
11
def ObjectModifiedEventHandler(obj, event):
12
    """ Various types need automation on edit.
13
    """
14
    if not hasattr(obj, 'portal_type'):
15
        return
16
17
    if obj.portal_type == 'Calculation':
18
        pr = getToolByName(obj, 'portal_repository')
19
        uc = getToolByName(obj, 'uid_catalog')
20
        obj = uc(UID=obj.UID())[0].getObject()
21
        version_id = obj.version_id if hasattr(obj, 'version_id') else 0
22
23
        backrefs = obj.getBackReferences('MethodCalculation')
24
        for i, target in enumerate(backrefs):
25
            target = uc(UID=target.UID())[0].getObject()
26
            pr.save(obj=target, comment="Calculation updated to version %s" %
27
                (version_id + 1,))
28
            reference_versions = getattr(target, 'reference_versions', {})
29
            reference_versions[obj.UID()] = version_id + 1
30
            target.reference_versions = reference_versions
31
32
    elif obj.portal_type == 'Contact':
33
        # Verify that the Contact details are the same as the Plone user.
34
        contact_username = obj.Schema()['Username'].get(obj)
35
        if contact_username:
36
            contact_email = obj.Schema()['EmailAddress'].get(obj)
37
            contact_fullname = obj.Schema()['Fullname'].get(obj)
38
            mt = getToolByName(obj, 'portal_membership')
39
            member = mt.getMemberById(contact_username)
40
            if member:
41
                properties = {'username': contact_username,
42
                              'email': contact_email,
43
                              'fullname': contact_fullname}
44
                member.setMemberProperties(properties)
45
46
    elif obj.portal_type == 'AnalysisCategory':
47
        # If the analysis category's Title is modified, we must
48
        # re-index all services and analyses that refer to this title.
49
        for i in [['Analysis', 'bika_analysis_catalog'],
50
                  ['AnalysisService', 'bika_setup_catalog']]:
51
            cat = getToolByName(obj, i[1])
52
            brains = cat(portal_type=i[0], getCategoryUID=obj.UID())
53
            for brain in brains:
54
                brain.getObject().reindexObject(idxs=['getCategoryTitle'])
55