server/hooks.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 29 Sep 2009 12:41:46 +0200
branchstable
changeset 3513 c002f6488631
parent 3071 6ad4e6d4df86
child 3508 d9915224f9a5
permissions -rw-r--r--
[form] replace is_subform by parent_form, carrying more information at the same cost
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     1
"""Core hooks: check schema validity, unsure we are not deleting necessary
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     2
entities...
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     3
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     4
:organization: Logilab
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1802
diff changeset
     5
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     6
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1802
diff changeset
     7
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     8
"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     9
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    10
1016
26387b836099 use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents: 479
diff changeset
    11
from datetime import datetime
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    12
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    13
from cubicweb import UnknownProperty, ValidationError, BadConnectionId
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    14
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    15
from cubicweb.server.pool import Operation, LateOperation, PreCommitOperation
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
    16
from cubicweb.server.hookhelper import (check_internal_entity, 
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
    17
                                        get_user_sessions, rproperty)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
from cubicweb.server.repository import FTIndexEntityOp
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
2605
c4f6a53884ec [R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2603
diff changeset
    20
# special relations that don't have to be checked for integrity, usually
c4f6a53884ec [R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2603
diff changeset
    21
# because they are handled internally by hooks (so we trust ourselves)
c4f6a53884ec [R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2603
diff changeset
    22
DONT_CHECK_RTYPES_ON_ADD = set(('owned_by', 'created_by',
c4f6a53884ec [R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2603
diff changeset
    23
                                'is', 'is_instance_of',
c4f6a53884ec [R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2603
diff changeset
    24
                                'wf_info_for', 'from_state', 'to_state'))
c4f6a53884ec [R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2603
diff changeset
    25
DONT_CHECK_RTYPES_ON_DEL = set(('is', 'is_instance_of',
c4f6a53884ec [R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2603
diff changeset
    26
                                'wf_info_for', 'from_state', 'to_state'))
c4f6a53884ec [R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2603
diff changeset
    27
c4f6a53884ec [R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2603
diff changeset
    28
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
def relation_deleted(session, eidfrom, rtype, eidto):
2101
08003e0354a7 update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    30
    session.transaction_data.setdefault('pendingrelations', []).append(
08003e0354a7 update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    31
        (eidfrom, rtype, eidto))
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
    32
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    33
def eschema_type_eid(session, etype):
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    34
    """get eid of the CWEType entity for the given yams type"""
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    35
    eschema = session.repo.schema.eschema(etype)
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    36
    # eschema.eid is None if schema has been readen from the filesystem, not
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    37
    # from the database (eg during tests)
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    38
    if eschema.eid is None:
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    39
        eschema.eid = session.unsafe_execute(
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
    40
            'Any X WHERE X is CWEType, X name %(name)s',
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
    41
            {'name': str(etype)})[0][0]
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    42
    return eschema.eid
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    44
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    45
# base meta-data handling ######################################################
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
def setctime_before_add_entity(session, entity):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
    """before create a new entity -> set creation and modification date
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
    49
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
    this is a conveniency hook, you shouldn't have to disable it
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
    """
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    52
    timestamp = datetime.now()
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    53
    entity.setdefault('creation_date', timestamp)
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    54
    entity.setdefault('modification_date', timestamp)
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    55
    if not session.get_shared_data('do-not-insert-cwuri'):
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    56
        entity.setdefault('cwuri', u'%seid/%s' % (session.base_url(), entity.eid))
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    57
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
def setmtime_before_update_entity(session, entity):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
    """update an entity -> set modification date"""
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    61
    entity.setdefault('modification_date', datetime.now())
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    62
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
    63
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
class SetCreatorOp(PreCommitOperation):
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
    65
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
    def precommit_event(self):
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    67
        session = self.session
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    68
        if self.entity.eid in session.transaction_data.get('pendingeids', ()):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
            # entity have been created and deleted in the same transaction
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
            return
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    71
        if not self.entity.created_by:
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    72
            session.add_relation(self.entity.eid, 'created_by', session.user.eid)
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    73
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
def setowner_after_add_entity(session, entity):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
    """create a new entity -> set owner and creator metadata"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
    asession = session.actual_session()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
    if not asession.is_internal_session:
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    79
        session.add_relation(entity.eid, 'owned_by', asession.user.eid)
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    80
        SetCreatorOp(asession, entity=entity)
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    81
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
def setis_after_add_entity(session, entity):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
    """create a new entity -> set is relation"""
1250
5c20a7f13c84 new recreate argument to extid2eid when an external source want to recreate entities previously imported with a predictable ext id
sylvain.thenault@logilab.fr
parents: 479
diff changeset
    85
    if hasattr(entity, '_cw_recreating'):
