本文整理汇总了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
)
示例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()
示例3: saveEntity
# 需要导入模块: from session import Session [as 别名]
# 或者: from session.Session import commit [as 别名]
def saveEntity(self, entity):
session = Session()
session.add(entity)
session.commit()
示例4: mergeEntity
# 需要导入模块: from session import Session [as 别名]
# 或者: from session.Session import commit [as 别名]
def mergeEntity(self, entity):
session = Session()
session.merge(entity)
session.commit()
示例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)