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

ObjectModifiedEventHandler()   C

Complexity

Conditions 11

Size

Total Lines 44
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 44
rs 5.4
c 0
b 0
f 0
cc 11
nop 2

How to fix   Complexity   

Complexity

Complex classes like bika.lims.subscribers.objectmodified.ObjectModifiedEventHandler() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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