コード例 #1
0
 def test_count_versions_with_multiple_objects(self):
     article = self.Article(name=u'Some article')
     self.session.add(article)
     article2 = self.Article(name=u'Some article')
     self.session.add(article2)
     self.session.commit()
     assert count_versions(article) == 1
コード例 #2
0
 def test_count_versions_with_multiple_versions(self):
     article = self.Article(name=u'Some article')
     self.session.add(article)
     self.session.commit()
     article.name = u'Updated article'
     self.session.commit()
     assert count_versions(article) == 2
コード例 #3
0
 def save_or_update_transaction(self):
     f_keys = self.get_f_keys()
     if hasattr(self.instance, 'locked'):
         if self.instance.locked:
             model_factory = self.factory_table_transaction(f_keys)
             transaction_model = self.get_transaction_by_id(model_factory)
             if transaction_model:
                 model_factory_instance = transaction_model
             else:
                 model_factory_instance = model_factory()
             model_factory_instance.entity_oid = self.instance.oid
             transaction_id = self.instance.versions[count_versions(self.instance) - 1]. \
                 transaction.id
             for f_key in f_keys:
                 f_key_oid = f_key + '_oid'
                 if not self.old_instance or \
                         (self.old_instance and
                          getattr(self.old_instance, f_key_oid) != getattr(self.instance, f_key_oid)):
                     if not getattr(self.instance, f_key_oid):
                         setattr(model_factory_instance, f_key + '_id_tr',
                                 None)
                     else:
                         setattr(model_factory_instance, f_key + '_id_tr',
                                 transaction_id)
             if not transaction_model:
                 self.session.add(model_factory_instance)
             self.session.commit()
コード例 #4
0
    def records(self):
        """
        Return list of records in versioning history.
        """
        class VersionedInstanceMixin(object):
            __abstract__ = True

            @property
            def previous(self):
                return self.__version__.previous

            @property
            def next(self):
                return self.__version__.next

            @property
            def index(self):
                return self.__version__.index

            def revert(self):
                self.__version__.revert()
                return

        # dynamically create new type with mixin functionality
        VersionedClass = type('{}Record'.format(self.__class__.__name__),
                              (VersionedInstanceMixin, self.__class__), {})

        # gather cols
        columns = []
        mapper = inspect(self.__class__)
        for col in mapper.attrs:
            if hasattr(col, 'columns'):
                columns.append(col.columns[0].key)

        # configure versioning
        proxies = []
        for idx in range(count_versions(self)):
            record = self.versions[idx]

            # get column data
            data = {}
            for k in columns:
                if k in record.__dict__:
                    data[k] = getattr(record, k)
                else:
                    data[k] = getattr(self, k)

            # create new abstract object
            item = VersionedClass(**data)
            item.__version__ = record
            proxies.append(item)

        return proxies
コード例 #5
0
 def test_insert(self):
     item = self.TextItem()
     self.session.add(item)
     self.session.commit()
     assert count_versions(item) == 1
コード例 #6
0
 def test_count_versions_with_initial_version(self):
     article = self.Article(name=u'Some article')
     self.session.add(article)
     self.session.commit()
     assert count_versions(article) == 1
コード例 #7
0
 def test_count_versions_without_versions(self):
     article = self.Article(name=u'Some article')
     assert count_versions(article) == 0
コード例 #8
0
 def test_insert(self):
     item = self.TextItem()
     self.session.add(item)
     self.session.commit()
     assert count_versions(item) == 1
コード例 #9
0
 def modified(self):
     """
     Return boolean describing if object has been modified.
     """
     return count_versions(self) > 0