5c20a7f13c84 new recreate argument to extid2eid when an external source want to recreate entities previously imported with a predictable ext id
sylvain.thenault@logilab.fr
parents: 479
diff changeset
    86
        return
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    87
    try:
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    88
        session.add_relation(entity.eid, 'is',
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    89
                             eschema_type_eid(session, entity.id))
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    90
    except IndexError:
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    91
        # during schema serialization, skip
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    92
        return
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
    # XXX < 2.50 bw compat
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
    if not session.get_shared_data('do-not-insert-is_instance_of'):
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    95
        for etype in entity.e_schema.ancestors() + [entity.e_schema]:
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    96
            session.add_relation(entity.eid, 'is_instance_of',
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    97
                                 eschema_type_eid(session, etype))
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
    98
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
def setowner_after_add_user(session, entity):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
    """when a user has been created, add owned_by relation on itself"""
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   102
    session.add_relation(entity.eid, 'owned_by', entity.eid)
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   103
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
def fti_update_after_add_relation(session, eidfrom, rtype, eidto):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
    """sync fulltext index when relevant relation is added. Reindexing the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
    contained entity is enough since it will implicitly reindex the container
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   108
    entity.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   109
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   110
    ftcontainer = session.repo.schema.rschema(rtype).fulltext_container
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   111
    if ftcontainer == 'subject':
2195
58bef4f707ce update calls to a deprecated method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2101
diff changeset
   112
        FTIndexEntityOp(session, entity=session.entity_from_eid(eidto))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   113
    elif ftcontainer == 'object':
2195
58bef4f707ce update calls to a deprecated method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2101
diff changeset
   114
        FTIndexEntityOp(session, entity=session.entity_from_eid(eidfrom))
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   115
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   116
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
def fti_update_after_delete_relation(session, eidfrom, rtype, eidto):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   118
    """sync fulltext index when relevant relation is deleted. Reindexing both
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
    entities is necessary.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
    if session.repo.schema.rschema(rtype).fulltext_container:
2195
58bef4f707ce update calls to a deprecated method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2101
diff changeset
   122
        FTIndexEntityOp(session, entity=session.entity_from_eid(eidto))
