server/hooks.py
branch3.5
changeset 2983 b458631fe347
parent 2978 d8c5ad14ab8e
child 2985 79185b3ccf2c
equal deleted inserted replaced
2982:1768dbe8e9b3 2983:b458631fe347
   513                 # use super session to by-pass security checks
   513                 # use super session to by-pass security checks
   514                 session.super_session.add_relation(entity.eid, 'in_state',
   514                 session.super_session.add_relation(entity.eid, 'in_state',
   515                                                    state.eid)
   515                                                    state.eid)
   516 
   516 
   517 
   517 
       
   518 def before_add_in_state(session, eidfrom, rtype, eidto):
       
   519     """check state apply"""
       
   520     nocheck = session.transaction_data.setdefault('skip-security', ())
       
   521     if (eidfrom, 'in_state', eidto) in nocheck:
       
   522         # state changed through TrInfo insertion, so we already know it's ok
       
   523         print 'skip in_state check'
       
   524         return
       
   525     entity = session.entity_from_eid(eidfrom)
       
   526     mainwf = entity.main_workflow
       
   527     if mainwf is None:
       
   528         msg = session._('entity has no workflow set')
       
   529         raise ValidationError(entity.eid, {None: msg})
       
   530     for wf in mainwf.iter_workflows():
       
   531         if wf.state_by_eid(eidto):
       
   532             break
       
   533     else:
       
   534         msg = session._("state doesn't belong to entity's workflow. You may "
       
   535                         "want to set a custom workflow for this entity first.")
       
   536         raise ValidationError(eidfrom, {'in_state': msg})
       
   537 
   518 def set_initial_state_after_add(session, entity):
   538 def set_initial_state_after_add(session, entity):
   519     SetInitialStateOp(session, entity=entity)
   539     SetInitialStateOp(session, entity=entity)
   520 
   540 
   521 
   541 
   522 class WorkflowChangedOp(PreCommitOperation):
   542 class WorkflowChangedOp(PreCommitOperation):
   579                 hm.register_hook(set_initial_state_after_add, 'after_add_entity',
   599                 hm.register_hook(set_initial_state_after_add, 'after_add_entity',
   580                                  str(eschema))
   600                                  str(eschema))
   581         hm.register_hook(set_custom_workflow, 'after_add_relation', 'custom_workflow')
   601         hm.register_hook(set_custom_workflow, 'after_add_relation', 'custom_workflow')
   582         hm.register_hook(del_custom_workflow, 'after_delete_relation', 'custom_workflow')
   602         hm.register_hook(del_custom_workflow, 'after_delete_relation', 'custom_workflow')
   583         hm.register_hook(after_del_workflow, 'after_delete_entity', 'Workflow')
   603         hm.register_hook(after_del_workflow, 'after_delete_entity', 'Workflow')
       
   604         hm.register_hook(before_add_in_state, 'before_add_relation', 'in_state')
   584 
   605 
   585 
   606 
   586 # CWProperty hooks #############################################################
   607 # CWProperty hooks #############################################################
   587 
   608 
   588 
   609