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


Python Session.commit方法代码示例

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


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

示例1: post

# 需要导入模块: from session import Session [as 别名]
# 或者: from session.Session import commit [as 别名]
  def post(self, id_):
    """ Publish to BBS
    """
    errors = {}

    username = self.get_argument("username", "名無し")
    if username == "":
      username = "名無し"
    mail = self.get_argument("mail")

    text = self.get_argument("text")
    if text == "":
      errors["text"] = True

    if not errors:
      session = Session()
      res = Response(text, username)
      res.bbs_id = id_
      session.add(res)
      session.commit()
      session.close()

    responses = self.get_responses(id_)

    self.render("BBS/index.html",
        responses=responses,
        errors=errors
    )
开发者ID:kouki-dan,项目名称:ShootingStarChecker,代码行数:30,代码来源:BBSHandler.py

示例2: upgrade

# 需要导入模块: from session import Session [as 别名]
# 或者: from session.Session import commit [as 别名]
    def upgrade(self):
        cnx = connector.connect(
            host=self.config.host,
            user=self.config.username,
            password=self.config.password)

        db_cursor = cnx.cursor()

        db_cursor.execute("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '{0}'".format(self.config.database))
        db_exists = db_cursor.fetchone()

        if not db_exists:
            try:
                db_cursor.execute("CREATE DATABASE {0}".format(self.config.database))
            except Exception as ex:
                message = 'Could not create database: ' + ex.msg
                logging.error(message)
                raise Exception(message)

        cnx.database = self.config.database

        session = Session(cnx)

        # do it.
        session.execute("""
            CREATE TABLE IF NOT EXISTS upgrade_history (
              id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
              upgrade_date DATETIME,
              version INTEGER
            )""")

        session.execute("SELECT IFNULL(MAX(version),0) FROM upgrade_history")
        db_version = session.fetch_results()[0][0]  # first row first col

        script = self.__create_upgrade_script()

        max_version = 0
        for query in script:
            if query['version'] > db_version:
                if query['version'] > max_version:
                    max_version = query['version']
                session.execute(query['sql'])

        if max_version > db_version:
            session.execute("""INSERT INTO upgrade_history (upgrade_date, version)
                          VALUES('{date}', '{version}');""".format(date=datetime.now().isoformat(), version=max_version))
            logginghelper.log_debug('Upgraded database to version ' + str(max_version))

        session.commit()
        session.close()
开发者ID:Shaggy84675,项目名称:ottdstats,代码行数:52,代码来源:mysql_database.py

示例3: saveEntity

# 需要导入模块: from session import Session [as 别名]
# 或者: from session.Session import commit [as 别名]
	def saveEntity(self, entity):
		session = Session()
		session.add(entity)
		session.commit()
开发者ID:Cbrdiv,项目名称:hacklog,代码行数:6,代码来源:accessdata.py

示例4: mergeEntity

# 需要导入模块: from session import Session [as 别名]
# 或者: from session.Session import commit [as 别名]
	def mergeEntity(self, entity):
		session = Session()
		session.merge(entity)
		session.commit()
开发者ID:Cbrdiv,项目名称:hacklog,代码行数:6,代码来源:accessdata.py

示例5: BaseHandler

# 需要导入模块: from session import Session [as 别名]
# 或者: from session.Session import commit [as 别名]
class BaseHandler(tornado.web.RequestHandler):
    """A class to collect common handler methods - all other handlers should
    subclass this one.
    """
    tornado.web.RequestHandler.write_error = error_handler
    
    def get_current_user(self):
        """ 
            Returns the models.User object representing the currently
            logged in user.  
            
            The get_current_user will only work if the the user is both
            logged in and has purchased the film
            
            There is also an override for admin accounts
        """
        user = self.get_secure_cookie('user')
        return self.session.query(User).filter_by(link=user).first()
        
    
    def show_error_message(self, message):
        self.set_secure_cookie('error_message', message)

    def show_message(self, message):
        self.set_secure_cookie('message', message)
    
    def login_user(self, user):
        self.set_secure_cookie('user', user.link)
    
    def logout_user(self, user):
        self.clear_cookie('user')
       
    def prepare(self):
        p3p = 'CP="Like Facebook, Santa does not have a P3P policy -Learn why: http://fb.me/p3p"'
        self.add_header('Accept-Charset', 'utf-8')
        self.set_header('P3P', p3p)
        self.session = Session()
    
    def on_finish(self):
        try:
            self.session.commit()            
        except Exception as e:
            self.session.rollback()
            import traceback
            logging.critical('Transaction needed to be rolled back because of: \n %s' % traceback.format_exc() )
        Session.remove()
    
    
    def respond_with_json(self, success, message, other_info={}):
        response_object = {'success':success, 'message':message}
        response_object.update(other_info)
        self.write(json.dumps(response_object))
        return
    
    def render_template(self, template, **kwargs):
        """
            An extension to the render() function.
            This adds things linke message, google analytics key, facebook appid,
            mixpanel token and the current_user's name to the page at render time.
            The args parameter allows you to add adhoc variables to a page as well.
            
            Remember that the args variable must be accessed like args['vari'] in the
            tempalte.  
        """
      
        current_user = self.get_current_user()
        current_user_name = ''
        current_user_unique_id = ''
        user_has_purchased = False
            
        
        # Grab the cookie messages
        cookie_message = self.get_secure_cookie('message')
        error_message = self.get_secure_cookie('error_message')
        self.set_secure_cookie("message", '')
        self.set_secure_cookie("error_message", '')
        
        kwargs.update({
            'user':current_user,
            'error_message':error_message,
            'message':cookie_message,
            'facebook_api_key': settings['facebook_api_key'],
            'domain':settings['domain']
        })
        
        template = 'templates/%s'%template
        return self.render( template, **kwargs)
开发者ID:gmacleod,项目名称:beautytube,代码行数:89,代码来源:base.py


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