transaction.py
changeset 10360 1fdbe2ea63d8
parent 10235 684215aca046
child 10666 7f6b5f023884
equal deleted inserted replaced
10359:8e4ba1028f55 10360:1fdbe2ea63d8
    34 class NoSuchTransaction(RepositoryError):
    34 class NoSuchTransaction(RepositoryError):
    35     # Used by CubicWebException
    35     # Used by CubicWebException
    36     msg = _("there is no transaction #%s")
    36     msg = _("there is no transaction #%s")
    37 
    37 
    38     def __init__(self, txuuid):
    38     def __init__(self, txuuid):
    39         super(RepositoryError, self).__init__(txuuid)
    39         super(NoSuchTransaction, self).__init__(txuuid)
    40         self.txuuid = txuuid
    40         self.txuuid = txuuid
    41 
    41 
    42 class Transaction(object):
    42 class Transaction(object):
    43     """an undoable transaction"""
    43     """an undoable transaction"""
    44 
    44 
    45     def __init__(self, uuid, time, ueid):
    45     def __init__(self, cnx, uuid, time, ueid):
       
    46         self.cnx = cnx
    46         self.uuid = uuid
    47         self.uuid = uuid
    47         self.datetime = time
    48         self.datetime = time
    48         self.user_eid = ueid
    49         self.user_eid = ueid
    49         # should be set by the dbapi connection
       
    50         self.req = None  # old style
       
    51         self.cnx = None  # new style
       
    52 
    50 
    53     def _execute(self, *args, **kwargs):
    51     def _execute(self, *args, **kwargs):
    54         """execute a query using either the req or the cnx"""
    52         """execute a query using either the req or the cnx"""
    55         if self.req is None:
    53         return self.cnx.execute(*args, **kwargs)
    56             execute = self.cnx.execute
       
    57         else:
       
    58             execute = self.req
       
    59         return execute(*args, **kwargs)
       
    60 
    54 
    61 
    55 
    62     def __repr__(self):
    56     def __repr__(self):
    63         return '<Transaction %s by %s on %s>' % (
    57         return '<Transaction %s by %s on %s>' % (
    64             self.uuid, self.user_eid, self.datetime)
    58             self.uuid, self.user_eid, self.datetime)
    65 
    59 
    66     def user(self):
    60     def user(self):
    67         """return the user entity which has done the transaction,
    61         """return the user entity which has done the transaction,
    68         none if not found.
    62         none if not found.
    69         """
    63         """
    70         return self._execute('Any X WHERE X eid %(x)s',
    64         return self.cnx.find('CWUser', eid=self.user_eid).one()
    71                              {'x': self.user_eid}).get_entity(0, 0)
       
    72 
    65 
    73     def actions_list(self, public=True):
    66     def actions_list(self, public=True):
    74         """return an ordered list of action effectued during that transaction
    67         """return an ordered list of action effectued during that transaction
    75 
    68 
    76         if public is true, return only 'public' action, eg not ones triggered
    69         if public is true, return only 'public' action, eg not ones triggered
    77         under the cover by hooks.
    70         under the cover by hooks.
    78         """
    71         """
    79         if self.req is not None:
    72         return self.cnx.transaction_actions(self.uuid, public)
    80             cnx = self.req.cnx
       
    81         else:
       
    82             cnx = self.cnx
       
    83         return cnx.transaction_actions(self.uuid, public)
       
    84 
    73 
    85 
    74 
    86 class AbstractAction(object):
    75 class AbstractAction(object):
       
    76 
    87     def __init__(self, action, public, order):
    77     def __init__(self, action, public, order):
    88         self.action = action
    78         self.action = action
    89         self.public = public
    79         self.public = public
    90         self.order = order
    80         self.order = order
    91 
    81 
    98         """ Return the entity or relation type this action is related to"""
    88         """ Return the entity or relation type this action is related to"""
    99         raise NotImplementedError(self)
    89         raise NotImplementedError(self)
   100 
    90 
   101 
    91 
   102 class EntityAction(AbstractAction):
    92 class EntityAction(AbstractAction):
       
    93 
   103     def __init__(self, action, public, order, etype, eid, changes):
    94     def __init__(self, action, public, order, etype, eid, changes):
   104         AbstractAction.__init__(self, action, public, order)
    95         super(EntityAction, self).__init__(action, public, order)
   105         self.etype = etype
    96         self.etype = etype
   106         self.eid = eid
    97         self.eid = eid
   107         self.changes = changes
    98         self.changes = changes
   108 
    99 
   109     def __repr__(self):
   100     def __repr__(self):
   116         """ Return the entity or relation type this action is related to"""
   107         """ Return the entity or relation type this action is related to"""
   117         return self.etype
   108         return self.etype
   118 
   109 
   119 
   110 
   120 class RelationAction(AbstractAction):
   111 class RelationAction(AbstractAction):
       
   112 
   121     def __init__(self, action, public, order, rtype, eidfrom, eidto):
   113     def __init__(self, action, public, order, rtype, eidfrom, eidto):
   122         AbstractAction.__init__(self, action, public, order)
   114         super(RelationAction, self).__init__(action, public, order)
   123         self.rtype = rtype
   115         self.rtype = rtype
   124         self.eid_from = eidfrom
   116         self.eid_from = eidfrom
   125         self.eid_to = eidto
   117         self.eid_to = eidto
   126 
   118 
   127     def __repr__(self):
   119     def __repr__(self):