Passed
Push — 2.x ( bb5fa7...513331 )
by Ramon
05:52
created

ObjectInitializedEventHandler()   A

Complexity

Conditions 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 2
nop 2
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 Products.CMFPlone.utils import safe_unicode
22
23
24
def move_to_client(batch):
25
    """Moves the batch to the client assigned in the Client field if it does
26
    not belong to that client yet. Does nothing otherwise
27
    """
28
    # If client is assigned, move the Batch the client's folder
29
    # Note here we directly get the Client from the Schema, cause getClient
30
    # getter is overriden in Batch content type to always look to aq_parent in
31
    # order to prevent inconsistencies (the Client schema field is only used to
32
    # allow the user to assign a Client to the Batch).
33
    client = batch.getField("Client").get(batch)
34
35
    # Check if the Batch is being created inside the Client
36
    if client and (client.UID() != batch.aq_parent.UID()):
37
        # move batch inside the client
38
        if batch.id in batch.aq_parent.objectIds():
39
            cp = batch.aq_parent.manage_cutObjects(batch.id)
40
            client.manage_pasteObjects(cp)
41
42
43
def ObjectInitializedEventHandler(batch, event):
44
    """Actions to be done when a batch is created:
45
    - Title as the Batch ID if title is not defined
46
    - Move the Batch inside the Client if defined
47
    """
48
    if not batch.title:
49
        batch.setTitle(safe_unicode(batch.id).encode('utf-8'))
50
51
    # Try to move the batch to it's client folder
52
    move_to_client(batch)
53
54
55
def ObjectModifiedEventHandler(batch, event):
56
    """Moves the batch object inside the folder that represents the client this
57
    batch has been assigned to via "Client" field
58
    """
59
    # Try to move the batch to it's client folder
60
    move_to_client(batch)
61