本文整理汇总了Python中cms.db.Session类的典型用法代码示例。如果您正苦于以下问题:Python Session类的具体用法?Python Session怎么用?Python Session使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Session类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: CommonRequestHandler
class CommonRequestHandler(RequestHandler):
"""Encapsulates shared RequestHandler functionality.
"""
# Whether the login cookie duration has to be refreshed when
# this handler is called. Useful to filter asynchronous
# requests.
refresh_cookie = True
def __init__(self, *args, **kwargs):
super(CommonRequestHandler, self).__init__(*args, **kwargs)
self.timestamp = None
self.sql_session = None
self.r_params = None
self.contest = None
self.url = None
def prepare(self):
"""This method is executed at the beginning of each request.
"""
super(CommonRequestHandler, self).prepare()
self.timestamp = make_datetime()
self.set_header("Cache-Control", "no-cache, must-revalidate")
self.sql_session = Session()
self.sql_session.expire_all()
self.url = create_url_builder(get_url_root(self.request.path))
@property
def service(self):
return self.application.service
示例2: AddStatementHandler
class AddStatementHandler(BaseHandler):
"""Add a statement to a task.
"""
@require_permission(BaseHandler.PERMISSION_ALL)
def get(self, task_id):
task = self.safe_get_item(Task, task_id)
self.r_params = self.render_params()
self.r_params["task"] = task
self.render("add_statement.html", **self.r_params)
@require_permission(BaseHandler.PERMISSION_ALL)
def post(self, task_id):
fallback_page = "/task/%s/statements/add" % task_id
task = self.safe_get_item(Task, task_id)
language = self.get_argument("language", None)
if language is None:
self.application.service.add_notification(
make_datetime(), "No language code specified",
"The language code can be any string.")
self.redirect(fallback_page)
return
statement = self.request.files["statement"][0]
if not statement["filename"].endswith(".pdf"):
self.application.service.add_notification(
make_datetime(), "Invalid task statement",
"The task statement must be a .pdf file.")
self.redirect(fallback_page)
return
task_name = task.name
self.sql_session.close()
try:
digest = self.application.service.file_cacher.put_file_content(
statement["body"],
"Statement for task %s (lang: %s)" % (task_name, language))
except Exception as error:
self.application.service.add_notification(
make_datetime(), "Task statement storage failed", repr(error))
self.redirect(fallback_page)
return
# TODO verify that there's no other Statement with that language
# otherwise we'd trigger an IntegrityError for constraint violation
self.sql_session = Session()
task = self.safe_get_item(Task, task_id)
self.contest = task.contest
statement = Statement(language, digest, task=task)
self.sql_session.add(statement)
if self.try_commit():
self.redirect("/task/%s" % task_id)
else:
self.redirect(fallback_page)
示例3: CommonRequestHandler
class CommonRequestHandler(RequestHandler):
"""Encapsulates shared RequestHandler functionality.
"""
# Whether the login cookie duration has to be refreshed when
# this handler is called. Useful to filter asynchronous
# requests.
refresh_cookie = True
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.timestamp = make_datetime()
self.sql_session = Session()
self.r_params = None
self.contest = None
self.url = None
def prepare(self):
"""This method is executed at the beginning of each request.
"""
super().prepare()
self.url = Url(get_url_root(self.request.path))
self.set_header("Cache-Control", "no-cache, must-revalidate")
def finish(self, *args, **kwargs):
"""Finish this response, ending the HTTP request.
We override this method in order to properly close the database.
TODO - Now that we have greenlet support, this method could be
refactored in terms of context manager or something like
that. So far I'm leaving it to minimize changes.
"""
if self.sql_session is not None:
try:
self.sql_session.close()
except Exception as error:
logger.warning("Couldn't close SQL connection: %r", error)
try:
super().finish(*args, **kwargs)
except OSError:
# When the client closes the connection before we reply,
# Tornado raises an OSError exception, that would pollute
# our log with unnecessarily critical messages
logger.debug("Connection closed before our reply.")
@property
def service(self):
return self.application.service
示例4: AddManagerHandler
class AddManagerHandler(BaseHandler):
"""Add a manager to a dataset.
"""
@require_permission(BaseHandler.PERMISSION_ALL)
def get(self, dataset_id):
dataset = self.safe_get_item(Dataset, dataset_id)
task = dataset.task
self.contest = task.contest
self.r_params = self.render_params()
self.r_params["task"] = task
self.r_params["dataset"] = dataset
self.render("add_manager.html", **self.r_params)
@require_permission(BaseHandler.PERMISSION_ALL)
def post(self, dataset_id):
fallback_page = self.url("dataset", dataset_id, "managers", "add")
dataset = self.safe_get_item(Dataset, dataset_id)
task = dataset.task
manager = self.request.files["manager"][0]
task_name = task.name
self.sql_session.close()
try:
digest = self.service.file_cacher.put_file_content(
manager["body"],
"Task manager for %s" % task_name)
except Exception as error:
self.service.add_notification(
make_datetime(),
"Manager storage failed",
repr(error))
self.redirect(fallback_page)
return
self.sql_session = Session()
dataset = self.safe_get_item(Dataset, dataset_id)
task = dataset.task
manager = Manager(manager["filename"], digest, dataset=dataset)
self.sql_session.add(manager)
if self.try_commit():
self.redirect(self.url("task", task.id))
else:
self.redirect(fallback_page)
示例5: AddSpoilerHandler
class AddSpoilerHandler(BaseHandler):
"""Add a spoiler to a task.
"""
@require_permission(BaseHandler.PERMISSION_ALL)
def get(self, task_id):
task = self.safe_get_item(Task, task_id)
self.contest = task.contest
self.r_params = self.render_params()
self.r_params["task"] = task
self.render("add_spoiler.html", **self.r_params)
@require_permission(BaseHandler.PERMISSION_ALL)
def post(self, task_id):
fallback_page = self.url("task", task_id, "spoiler", "add")
task = self.safe_get_item(Task, task_id)
spoiler = self.request.files["spoiler"][0]
task_name = task.name
self.sql_session.close()
try:
digest = self.service.file_cacher.put_file_content(
spoiler["body"],
"Task spoiler for %s" % task_name)
except Exception as error:
self.service.add_notification(
make_datetime(),
"Spoiler storage failed",
repr(error))
self.redirect(fallback_page)
return
# TODO verify that there's no other Spoiler with that filename
# otherwise we'd trigger an IntegrityError for constraint violation
self.sql_session = Session()
task = self.safe_get_item(Task, task_id)
spoiler = Spoiler(spoiler["filename"], digest, task=task)
self.sql_session.add(spoiler)
if self.try_commit():
self.redirect(self.url("task", task_id))
else:
self.redirect(fallback_page)
示例6: post
def post(self, task_id):
fallback_page = self.url("task", task_id, "spoiler", "add")
task = self.safe_get_item(Task, task_id)
spoiler = self.request.files["spoiler"][0]
task_name = task.name
self.sql_session.close()
try:
digest = self.service.file_cacher.put_file_content(
spoiler["body"],
"Task spoiler for %s" % task_name)
except Exception as error:
self.service.add_notification(
make_datetime(),
"Spoiler storage failed",
repr(error))
self.redirect(fallback_page)
return
# TODO verify that there's no other Spoiler with that filename
# otherwise we'd trigger an IntegrityError for constraint violation
self.sql_session = Session()
task = self.safe_get_item(Task, task_id)
spoiler = Spoiler(spoiler["filename"], digest, task=task)
self.sql_session.add(spoiler)
if self.try_commit():
self.redirect(self.url("task", task_id))
else:
self.redirect(fallback_page)
示例7: post
def post(self, dataset_id):
fallback_page = "/dataset/%s/managers/add" % dataset_id
dataset = self.safe_get_item(Dataset, dataset_id)
task = dataset.task
manager = self.request.files["manager"][0]
task_name = task.name
self.sql_session.close()
try:
digest = self.application.service.file_cacher.put_file_content(
manager["body"],
"Task manager for %s" % task_name)
except Exception as error:
self.application.service.add_notification(
make_datetime(),
"Manager storage failed",
repr(error))
self.redirect(fallback_page)
return
self.sql_session = Session()
dataset = self.safe_get_item(Dataset, dataset_id)
task = dataset.task
manager = Manager(manager["filename"], digest, dataset=dataset)
self.sql_session.add(manager)
if self.try_commit():
self.redirect("/task/%s" % task.id)
else:
self.redirect(fallback_page)
示例8: post
def post(self, task_id):
fallback_page = "/task/%s/attachments/add" % task_id
task = self.safe_get_item(Task, task_id)
attachment = self.request.files["attachment"][0]
task_name = task.name
self.sql_session.close()
try:
digest = self.application.service.file_cacher.put_file_content(
attachment["body"],
"Task attachment for %s" % task_name)
except Exception as error:
self.application.service.add_notification(
make_datetime(),
"Attachment storage failed",
repr(error))
self.redirect(fallback_page)
return
# TODO verify that there's no other Attachment with that filename
# otherwise we'd trigger an IntegrityError for constraint violation
self.sql_session = Session()
task = self.safe_get_item(Task, task_id)
attachment = Attachment(attachment["filename"], digest, task=task)
self.sql_session.add(attachment)
if self.try_commit():
self.redirect("/task/%s" % task_id)
else:
self.redirect(fallback_page)
示例9: __init__
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.timestamp = make_datetime()
self.sql_session = Session()
self.r_params = None
self.contest = None
self.url = None