Passed
Push — 2.x ( ee4c2a...00630e )
by Ramon
06:59
created

ObjectEditedEventHandler()   A

Complexity

Conditions 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 23
rs 9.8
c 0
b 0
f 0
cc 4
nop 2
1
# -*- coding: utf-8 -*-
2
3
from bika.lims import api
4
5
6
def ObjectEditedEventHandler(obj, event):
7
    """Event called after modifying a ReferenceSample, that is not in the
8
    creation stage any more. It updates the ID of the reference sample with the
9
    value of field ManualID if valid and different from current id
10
    """
11
    # compare with current id
12
    obj_id = obj.getId()
13
    manual_id = obj.getManualId()
14
    if obj_id == manual_id:
15
        return
16
17
    # check if this new id is valid
18
    parent = api.get_parent(obj)
19
    if not api.is_valid_id(manual_id, container=parent):
20
        return
21
22
    # do not modify the id if it has objects inside
23
    if obj.objectIds():
24
        return
25
26
    # re-assign id and reindex
27
    parent.manage_renameObject(obj_id, manual_id)
28
    obj.reindexObject()
29