当前位置: 首页>>代码示例>>Python>>正文


Python Session.commit方法代码示例

本文整理汇总了Python中cms.db.Session.commit方法的典型用法代码示例。如果您正苦于以下问题:Python Session.commit方法的具体用法?Python Session.commit怎么用?Python Session.commit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在cms.db.Session的用法示例。


在下文中一共展示了Session.commit方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: from cms.db import Session [as 别名]
# 或者: from cms.db.Session import commit [as 别名]
def main():
	# Get session
	session = Session()
	# Apre una sessione e lista i forum
	# forums = session.query(Forum).all()
	forums = session.query(forum.Forum).all()
	print "Forums found: ", len(forums)
	params = {'title': "Discussioni sul sito", 'description': "Per malfunzionamenti, dubbi o suggerimenti relativi al sito."}
	my_forum = forum.Forum(**params)
	session.add(my_forum)
	session.commit()
开发者ID:acube-unipi,项目名称:oii-web,代码行数:13,代码来源:add_forum.py

示例2: TestCaseWithDatabase

# 需要导入模块: from cms.db import Session [as 别名]
# 或者: from cms.db.Session import commit [as 别名]
class TestCaseWithDatabase(unittest.TestCase):
    """TestCase subclass starting with a clean testing database."""

    @classmethod
    def setUpClass(cls):
        assert "fortesting" in str(cms.db.engine), \
            "Monkey patching of DB connection string failed"
        drop_db()
        init_db()
        cls.connection = cms.db.engine.connect()
        cms.db.metadata.create_all(cls.connection)

    @classmethod
    def tearDownClass(cls):
        drop_db()
        cls.connection.close()
        cms.db.engine.dispose()

    def setUp(self):
        self.session = Session()

    def tearDown(self):
        self.session.rollback()

    def delete_data(self):
        """Delete all the data in the DB.

        This is useful to call during tear down, for tests that rely on
        starting from a clean DB.

        """
        for table in itervalues(Base.metadata.tables):
            self.session.execute(table.delete())
        self.session.commit()

    @staticmethod
    def add_fsobject(digest, content):
        dbbackend = DBBackend()
        fobj = dbbackend.create_file(digest)
        fobj.write(content)
        dbbackend.commit_file(fobj, digest)

    @staticmethod
    def get_contest(**kwargs):
        """Create a contest"""
        args = {
            "name": unique_unicode_id(),
            "description": unique_unicode_id(),
        }
        args.update(kwargs)
        contest = Contest(**args)
        return contest

    def add_contest(self, **kwargs):
        """Create a contest and add it to the session"""
        contest = self.get_contest(**kwargs)
        self.session.add(contest)
        return contest

    @staticmethod
    def get_user(**kwargs):
        """Create a user"""
        args = {
            "username": unique_unicode_id(),
            "password": "",
            "first_name": unique_unicode_id(),
            "last_name": unique_unicode_id(),
        }
        args.update(kwargs)
        user = User(**args)
        return user

    def add_user(self, **kwargs):
        """Create a user and add it to the session"""
        user = self.get_user(**kwargs)
        self.session.add(user)
        return user

    @staticmethod
    def get_participation(user=None, contest=None, **kwargs):
        """Create a participation"""
        user = user if user is not None else TestCaseWithDatabase.get_user()
        contest = contest \
            if contest is not None else TestCaseWithDatabase.get_contest()
        args = {
            "user": user,
            "contest": contest,
        }
        args.update(kwargs)
        participation = Participation(**args)
        return participation

    def add_participation(self, **kwargs):
        """Create a participation and add it to the session"""
        participation = self.get_participation(**kwargs)
        self.session.add(participation)
        return participation

    @staticmethod
    def get_task(**kwargs):
#.........这里部分代码省略.........
开发者ID:akmohtashami,项目名称:cms,代码行数:103,代码来源:testdbgenerator.py

示例3: DatabaseMixin

# 需要导入模块: from cms.db import Session [as 别名]
# 或者: from cms.db.Session import commit [as 别名]
class DatabaseMixin(DatabaseObjectGeneratorMixin):
    """Mixin for tests with database access."""

    @classmethod
    def setUpClass(cls):
        super(DatabaseMixin, cls).setUpClass()
        assert "fortesting" in str(engine), \
            "Monkey patching of DB connection string failed"
        drop_db()
        init_db()

    @classmethod
    def tearDownClass(cls):
        drop_db()
        super(DatabaseMixin, cls).tearDownClass()

    def setUp(self):
        super(DatabaseMixin, self).setUp()
        self.session = Session()

    def tearDown(self):
        self.session.rollback()
        super(DatabaseMixin, self).tearDown()

    def delete_data(self):
        """Delete all the data in the DB.

        This is useful to call during tear down, for tests that rely on
        starting from a clean DB.

        """
        for table in itervalues(metadata.tables):
            self.session.execute(table.delete())
        self.session.commit()

    @staticmethod
    def add_fsobject(digest, content):
        dbbackend = DBBackend()
        fobj = dbbackend.create_file(digest)
        fobj.write(content)
        dbbackend.commit_file(fobj, digest)

    def add_contest(self, **kwargs):
        """Create a contest and add it to the session"""
        contest = self.get_contest(**kwargs)
        self.session.add(contest)
        return contest

    def add_announcement(self, **kwargs):
        """Create an announcement and add it to the session"""
        announcement = self.get_announcement(**kwargs)
        self.session.add(announcement)
        return announcement

    def add_user(self, **kwargs):
        """Create a user and add it to the session"""
        user = self.get_user(**kwargs)
        self.session.add(user)
        return user

    def add_participation(self, **kwargs):
        """Create a participation and add it to the session"""
        participation = self.get_participation(**kwargs)
        self.session.add(participation)
        return participation

    def add_message(self, **kwargs):
        """Create a message and add it to the session"""
        message = self.get_message(**kwargs)
        self.session.add(message)
        return message

    def add_question(self, **kwargs):
        """Create a question and add it to the session"""
        question = self.get_question(**kwargs)
        self.session.add(question)
        return question

    def add_task(self, **kwargs):
        """Create a task and add it to the session"""
        task = self.get_task(**kwargs)
        self.session.add(task)
        return task

    def add_statement(self, task=None, **kwargs):
        """Create a statement and add it to the session"""
        task = task if task is not None else self.add_task()
        args = {
            "task": task,
            "digest": unique_digest(),
            "language": unique_unicode_id(),
        }
        args.update(kwargs)
        statement = Statement(**args)
        self.session.add(statement)
        return statement

    def add_dataset(self, **kwargs):
        """Create a dataset and add it to the session"""
        dataset = self.get_dataset(**kwargs)
#.........这里部分代码省略.........
开发者ID:Nyrio,项目名称:cms,代码行数:103,代码来源:databasemixin.py


注:本文中的cms.db.Session.commit方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。