58bef4f707ce update calls to a deprecated method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2101
diff changeset
   123
        FTIndexEntityOp(session, entity=session.entity_from_eid(eidfrom))
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   124
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   125
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
class SyncOwnersOp(PreCommitOperation):
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   127
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
    def precommit_event(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
        self.session.unsafe_execute('SET X owned_by U WHERE C owned_by U, C eid %(c)s,'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
                                    'NOT EXISTS(X owned_by U, X eid %(x)s)',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
                                    {'c': self.compositeeid, 'x': self.composedeid},
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
                                    ('c', 'x'))
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   133
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   134
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   135
def sync_owner_after_add_composite_relation(session, eidfrom, rtype, eidto):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   136
    """when adding composite relation, the composed should have the same owners
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   137
    has the composite
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   139
    if rtype == 'wf_info_for':
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   140
        # skip this special composite relation # XXX (syt) why?
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   141
        return
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
    composite = rproperty(session, rtype, eidfrom, eidto, 'composite')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   143
    if composite == 'subject':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   144
        SyncOwnersOp(session, compositeeid=eidfrom, composedeid=eidto)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   145
    elif composite == 'object':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   146
        SyncOwnersOp(session, compositeeid=eidto, composedeid=eidfrom)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   147
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   148
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   149
def _register_metadata_hooks(hm):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   150
    """register meta-data related hooks on the hooks manager"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   151
    hm.register_hook(setctime_before_add_entity, 'before_add_entity', '')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
    hm.register_hook(setmtime_before_update_entity, 'before_update_entity', '')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   153
    hm.register_hook(setowner_after_add_entity, 'after_add_entity', '')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   154
    hm.register_hook(sync_owner_after_add_composite_relation, 'after_add_relation', '')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   155
    hm.register_hook(fti_update_after_add_relation, 'after_add_relation', '')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   156
    hm.register_hook(fti_update_after_delete_relation, 'after_delete_relation', '')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   157
    if 'is' in hm.schema:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   158
        hm.register_hook(setis_after_add_entity, 'after_add_entity', '')
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   159
    if 'CWUser' in hm.schema:
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   160
        hm.register_hook(setowner_after_add_user, 'after_add_entity', 'CWUser')
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   161
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   162
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   163
# core hooks ##################################################################
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   164
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   165
class DelayedDeleteOp(PreCommitOperation):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   166
    """delete the object of composite relation except if the relation
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   167
    has actually been redirected to another composite
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   168
    """
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   169
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   170
    def precommit_event(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   171
        session = self.session
2607
5d889b4928bb [F hooks] skip new eids as well as pending ones in DelayedDelete operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2606
diff changeset
   172
        # don't do anything if the entity is being created or deleted
5d889b4928bb [F hooks] skip new eids as well as pending ones in DelayedDelete operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2606
diff changeset
   173
        if not (self.eid in session.transaction_data.get('pendingeids', ()) or
5d889b4928bb [F hooks] skip new eids as well as pending ones in DelayedDelete operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2606
diff changeset
   174
                self.eid in session.transaction_data.get('neweids', ())):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   175
            etype = session.describe(self.eid)[0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   176
            session.unsafe_execute('DELETE %s X WHERE X eid %%(x)s, NOT %s'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   177
                                   % (etype, self.relation),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   178
                                   {'x': self.eid}, 'x')
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   179
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   180
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   181
def handle_composite_before_del_relation(session, eidfrom, rtype, eidto):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   182
    """delete the object of composite relation"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   183
    composite = rproperty(session, rtype, eidfrom, eidto, 'composite')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   184
    if composite == 'subject':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   185
        DelayedDeleteOp(session, eid=eidto, relation='Y %s X' % rtype)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   186
    elif composite == 'object':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   187
        DelayedDeleteOp(session, eid=eidfrom, relation='X %s Y' % rtype)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   188
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   189
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   190
def before_del_group(session, eid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   191
    """check that we don't remove the owners group"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   192
    check_internal_entity(session, eid, ('owners',))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   193
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   194
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   195
# schema validation hooks #####################################################
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   196
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   197
class CheckConstraintsOperation(LateOperation):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   198
    """check a new relation satisfy its constraints
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   200
    def precommit_event(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
        eidfrom, rtype, eidto = self.rdef
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   202
        # first check related entities have not been deleted in the same
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   203
        # transaction
2101
08003e0354a7 update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   204
        pending = self.session.transaction_data.get('pendingeids', ())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   205
        if eidfrom in pending:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   206
            return
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   207
        if eidto in pending:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   208
            return
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   209
        for constraint in self.constraints:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   210
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   211
                constraint.repo_check(self.session, eidfrom, rtype, eidto)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   212
            except NotImplementedError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   213
                self.critical('can\'t check constraint %s, not supported',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   214
                              constraint)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   215
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   216
    def commit_event(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   217
        pass
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   218
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   219
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   220
def cstrcheck_after_add_relation(session, eidfrom, rtype, eidto):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   221
    """check the relation satisfy its constraints
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   222
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   223
    this is delayed to a precommit time operation since other relation which
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   224
    will make constraint satisfied may be added later.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   225
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   226
    constraints = rproperty(session, rtype, eidfrom, eidto, 'constraints')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   227
    if constraints:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
        CheckConstraintsOperation(session, constraints=constraints,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   229
                                  rdef=(eidfrom, rtype, eidto))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   230
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   231
def uniquecstrcheck_before_modification(session, entity):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   232
    eschema = entity.e_schema
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   233
    for attr, val in entity.items():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   234
        if val is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   235
            continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   236
        if eschema.subject_relation(attr).is_final() and \
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   237
               eschema.has_unique_values(attr):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   238
            rql = '%s X WHERE X %s %%(val)s' % (entity.e_schema, attr)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   239
            rset = session.unsafe_execute(rql, {'val': val})
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   240
            if rset and rset[0][0] != entity.eid:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   241
                msg = session._('the value "%s" is already used, use another one')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   242
                raise ValidationError(entity.eid, {attr: msg % val})
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   243
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   244
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   245
class CheckRequiredRelationOperation(LateOperation):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   246
    """checking relation cardinality has to be done after commit in
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   247
    case the relation is being replaced
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   248
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   249
    eid, rtype = None, None
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   250
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   251
    def precommit_event(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   252
        # recheck pending eids
2101
08003e0354a7 update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   253
        if self.eid in self.session.transaction_data.get('pendingeids', ()):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   254
            return
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   255
        if self.session.unsafe_execute(*self._rql()).rowcount < 1:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   256
            etype = self.session.describe(self.eid)[0]
2241
fcf08ac5f8c0 translate schema types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2195
diff changeset
   257
            _ = self.session._
fcf08ac5f8c0 translate schema types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2195
diff changeset
   258
            msg = _('at least one relation %(rtype)s is required on %(etype)s (%(eid)s)')
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   259
            msg %= {'rtype': _(self.rtype), 'etype': _(etype), 'eid': self.eid}
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   260
            raise ValidationError(self.eid, {self.rtype: msg})
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   261
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   262
    def commit_event(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   263
        pass
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   264
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   265
    def _rql(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   266
        raise NotImplementedError()
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   267
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   268
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   269
class CheckSRelationOp(CheckRequiredRelationOperation):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   270
    """check required subject relation"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   271
    def _rql(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   272
        return 'Any O WHERE S eid %%(x)s, S %s O' % self.rtype, {'x': self.eid}, 'x'
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   273
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   274
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   275
class CheckORelationOp(CheckRequiredRelationOperation):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   276
    """check required object relation"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   277
    def _rql(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   278
        return 'Any S WHERE O eid %%(x)s, S %s O' % self.rtype, {'x': self.eid}, 'x'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   279
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   280
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   281
def checkrel_if_necessary(session, opcls, rtype, eid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   282
    """check an equivalent operation has not already been added"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   283
    for op in session.pending_operations:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   284
        if isinstance(op, opcls) and op.rtype == rtype and op.eid == eid:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   285
            break
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   286
    else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   287
        opcls(session, rtype=rtype, eid=eid)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   288
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   289
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   290
def cardinalitycheck_after_add_entity(session, entity):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   291
    """check cardinalities are satisfied"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   292
    eid = entity.eid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   293
    for rschema, targetschemas, x in entity.e_schema.relation_definitions():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   294
        # skip automatically handled relations
2605
c4f6a53884ec [R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2603
diff changeset
   295
        if rschema.type in DONT_CHECK_RTYPES_ON_ADD:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   296
            continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   297
        if x == 'subject':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   298
            subjtype = entity.e_schema
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   299
            objtype = targetschemas[0].type
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   300
            cardindex = 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   301
            opcls = CheckSRelationOp
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   302
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   303
            subjtype = targetschemas[0].type
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   304
            objtype = entity.e_schema
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   305
            cardindex = 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   306
            opcls = CheckORelationOp
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   307
        card = rschema.rproperty(subjtype, objtype, 'cardinality')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   308
        if card[cardindex] in '1+':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   309
            checkrel_if_necessary(session, opcls, rschema.type, eid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   310
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   311
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   312
def cardinalitycheck_before_del_relation(session, eidfrom, rtype, eidto):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   313
    """check cardinalities are satisfied"""
2605
c4f6a53884ec [R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2603
diff changeset
   314
    if rtype in DONT_CHECK_RTYPES_ON_DEL:
c4f6a53884ec [R hooks] use DONT_CHECK_RTYPES_ON_[ADD|DEL] constant, don't check wf related internal relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2603
diff changeset
   315
        return
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   316
    card = rproperty(session, rtype, eidfrom, eidto, 'cardinality')
2745
0dafa29ace1f [schema migration] test reproducing pb when deleting a relation definition with mandatory card and without removing the associated rtype + fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2695
diff changeset
   317
    pendingrdefs = session.transaction_data.get('pendingrdefs', ())
0dafa29ace1f [schema migration] test reproducing pb when deleting a relation definition with mandatory card and without removing the associated rtype + fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2695
diff changeset
   318
    if (session.describe(eidfrom)[0], rtype, session.describe(eidto)[0]) in pendingrdefs:
0dafa29ace1f [schema migration] test reproducing pb when deleting a relation definition with mandatory card and without removing the associated rtype + fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2695
diff changeset
   319
        return
2101
08003e0354a7 update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   320
    pendingeids = session.transaction_data.get('pendingeids', ())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   321
    if card[0] in '1+' and not eidfrom in pendingeids:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   322
        checkrel_if_necessary(session, CheckSRelationOp, rtype, eidfrom)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   323
    if card[1] in '1+' and not eidto in pendingeids:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   324
        checkrel_if_necessary(session, CheckORelationOp, rtype, eidto)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   325
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   326
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   327
def _register_core_hooks(hm):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   328
    hm.register_hook(handle_composite_before_del_relation, 'before_delete_relation', '')
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   329
    hm.register_hook(before_del_group, 'before_delete_entity', 'CWGroup')
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   330
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   331
    #hm.register_hook(cstrcheck_before_update_entity, 'before_update_entity', '')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   332
    hm.register_hook(cardinalitycheck_after_add_entity, 'after_add_entity', '')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   333
    hm.register_hook(cardinalitycheck_before_del_relation, 'before_delete_relation', '')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   334
    hm.register_hook(cstrcheck_after_add_relation, 'after_add_relation', '')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   335
    hm.register_hook(uniquecstrcheck_before_modification, 'before_add_entity', '')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   336
    hm.register_hook(uniquecstrcheck_before_modification, 'before_update_entity', '')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   337
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   338
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   339
# user/groups synchronisation #################################################
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   340
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   341
class GroupOperation(Operation):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   342
    """base class for group operation"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   343
    geid = None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   344
    def __init__(self, session, *args, **kwargs):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   345
        """override to get the group name before actual groups manipulation:
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   346
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   347
        we may temporarily loose right access during a commit event, so
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   348
        no query should be emitted while comitting
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   349
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   350
        rql = 'Any N WHERE G eid %(x)s, G name N'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   351
        result = session.execute(rql, {'x': kwargs['geid']}, 'x', build_descr=False)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   352
        Operation.__init__(self, session, *args, **kwargs)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   353
        self.group = result[0][0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   354
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   355
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   356
class DeleteGroupOp(GroupOperation):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   357
    """synchronize user when a in_group relation has been deleted"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   358
    def commit_event(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   359
        """the observed connections pool has been commited"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   360
        groups = self.cnxuser.groups
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   361
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   362
            groups.remove(self.group)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   363
        except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   364
            self.error('user %s not in group %s',  self.cnxuser, self.group)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   365
            return
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   366
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   367
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   368
def after_del_in_group(session, fromeid, rtype, toeid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   369
    """modify user permission, need to update users"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   370
    for session_ in get_user_sessions(session.repo, fromeid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   371
        DeleteGroupOp(session, cnxuser=session_.user, geid=toeid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   372
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   373
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   374
class AddGroupOp(GroupOperation):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   375
    """synchronize user when a in_group relation has been added"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   376
    def commit_event(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   377
        """the observed connections pool has been commited"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   378
        groups = self.cnxuser.groups
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   379
        if self.group in groups:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   380
            self.warning('user %s already in group %s', self.cnxuser,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   381
                         self.group)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   382
            return
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   383
        groups.add(self.group)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   384
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   385
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   386
def after_add_in_group(session, fromeid, rtype, toeid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   387
    """modify user permission, need to update users"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   388
    for session_ in get_user_sessions(session.repo, fromeid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   389
        AddGroupOp(session, cnxuser=session_.user, geid=toeid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   390
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   391
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   392
class DelUserOp(Operation):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   393
    """synchronize user when a in_group relation has been added"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   394
    def __init__(self, session, cnxid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   395
        self.cnxid = cnxid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   396
        Operation.__init__(self, session)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   397
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   398
    def commit_event(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   399
        """the observed connections pool has been commited"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   400
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   401
            self.repo.close(self.cnxid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   402
        except BadConnectionId:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   403
            pass # already closed
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   404
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   405
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   406
def after_del_user(session, eid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   407
    """modify user permission, need to update users"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   408
    for session_ in get_user_sessions(session.repo, eid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   409
        DelUserOp(session, session_.id)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   410
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   411
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   412
def _register_usergroup_hooks(hm):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   413
    """register user/group related hooks on the hooks manager"""
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   414
    hm.register_hook(after_del_user, 'after_delete_entity', 'CWUser')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   415
    hm.register_hook(after_add_in_group, 'after_add_relation', 'in_group')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   416
    hm.register_hook(after_del_in_group, 'after_delete_relation', 'in_group')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   417
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   418
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   419
# workflow handling ###########################################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   420
2992
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   421
from cubicweb.entities.wfobjs import WorkflowTransition, WorkflowException
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   422
2949
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   423
def _change_state(session, x, oldstate, newstate):
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   424
    nocheck = session.transaction_data.setdefault('skip-security', set())
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   425
    nocheck.add((x, 'in_state', oldstate))
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   426
    nocheck.add((x, 'in_state', newstate))
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   427
    # delete previous state first in case we're using a super session
3039
7d5a4d27d052 [multi-sources] in ms config, don't try to delete previous state in some condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2992
diff changeset
   428
    fromsource = session.describe(x)[1]
7d5a4d27d052 [multi-sources] in ms config, don't try to delete previous state in some condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2992
diff changeset
   429
    # don't try to remove previous state if in_state isn't stored in the system
7d5a4d27d052 [multi-sources] in ms config, don't try to delete previous state in some condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2992
diff changeset
   430
    # source
7d5a4d27d052 [multi-sources] in ms config, don't try to delete previous state in some condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2992
diff changeset
   431
    if fromsource == 'system' or \
7d5a4d27d052 [multi-sources] in ms config, don't try to delete previous state in some condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2992
diff changeset
   432
       not session.repo.sources_by_uri[fromsource].support_relation('in_state'):
7d5a4d27d052 [multi-sources] in ms config, don't try to delete previous state in some condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2992
diff changeset
   433
        session.delete_relation(x, 'in_state', oldstate)
2949
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   434
    session.add_relation(x, 'in_state', newstate)
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   435
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   436
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   437
def before_add_trinfo(session, entity):
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   438
    """check the transition is allowed, add missing information. Expect that:
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   439
    * wf_info_for inlined relation is set
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   440
    * by_transition or to_state (managers only) inlined relation is set
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   441
    """
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   442
    # first retreive entity to which the state change apply
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   443
    try:
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   444
        foreid = entity['wf_info_for']
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   445
    except KeyError:
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   446
        msg = session._('mandatory relation')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   447
        raise ValidationError(entity.eid, {'wf_info_for': msg})
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   448
    forentity = session.entity_from_eid(foreid)
2992
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   449
    # then check it has a workflow set, unless we're in the process of changing
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   450
    # entity's workflow
2978
d8c5ad14ab8e [wf] distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2949
diff changeset
   451
    if session.transaction_data.get((forentity.eid, 'customwf')):
d8c5ad14ab8e [wf] distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2949
diff changeset
   452
        wfeid = session.transaction_data[(forentity.eid, 'customwf')]
d8c5ad14ab8e [wf] distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2949
diff changeset
   453
        wf = session.entity_from_eid(wfeid)
d8c5ad14ab8e [wf] distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2949
diff changeset
   454
    else:
d8c5ad14ab8e [wf] distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2949
diff changeset
   455
        wf = forentity.current_workflow
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   456
    if wf is None:
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   457
        msg = session._('related entity has no workflow set')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   458
        raise ValidationError(entity.eid, {None: msg})
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   459
    # then check it has a state set
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   460
    fromstate = forentity.current_state
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   461
    if fromstate is None:
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   462
        msg = session._('related entity has no state')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   463
        raise ValidationError(entity.eid, {None: msg})
2992
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   464
    # True if we are coming back from subworkflow
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   465
    swtr = session.transaction_data.pop((forentity.eid, 'subwfentrytr'), None)
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   466
    cowpowers = session.is_super_session or 'managers' in session.user.groups
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   467
    # no investigate the requested state change...
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   468
    try:
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   469
        treid = entity['by_transition']
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   470
    except KeyError:
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   471
        # no transition set, check user is a manager and destination state is
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   472
        # specified (and valid)
2992
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   473
        if not cowpowers:
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   474
            msg = session._('mandatory relation')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   475
            raise ValidationError(entity.eid, {'by_transition': msg})
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   476
        deststateeid = entity.get('to_state')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   477
        if not deststateeid:
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   478
            msg = session._('mandatory relation')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   479
            raise ValidationError(entity.eid, {'by_transition': msg})
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   480
        deststate = wf.state_by_eid(deststateeid)
2992
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   481
        if not cowpowers and deststate is None:
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   482
            msg = entity.req._("state doesn't belong to entity's workflow")
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   483
            raise ValidationError(entity.eid, {'to_state': msg})
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   484
    else:
2992
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   485
        # check transition is valid and allowed, unless we're coming back from
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   486
        # subworkflow
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   487
        tr = session.entity_from_eid(treid)
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   488
        if swtr is None:
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   489
            if tr is None:
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   490
                msg = session._("transition doesn't belong to entity's workflow")
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   491
                raise ValidationError(entity.eid, {'by_transition': msg})
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   492
            if not tr.has_input_state(fromstate):
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   493
                msg = session._("transition isn't allowed")
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   494
                raise ValidationError(entity.eid, {'by_transition': msg})
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   495
            if not tr.may_be_fired(foreid):
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   496
                msg = session._("transition may not be fired")
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   497
                raise ValidationError(entity.eid, {'by_transition': msg})
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   498
        if entity.get('to_state'):
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   499
            deststateeid = entity['to_state']
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   500
            if not cowpowers and deststateeid != tr.destination().eid:
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   501
                msg = session._("transition isn't allowed")
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   502
                raise ValidationError(entity.eid, {'by_transition': msg})
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   503
            if swtr is None:
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   504
                deststate = session.entity_from_eid(deststateeid)
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   505
                if not cowpowers and deststate is None:
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   506
                    msg = entity.req._("state doesn't belong to entity's workflow")
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   507
                    raise ValidationError(entity.eid, {'to_state': msg})
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   508
        else:
2992
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   509
            deststateeid = tr.destination().eid
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   510
    # everything is ok, add missing information on the trinfo entity
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   511
    entity['from_state'] = fromstate.eid
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   512
    entity['to_state'] = deststateeid
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   513
    nocheck = session.transaction_data.setdefault('skip-security', set())
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   514
    nocheck.add((entity.eid, 'from_state', fromstate.eid))
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   515
    nocheck.add((entity.eid, 'to_state', deststateeid))
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   516
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   517
def after_add_trinfo(session, entity):
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   518
    """change related entity state"""
2949
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   519
    _change_state(session, entity['wf_info_for'],
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   520
                  entity['from_state'], entity['to_state'])
2992
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   521
    forentity = session.entity_from_eid(entity['wf_info_for'])
3039
7d5a4d27d052 [multi-sources] in ms config, don't try to delete previous state in some condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2992
diff changeset
   522
    assert forentity.current_state.eid == entity['to_state'], forentity.current_state.name
2992
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   523
    if forentity.main_workflow.eid != forentity.current_workflow.eid:
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   524
        # we're in a subworkflow, check if we've reached an exit point
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   525
        wftr = forentity.subworkflow_input_transition()
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   526
        if wftr is None:
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   527
            # inconsistency detected
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   528
            msg = entity.req._("state doesn't belong to entity's current workflow")
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   529
            raise ValidationError(entity.eid, {'to_state': msg})
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   530
        tostate = wftr.get_exit_point(entity['to_state'])
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   531
        if tostate is not None:
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   532
            # reached an exit point
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   533
            msg = session._('exiting from subworkflow %s')
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   534
            msg %= session._(forentity.current_workflow.name)
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   535
            session.transaction_data[(forentity.eid, 'subwfentrytr')] = True
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   536
            # XXX iirk
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   537
            req = forentity.req
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   538
            forentity.req = session.super_session
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   539
            try:
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   540
                trinfo = forentity.change_state(tostate, msg, u'text/plain',
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   541
                                                tr=wftr)
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   542
            finally:
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   543
                forentity.req = req
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   544
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   545
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   546
class SetInitialStateOp(PreCommitOperation):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   547
    """make initial state be a default state"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   548
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   549
    def precommit_event(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   550
        session = self.session
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   551
        entity = self.entity
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   552
        # if there is an initial state and the entity's state is not set,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   553
        # use the initial state as a default state
2101
08003e0354a7 update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   554
        pendingeids = session.transaction_data.get('pendingeids', ())
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   555
        if not entity.eid in pendingeids and not entity.in_state and \
2978
d8c5ad14ab8e [wf] distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2949
diff changeset
   556
               entity.main_workflow:
d8c5ad14ab8e [wf] distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2949
diff changeset
   557
            state = entity.main_workflow.initial
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   558
            if state:
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   559
                # use super session to by-pass security checks
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   560
                session.super_session.add_relation(entity.eid, 'in_state',
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   561
                                                   state.eid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   562
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   563
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   564
def set_initial_state_after_add(session, entity):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   565
    SetInitialStateOp(session, entity=entity)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   566
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   567
2983
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2978
diff changeset
   568
def before_add_in_state(session, eidfrom, rtype, eidto):
2992
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   569
    """check state apply, in case of direct in_state change using unsafe_execute
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   570
    """
2983
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2978
diff changeset
   571
    nocheck = session.transaction_data.setdefault('skip-security', ())
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2978
diff changeset
   572
    if (eidfrom, 'in_state', eidto) in nocheck:
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2978
diff changeset
   573
        # state changed through TrInfo insertion, so we already know it's ok
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2978
diff changeset
   574
        return
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2978
diff changeset
   575
    entity = session.entity_from_eid(eidfrom)
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2978
diff changeset
   576
    mainwf = entity.main_workflow
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2978
diff changeset
   577
    if mainwf is None:
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2978
diff changeset
   578
        msg = session._('entity has no workflow set')
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2978
diff changeset
   579
        raise ValidationError(entity.eid, {None: msg})
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2978
diff changeset
   580
    for wf in mainwf.iter_workflows():
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2978
diff changeset
   581
        if wf.state_by_eid(eidto):
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2978
diff changeset
   582
            break
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2978
diff changeset
   583
    else:
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2978
diff changeset
   584
        msg = session._("state doesn't belong to entity's workflow. You may "
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2978
diff changeset
   585
                        "want to set a custom workflow for this entity first.")
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2978
diff changeset
   586
        raise ValidationError(eidfrom, {'in_state': msg})
2992
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   587
    if entity.current_workflow and wf.eid != entity.current_workflow.eid:
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   588
        msg = session._("state doesn't belong to entity's current workflow")
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   589
        raise ValidationError(eidfrom, {'in_state': msg})
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   590
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   591
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   592
class CheckTrExitPoint(PreCommitOperation):
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   593
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   594
    def precommit_event(self):
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   595
        tr = self.session.entity_from_eid(self.treid)
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   596
        outputs = set()
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   597
        for ep in tr.subworkflow_exit:
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   598
            if ep.subwf_state.eid in outputs:
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   599
                msg = self.session._("can't have multiple exits on the same state")
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   600
                raise ValidationError(self.treid, {'subworkflow_exit': msg})
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   601
            outputs.add(ep.subwf_state.eid)
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   602
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   603
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   604
def after_add_subworkflow_exit(session, eidfrom, rtype, eidto):
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   605
    CheckTrExitPoint(session, treid=eidfrom)
2983
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2978
diff changeset
   606
2949
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   607
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   608
class WorkflowChangedOp(PreCommitOperation):
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   609
    """fix entity current state when changing its workflow"""
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   610
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   611
    def precommit_event(self):
2985
79185b3ccf2c cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2983
diff changeset
   612
        # notice that enforcement that new workflow apply to the entity's type is
79185b3ccf2c cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2983
diff changeset
   613
        # done by schema rule, no need to check it here
2949
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   614
        session = self.session
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   615
        pendingeids = session.transaction_data.get('pendingeids', ())
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   616
        if self.eid in pendingeids:
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   617
            return
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   618
        entity = session.entity_from_eid(self.eid)
2978
d8c5ad14ab8e [wf] distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2949
diff changeset
   619
        # check custom workflow has not been rechanged to another one in the same
d8c5ad14ab8e [wf] distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2949
diff changeset
   620
        # transaction
d8c5ad14ab8e [wf] distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2949
diff changeset
   621
        mainwf = entity.main_workflow
d8c5ad14ab8e [wf] distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2949
diff changeset
   622
        if mainwf.eid == self.wfeid:
d8c5ad14ab8e [wf] distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2949
diff changeset
   623
            deststate = mainwf.initial
2949
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   624
            if not deststate:
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   625
                msg = session._('workflow has no initial state')
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   626
                raise ValidationError(entity.eid, {'custom_workflow': msg})
2978
d8c5ad14ab8e [wf] distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2949
diff changeset
   627
            if mainwf.state_by_eid(entity.current_state.eid):
2949
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   628
                # nothing to do
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   629
                return
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   630
            # if there are no history, simply go to new workflow's initial state
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   631
            if not entity.workflow_history:
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   632
                if entity.current_state.eid != deststate.eid:
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   633
                    _change_state(session, entity.eid,
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   634
                                  entity.current_state.eid, deststate.eid)
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   635
                return
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   636
            msg = session._('workflow changed to "%s"')
2978
d8c5ad14ab8e [wf] distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2949
diff changeset
   637
            msg %= session._(mainwf.name)
d8c5ad14ab8e [wf] distinguish main workflow / current workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2949
diff changeset
   638
            session.transaction_data[(entity.eid, 'customwf')] = self.wfeid
2992
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   639
            entity.change_state(deststate, msg, u'text/plain')
2949
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   640
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   641
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   642
def set_custom_workflow(session, eidfrom, rtype, eidto):
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   643
    WorkflowChangedOp(session, eid=eidfrom, wfeid=eidto)
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   644
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   645
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   646
def del_custom_workflow(session, eidfrom, rtype, eidto):
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   647
    entity = session.entity_from_eid(eidfrom)
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   648
    typewf = entity.cwetype_workflow()
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   649
    if typewf is not None:
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   650
        WorkflowChangedOp(session, eid=eidfrom, wfeid=typewf.eid)
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   651
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   652
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   653
def after_del_workflow(session, eid):
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   654
    # workflow cleanup
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   655
    session.execute('DELETE State X WHERE NOT X state_of Y')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   656
    session.execute('DELETE Transition X WHERE NOT X transition_of Y')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   657
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   658
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   659
def _register_wf_hooks(hm):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   660
    """register workflow related hooks on the hooks manager"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   661
    if 'in_state' in hm.schema:
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   662
        hm.register_hook(before_add_trinfo, 'before_add_entity', 'TrInfo')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   663
        hm.register_hook(after_add_trinfo, 'after_add_entity', 'TrInfo')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   664
        #hm.register_hook(relation_deleted, 'before_delete_relation', 'in_state')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   665
        for eschema in hm.schema.entities():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   666
            if 'in_state' in eschema.subject_relations():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   667
                hm.register_hook(set_initial_state_after_add, 'after_add_entity',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   668
                                 str(eschema))
2949
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   669
        hm.register_hook(set_custom_workflow, 'after_add_relation', 'custom_workflow')
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   670
        hm.register_hook(del_custom_workflow, 'after_delete_relation', 'custom_workflow')
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2909
diff changeset
   671
        hm.register_hook(after_del_workflow, 'after_delete_entity', 'Workflow')
2983
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2978
diff changeset
   672
        hm.register_hook(before_add_in_state, 'before_add_relation', 'in_state')
2992
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   673
        hm.register_hook(after_add_subworkflow_exit, 'after_add_relation', 'subworkflow_exit')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   674
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   675
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   676
# CWProperty hooks #############################################################
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   677
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   678
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   679
class DelCWPropertyOp(Operation):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   680
    """a user's custom properties has been deleted"""
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   681
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   682
    def commit_event(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   683
        """the observed connections pool has been commited"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   684
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   685
            del self.epropdict[self.key]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   686
        except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   687
            self.error('%s has no associated value', self.key)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   688
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   689
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   690
class ChangeCWPropertyOp(Operation):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   691
    """a user's custom properties has been added/changed"""
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   692
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   693
    def commit_event(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   694
        """the observed connections pool has been commited"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   695
        self.epropdict[self.key] = self.value
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   696
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   697
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   698
class AddCWPropertyOp(Operation):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   699
    """a user's custom properties has been added/changed"""
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   700
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   701
    def commit_event(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   702
        """the observed connections pool has been commited"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   703
        eprop = self.eprop
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   704
        if not eprop.for_user:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   705
            self.repo.vreg.eprop_values[eprop.pkey] = eprop.value
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   706
        # if for_user is set, update is handled by a ChangeCWPropertyOp operation
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   707
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   708
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   709
def after_add_eproperty(session, entity):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   710
    key, value = entity.pkey, entity.value
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   711
    try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   712
        value = session.vreg.typed_value(key, value)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   713
    except UnknownProperty:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   714
        raise ValidationError(entity.eid, {'pkey': session._('unknown property key')})
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   715
    except ValueError, ex:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   716
        raise ValidationError(entity.eid, {'value': session._(str(ex))})
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   717
    if not session.user.matching_groups('managers'):
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   718
        session.add_relation(entity.eid, 'for_user', session.user.eid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   719
    else:
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   720
        AddCWPropertyOp(session, eprop=entity)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   721
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   722
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   723
def after_update_eproperty(session, entity):
2695
56439c45781c [hooks] check key or value is actually edited
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2607
diff changeset
   724
    if not ('pkey' in entity.edited_attributes or
56439c45781c [hooks] check key or value is actually edited
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2607
diff changeset
   725
            'value' in entity.edited_attributes):
56439c45781c [hooks] check key or value is actually edited
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2607
diff changeset
   726
        return
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   727
    key, value = entity.pkey, entity.value
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   728
    try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   729
        value = session.vreg.typed_value(key, value)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   730
    except UnknownProperty:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   731
        return
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   732
    except ValueError, ex:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   733
        raise ValidationError(entity.eid, {'value': session._(str(ex))})
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   734
    if entity.for_user:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   735
        for session_ in get_user_sessions(session.repo, entity.for_user[0].eid):
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   736
            ChangeCWPropertyOp(session, epropdict=session_.user.properties,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   737
                              key=key, value=value)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   738
    else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   739
        # site wide properties
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   740
        ChangeCWPropertyOp(session, epropdict=session.vreg.eprop_values,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   741
                          key=key, value=value)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   742
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   743
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   744
def before_del_eproperty(session, eid):
2101
08003e0354a7 update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   745
    for eidfrom, rtype, eidto in session.transaction_data.get('pendingrelations', ()):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   746
        if rtype == 'for_user' and eidfrom == eid:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   747
            # if for_user was set, delete has already been handled
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   748
            break
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   749
    else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   750
        key = session.execute('Any K WHERE P eid %(x)s, P pkey K',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   751
                              {'x': eid}, 'x')[0][0]
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   752
        DelCWPropertyOp(session, epropdict=session.vreg.eprop_values, key=key)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   753
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   754
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   755
def after_add_for_user(session, fromeid, rtype, toeid):
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   756
    if not session.describe(fromeid)[0] == 'CWProperty':
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   757
        return
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   758
    key, value = session.execute('Any K,V WHERE P eid %(x)s,P pkey K,P value V',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   759
                                 {'x': fromeid}, 'x')[0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   760
    if session.vreg.property_info(key)['sitewide']:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   761
        raise ValidationError(fromeid,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   762
                              {'for_user': session._("site-wide property can't be set for user")})
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   763
    for session_ in get_user_sessions(session.repo, toeid):
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   764
        ChangeCWPropertyOp(session, epropdict=session_.user.properties,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   765
                          key=key, value=value)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   766
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   767
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   768
def before_del_for_user(session, fromeid, rtype, toeid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   769
    key = session.execute('Any K WHERE P eid %(x)s, P pkey K',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   770
                          {'x': fromeid}, 'x')[0][0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   771
    relation_deleted(session, fromeid, rtype, toeid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   772
    for session_ in get_user_sessions(session.repo, toeid):
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   773
        DelCWPropertyOp(session, epropdict=session_.user.properties, key=key)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   774
2603
e47d63351891 [R hooks] optmize insertion by using direct repository methods when possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2463
diff changeset
   775
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   776
def _register_eproperty_hooks(hm):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   777
    """register workflow related hooks on the hooks manager"""
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   778
    hm.register_hook(after_add_eproperty, 'after_add_entity', 'CWProperty')
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   779
    hm.register_hook(after_update_eproperty, 'after_update_entity', 'CWProperty')
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   780
    hm.register_hook(before_del_eproperty, 'before_delete_entity', 'CWProperty')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   781
    hm.register_hook(after_add_for_user, 'after_add_relation', 'for_user')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   782
    hm.register_hook(before_del_for_user, 'before_delete_relation', 'for_user')