[datafeed] for datafeed source, we don't want commit in extid2eid but explicitly handled by the source. Also, we should use 'safe' internal session stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 27 Jul 2011 14:24:40 +0200
branchstable
changeset 7708 45be3a9debe6
parent 7707 936530f8d32c
child 7709 83534bb9d893
[datafeed] for datafeed source, we don't want commit in extid2eid but explicitly handled by the source. Also, we should use 'safe' internal session
hooks/__init__.py
hooks/metadata.py
server/repository.py
server/sources/datafeed.py
sobjects/test/unittest_parsers.py
--- a/hooks/__init__.py	Wed Jul 27 14:22:32 2011 +0200
+++ b/hooks/__init__.py	Wed Jul 27 14:24:40 2011 +0200
@@ -57,7 +57,7 @@
                     or not repo.config.source_enabled(source)
                     or not source.config['synchronize']):
                     continue
-                session = repo.internal_session()
+                session = repo.internal_session(safe=True)
                 try:
                     stats = source.pull_data(session)
                     if stats.get('created'):
--- a/hooks/metadata.py	Wed Jul 27 14:22:32 2011 +0200
+++ b/hooks/metadata.py	Wed Jul 27 14:24:40 2011 +0200
@@ -42,8 +42,10 @@
     def __call__(self):
         timestamp = datetime.now()
         edited = self.entity.cw_edited
-        edited.setdefault('creation_date', timestamp)
-        edited.setdefault('modification_date', timestamp)
+        if not edited.get('creation_date'):
+            edited['creation_date'] = timestamp
+        if not edited.get('modification_date'):
+            edited['modification_date'] = timestamp
         if not self._cw.get_shared_data('do-not-insert-cwuri'):
             cwuri = u'%s%s' % (self._cw.base_url(), self.entity.eid)
             edited.setdefault('cwuri', cwuri)
--- a/server/repository.py	Wed Jul 27 14:22:32 2011 +0200
+++ b/server/repository.py	Wed Jul 27 14:24:40 2011 +0200
@@ -1032,7 +1032,7 @@
         return extid
 
     def extid2eid(self, source, extid, etype, session=None, insert=True,
-                  complete=True, sourceparams=None):
+                  complete=True, commit=True, sourceparams=None):
         """Return eid from a local id. If the eid is a negative integer, that
         means the entity is known but has been copied back to the system source
         hence should be ignored.
@@ -1095,10 +1095,12 @@
             source.after_entity_insertion(session, extid, entity, sourceparams)
             if source.should_call_hooks:
                 self.hm.call_hooks('after_add_entity', session, entity=entity)
-            session.commit(free_cnxset)
+            if commit:
+                session.commit(free_cnxset)
             return eid
-        except:
-            session.rollback(free_cnxset)
+        except Exception:
+            if commit:
+                session.rollback(free_cnxset)
             raise
 
     def add_info(self, session, entity, source, extid=None, complete=True):
--- a/server/sources/datafeed.py	Wed Jul 27 14:22:32 2011 +0200
+++ b/server/sources/datafeed.py	Wed Jul 27 14:24:40 2011 +0200
@@ -147,6 +147,7 @@
         return True
 
     def release_synchronization_lock(self, session):
+        session.set_cnxset()
         session.execute('SET X synchronizing FALSE WHERE X eid %(x)s',
                         {'x': self.eid})
         session.commit()
@@ -276,7 +277,7 @@
         sourceparams['parser'] = self
         try:
             eid = session.repo.extid2eid(source, str(uri), etype, session,
-                                         complete=False,
+                                         complete=False, commit=False,
                                          sourceparams=sourceparams)
         except ValidationError, ex:
             self.source.error('error while creating %s: %s', etype, ex)
--- a/sobjects/test/unittest_parsers.py	Wed Jul 27 14:22:32 2011 +0200
+++ b/sobjects/test/unittest_parsers.py	Wed Jul 27 14:24:40 2011 +0200
@@ -113,6 +113,9 @@
   <last_login_time>2011-01-25 14:14:06</last_login_time>
   <creation_date>2010-01-22 10:27:59</creation_date>
   <modification_date>2011-01-25 14:14:06</modification_date>
+  <in_group role="subject">
+    <CWGroup cwuri="http://pouet.org/7" eid="7"/>
+  </in_group>
  </CWUser>
 </rset>
 '''.splitlines()
@@ -126,12 +129,12 @@
     test_db_id = 'xmlparser'
     @classmethod
     def pre_setup_database(cls, session, config):
-        source = session.create_entity('CWSource', name=u'myfeed', type=u'datafeed',
+        myfeed = session.create_entity('CWSource', name=u'myfeed', type=u'datafeed',
                                    parser=u'cw.entityxml', url=BASEXML)
-        session.create_entity('CWSource', name=u'myotherfeed', type=u'datafeed',
-                              parser=u'cw.entityxml', url=OTHERXML)
+        myotherfeed = session.create_entity('CWSource', name=u'myotherfeed', type=u'datafeed',
+                                            parser=u'cw.entityxml', url=OTHERXML)
         session.commit()
-        source.init_mapping([(('CWUser', 'use_email', '*'),
+        myfeed.init_mapping([(('CWUser', 'use_email', '*'),
                               u'role=subject\naction=copy'),
                              (('CWUser', 'in_group', '*'),
                               u'role=subject\naction=link\nlinkattr=name'),
@@ -140,6 +143,11 @@
                              (('*', 'tags', 'CWUser'),
                               u'role=object\naction=link-or-create\nlinkattr=name'),
                             ])
+        myotherfeed.init_mapping([(('CWUser', 'in_group', '*'),
+                                   u'role=subject\naction=link\nlinkattr=name'),
+                                  (('CWUser', 'in_state', '*'),
+                                   u'role=subject\naction=link\nlinkattr=name'),
+                                  ])
         session.create_entity('Tag', name=u'hop')
 
     def test_complete_url(self):
@@ -171,7 +179,7 @@
                                  (u'EmailAddress', {})]
                              }
                           })
-        session = self.repo.internal_session()
+        session = self.repo.internal_session(safe=True)
         stats = dfsource.pull_data(session, force=True, raise_on_error=True)
         self.assertEqual(sorted(stats.keys()), ['created', 'updated'])
         self.assertEqual(len(stats['created']), 2)
@@ -255,7 +263,7 @@
 
     def test_external_entity(self):
         dfsource = self.repo.sources_by_uri['myotherfeed']
-        session = self.repo.internal_session()
+        session = self.repo.internal_session(safe=True)
         stats = dfsource.pull_data(session, force=True, raise_on_error=True)
         user = self.execute('CWUser X WHERE X login "sthenault"').get_entity(0, 0)
         self.assertEqual(user.creation_date, datetime(2010, 01, 22, 10, 27, 59))
@@ -265,7 +273,7 @@
 
     def test_noerror_missing_fti_attribute(self):
         dfsource = self.repo.sources_by_uri['myfeed']
-        session = self.repo.internal_session()
+        session = self.repo.internal_session(safe=True)
         parser = dfsource._get_parser(session)
         dfsource.process_urls(parser, ['''
 <rset size="1">
@@ -277,7 +285,7 @@
 
     def test_noerror_unspecified_date(self):
         dfsource = self.repo.sources_by_uri['myfeed']
-        session = self.repo.internal_session()
+        session = self.repo.internal_session(safe=True)
         parser = dfsource._get_parser(session)
         dfsource.process_urls(parser, ['''
 <rset size="1">