server/migractions.py
branch3.5
changeset 2920 64322aa83a1d
parent 2903 043c8fcb3819
child 2931 17224e90a1c4
equal deleted inserted replaced
2919:662f35236d1c 2920:64322aa83a1d
   904     def cmd_synchronize_permissions(self, ertype, commit=True):
   904     def cmd_synchronize_permissions(self, ertype, commit=True):
   905         self.cmd_sync_schema_props_perms(ertype, syncprops=False, commit=commit)
   905         self.cmd_sync_schema_props_perms(ertype, syncprops=False, commit=commit)
   906 
   906 
   907     # Workflows handling ######################################################
   907     # Workflows handling ######################################################
   908 
   908 
       
   909     def cmd_add_workflow(self, name, wfof, default=True, commit=False,
       
   910                          **kwargs):
       
   911         self.session.set_pool() # ensure pool is set
       
   912         wf = self.cmd_create_entity('Workflow', name=unicode(name),
       
   913                                     **kwargs)
       
   914         if not isinstance(wfof, (list, tuple)):
       
   915             wfof = (wfof,)
       
   916         for etype in wfof:
       
   917             rset = self.rqlexec('SET X workflow_of ET '
       
   918                                 'WHERE X eid %(x)s, ET name %(et)s',
       
   919                                 {'x': wf.eid, 'et': etype}, 'x')
       
   920             assert rset, 'unexistant entity type %s' % etype
       
   921             if default:
       
   922                 rset = self.rqlexec('SET X default_workflow_of ET '
       
   923                                     'WHERE X eid %(x)s, ET name %(et)s',
       
   924                                     {'x': wf.eid, 'et': etype}, 'x')
       
   925         if commit:
       
   926             self.commit()
       
   927         return wf
       
   928 
       
   929     # XXX remove once cmd_add_[state|transition] are removed
       
   930     def _get_or_create_wf(self, etypes):
       
   931         self.session.set_pool() # ensure pool is set
       
   932         if not isinstance(etypes, (list, tuple)):
       
   933             etypes = (etypes,)
       
   934         rset = self.rqlexec('Workflow X WHERE X workflow_of ET, ET name %(et)s',
       
   935                             {'et': etypes[0]})
       
   936         if rset:
       
   937             return rset.get_entity(0, 0)
       
   938         return self.cmd_add_workflow('%s workflow' % ';'.join(etypes), etypes)
       
   939 
       
   940     @deprecated('use add_workflow and Workflow.add_state method')
   909     def cmd_add_state(self, name, stateof, initial=False, commit=False, **kwargs):
   941     def cmd_add_state(self, name, stateof, initial=False, commit=False, **kwargs):
   910         """method to ease workflow definition: add a state for one or more
   942         """method to ease workflow definition: add a state for one or more
   911         entity type(s)
   943         entity type(s)
   912         """
   944         """
   913         stateeid = self.cmd_add_entity('State', name=name, **kwargs)
   945         wf = self._get_or_create_wf(stateof)
   914         if not isinstance(stateof, (list, tuple)):
   946         state = wf.add_state(name, initial, **kwargs)
   915             stateof = (stateof,)
   947         if commit:
   916         for etype in stateof:
   948             self.commit()
   917             # XXX ensure etype validity
   949         return state.eid
   918             self.rqlexec('SET X state_of Y WHERE X eid %(x)s, Y name %(et)s',
   950 
   919                          {'x': stateeid, 'et': etype}, 'x', ask_confirm=False)
   951     @deprecated('use add_workflow and Workflow.add_transition method')
   920             if initial:
       
   921                 self.rqlexec('SET ET initial_state S WHERE ET name %(et)s, S eid %(x)s',
       
   922                              {'x': stateeid, 'et': etype}, 'x', ask_confirm=False)
       
   923         if commit:
       
   924             self.commit()
       
   925         return stateeid
       
   926 
       
   927     def cmd_add_transition(self, name, transitionof, fromstates, tostate,
   952     def cmd_add_transition(self, name, transitionof, fromstates, tostate,
   928                            requiredgroups=(), conditions=(), commit=False, **kwargs):
   953                            requiredgroups=(), conditions=(), commit=False, **kwargs):
   929         """method to ease workflow definition: add a transition for one or more
   954         """method to ease workflow definition: add a transition for one or more
   930         entity type(s), from one or more state and to a single state
   955         entity type(s), from one or more state and to a single state
   931         """
   956         """
   932         treid = self.cmd_add_entity('Transition', name=name, **kwargs)
   957         wf = self._get_or_create_wf(transitionof)
   933         if not isinstance(transitionof, (list, tuple)):
   958         tr = wf.add_transition(name, fromstates, tostate, requiredgroups,
   934             transitionof = (transitionof,)
   959                                conditions, **kwargs)
   935         for etype in transitionof:
   960         if commit:
   936             # XXX ensure etype validity
   961             self.commit()
   937             self.rqlexec('SET X transition_of Y WHERE X eid %(x)s, Y name %(et)s',
   962         return tr.eid
   938                          {'x': treid, 'et': etype}, 'x', ask_confirm=False)
   963 
   939         for stateeid in fromstates:
   964     @deprecated('use Transition.set_transition_permissions method')
   940             self.rqlexec('SET X allowed_transition Y WHERE X eid %(x)s, Y eid %(y)s',
       
   941                          {'x': stateeid, 'y': treid}, 'x', ask_confirm=False)
       
   942         self.rqlexec('SET X destination_state Y WHERE X eid %(x)s, Y eid %(y)s',
       
   943                      {'x': treid, 'y': tostate}, 'x', ask_confirm=False)
       
   944         self.cmd_set_transition_permissions(treid, requiredgroups, conditions,
       
   945                                             reset=False)
       
   946         if commit:
       
   947             self.commit()
       
   948         return treid
       
   949 
       
   950     def cmd_set_transition_permissions(self, treid,
   965     def cmd_set_transition_permissions(self, treid,
   951                                        requiredgroups=(), conditions=(),
   966                                        requiredgroups=(), conditions=(),
   952                                        reset=True, commit=False):
   967                                        reset=True, commit=False):
   953         """set or add (if `reset` is False) groups and conditions for a
   968         """set or add (if `reset` is False) groups and conditions for a
   954         transition
   969         transition
   955         """
   970         """
   956         if reset:
   971         self.session.set_pool() # ensure pool is set
   957             self.rqlexec('DELETE T require_group G WHERE T eid %(x)s',
   972         tr = self.session.entity_from_eid(treid)
   958                          {'x': treid}, 'x', ask_confirm=False)
   973         tr.set_transition_permissions(requiredgroups, conditions, reset)
   959             self.rqlexec('DELETE T condition R WHERE T eid %(x)s',
   974         if commit:
   960                          {'x': treid}, 'x', ask_confirm=False)
   975             self.commit()
   961         for gname in requiredgroups:
   976 
   962             ### XXX ensure gname validity
   977     @deprecated('use entity.change_state("state")')
   963             self.rqlexec('SET T require_group G WHERE T eid %(x)s, G name %(gn)s',
       
   964                          {'x': treid, 'gn': gname}, 'x', ask_confirm=False)
       
   965         if isinstance(conditions, basestring):
       
   966             conditions = (conditions,)
       
   967         for expr in conditions:
       
   968             if isinstance(expr, str):
       
   969                 expr = unicode(expr)
       
   970             self.rqlexec('INSERT RQLExpression X: X exprtype "ERQLExpression", '
       
   971                          'X expression %(expr)s, T condition X '
       
   972                          'WHERE T eid %(x)s',
       
   973                          {'x': treid, 'expr': expr}, 'x', ask_confirm=False)
       
   974         if commit:
       
   975             self.commit()
       
   976 
       
   977     def cmd_set_state(self, eid, statename, commit=False):
   978     def cmd_set_state(self, eid, statename, commit=False):
   978         self.session.set_pool() # ensure pool is set
   979         self.session.set_pool() # ensure pool is set
   979         entity = self.session.entity_from_eid(eid)
   980         self.session.entity_from_eid(eid).change_state(statename)
   980         entity.change_state(entity.wf_state(statename).eid)
       
   981         if commit:
   981         if commit:
   982             self.commit()
   982             self.commit()
   983 
   983 
   984     # CWProperty handling ######################################################
   984     # CWProperty handling ######################################################
   985 
   985 
   992         value = unicode(value)
   992         value = unicode(value)
   993         try:
   993         try:
   994             prop = self.rqlexec('CWProperty X WHERE X pkey %(k)s', {'k': pkey},
   994             prop = self.rqlexec('CWProperty X WHERE X pkey %(k)s', {'k': pkey},
   995                                 ask_confirm=False).get_entity(0, 0)
   995                                 ask_confirm=False).get_entity(0, 0)
   996         except:
   996         except:
   997             self.cmd_add_entity('CWProperty', pkey=unicode(pkey), value=value)
   997             self.cmd_create_entity('CWProperty', pkey=unicode(pkey), value=value)
   998         else:
   998         else:
   999             self.rqlexec('SET X value %(v)s WHERE X pkey %(k)s',
   999             self.rqlexec('SET X value %(v)s WHERE X pkey %(k)s',
  1000                          {'k': pkey, 'v': value}, ask_confirm=False)
  1000                          {'k': pkey, 'v': value}, ask_confirm=False)
  1001 
  1001 
  1002     # other data migration commands ###########################################
  1002     # other data migration commands ###########################################
  1003 
  1003 
       
  1004     def cmd_create_entity(self, etype, *args, **kwargs):
       
  1005         """add a new entity of the given type"""
       
  1006         commit = kwargs.pop('commit', False)
       
  1007         self.session.set_pool()
       
  1008         entity = self.session.create_entity(etype, *args, **kwargs)
       
  1009         if commit:
       
  1010             self.commit()
       
  1011         return entity
       
  1012 
       
  1013     @deprecated('use create_entity')
  1004     def cmd_add_entity(self, etype, *args, **kwargs):
  1014     def cmd_add_entity(self, etype, *args, **kwargs):
  1005         """add a new entity of the given type"""
  1015         """add a new entity of the given type"""
  1006         rql = 'INSERT %s X' % etype
  1016         return self.cmd_create_entity(etype, *args, **kwargs).eid
  1007         relations = []
       
  1008         restrictions = []
       
  1009         for rtype, rvar in args:
       
  1010             relations.append('X %s %s' % (rtype, rvar))
       
  1011             restrictions.append('%s eid %s' % (rvar, kwargs.pop(rvar)))
       
  1012         commit = kwargs.pop('commit', False)
       
  1013         for attr in kwargs:
       
  1014             relations.append('X %s %%(%s)s' % (attr, attr))
       
  1015         if relations:
       
  1016             rql = '%s: %s' % (rql, ', '.join(relations))
       
  1017         if restrictions:
       
  1018             rql = '%s WHERE %s' % (rql, ', '.join(restrictions))
       
  1019         eid = self.rqlexec(rql, kwargs, ask_confirm=self.verbosity>=2).rows[0][0]
       
  1020         if commit:
       
  1021             self.commit()
       
  1022         return eid
       
  1023 
  1017 
  1024     def sqlexec(self, sql, args=None, ask_confirm=True):
  1018     def sqlexec(self, sql, args=None, ask_confirm=True):
  1025         """execute the given sql if confirmed
  1019         """execute the given sql if confirmed
  1026 
  1020 
  1027         should only be used for low level stuff undoable with existing higher
  1021         should only be used for low level stuff undoable with existing higher