本文整理汇总了Python中django.contrib.sessions.backends.base.SessionBase方法的典型用法代码示例。如果您正苦于以下问题:Python base.SessionBase方法的具体用法?Python base.SessionBase怎么用?Python base.SessionBase使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.sessions.backends.base
的用法示例。
在下文中一共展示了base.SessionBase方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from django.contrib.sessions.backends import base [as 别名]
# 或者: from django.contrib.sessions.backends.base import SessionBase [as 别名]
def __init__(
self,
session: Optional[SessionBase] = None,
initial_values: Optional[Dict] = None,
):
"""Initialize the store with the given arguments."""
super().__init__()
self.__store = {}
if session:
in_session = session.get(PAYLOAD_SESSION_DICTIONARY)
if in_session:
self.__store = in_session
if initial_values:
self.__store.update(initial_values)
if session:
self.store_in_session(session)
示例2: get_user_from_session
# 需要导入模块: from django.contrib.sessions.backends import base [as 别名]
# 或者: from django.contrib.sessions.backends.base import SessionBase [as 别名]
def get_user_from_session(session: SessionBase) -> Union[User, AnonymousUser]:
"""
Get User object (or AnonymousUser() if not logged in) from session.
"""
try:
user_id = session[SESSION_KEY]
backend_path = session[BACKEND_SESSION_KEY]
backend = load_backend(backend_path)
return backend.get_user(user_id) or AnonymousUser()
except KeyError:
return AnonymousUser()
示例3: generic
# 需要导入模块: from django.contrib.sessions.backends import base [as 别名]
# 或者: from django.contrib.sessions.backends.base import SessionBase [as 别名]
def generic(self, *args, **kwargs):
request = super().generic(*args, **kwargs)
request.session = SessionBase()
request.user = self.user
request._messages = default_storage(request)
return request
示例4: flush
# 需要导入模块: from django.contrib.sessions.backends import base [as 别名]
# 或者: from django.contrib.sessions.backends.base import SessionBase [as 别名]
def flush(cls, session: SessionBase):
"""Remove the table from the session."""
session.pop(PAYLOAD_SESSION_DICTIONARY, None)
示例5: store_in_session
# 需要导入模块: from django.contrib.sessions.backends import base [as 别名]
# 或者: from django.contrib.sessions.backends.base import SessionBase [as 别名]
def store_in_session(self, session: SessionBase):
"""Set the payload in the current session.
:param session: Session object
"""
session[PAYLOAD_SESSION_DICTIONARY] = self.__store
session.save()
示例6: create_and_send_zip
# 需要导入模块: from django.contrib.sessions.backends import base [as 别名]
# 或者: from django.contrib.sessions.backends.base import SessionBase [as 别名]
def create_and_send_zip(
session: SessionBase,
action: models.Action,
item_column: models.Column,
user_fname_column: Optional[models.Column],
payload: SessionPayload,
) -> http.HttpResponse:
"""Process the list of tuples in files and create the ZIP BytesIO object.
:param session: Session object while creating a zip (need it to flush it)
:param action: Action being used for ZIP
:param item_column: Column used to itemize the zip
:param user_fname_column: Optional column to create file name
:param payload: Dictionary with additional parameters to create the ZIP
:return: HttpResponse to send back with the ZIP download header
"""
files = _create_eval_data_tuple(
action,
item_column,
payload.get('exclude_values', []),
user_fname_column)
file_name_template = _create_filename_template(payload, user_fname_column)
# Create the ZIP and return it for download
sbuf = BytesIO()
zip_file_obj = zipfile.ZipFile(sbuf, 'w')
for user_fname, part_id, msg_body in files:
if payload['zip_for_moodle']:
# If a zip for Moodle, field is Participant [number]. Take the
# number only
part_id = part_id.split()[1]
zip_file_obj.writestr(
file_name_template.format(user_fname=user_fname, part_id=part_id),
str(msg_body),
)
zip_file_obj.close()
SessionPayload.flush(session)
suffix = datetime.now().strftime('%y%m%d_%H%M%S')
# Attach the compressed value to the response and send
compressed_content = sbuf.getvalue()
response = http.HttpResponse(compressed_content)
response['Content-Type'] = 'application/x-zip-compressed'
response['Content-Transfer-Encoding'] = 'binary'
response['Content-Disposition'] = (
'attachment; filename="ontask_zip_action_{0}.zip"'.format(suffix))
response['Content-Length'] = str(len(compressed_content))
return response