本文整理匯總了Python中django.contrib.sessions.models.Session方法的典型用法代碼示例。如果您正苦於以下問題:Python models.Session方法的具體用法?Python models.Session怎麽用?Python models.Session使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.contrib.sessions.models
的用法示例。
在下文中一共展示了models.Session方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_expirable_session_var
# 需要導入模塊: from django.contrib.sessions import models [as 別名]
# 或者: from django.contrib.sessions.models import Session [as 別名]
def get_expirable_session_var(session: Session, var_name: str, default_value: Any=None,
delete: bool=False) -> Any:
if var_name not in session:
return default_value
try:
value, expire_at = (session[var_name]['value'], session[var_name]['expire_at'])
except (KeyError, TypeError):
logging.warning("get_expirable_session_var: error getting %s", var_name, exc_info=True)
return default_value
if timestamp_to_datetime(expire_at) < timezone_now():
del session[var_name]
return default_value
if delete:
del session[var_name]
return value
示例2: is_locked
# 需要導入模塊: from django.contrib.sessions import models [as 別名]
# 或者: from django.contrib.sessions.models import Session [as 別名]
def is_locked(self) -> bool:
"""Check if the workflow is locked.
:return: Is the given workflow locked?
"""
if not self.session_key:
# No key in the workflow, then it is not locked.
return False
session = Session.objects.filter(session_key=self.session_key).first()
if not session:
# Session does not exist, then it is not locked
return False
# Session is in the workflow and in the session table. Locked if expire
# date is beyond the current time.
return session.expire_date >= timezone.now()
示例3: _wf_lock_and_update
# 需要導入模塊: from django.contrib.sessions import models [as 別名]
# 或者: from django.contrib.sessions.models import Session [as 別名]
def _wf_lock_and_update(
session: SessionStore,
user: get_user_model(),
workflow: models.Workflow,
create_session: Optional[bool] = False,
) -> models.Workflow:
"""Lock a workflow and updates the value in the session.
:param session: Session being used
:param user: User requesting the lock
:param workflow: Workflow being modified
:param create_session: Boolean encoding if a session needs to be created.
:param workflow: Object to store
"""
workflow.lock(session, user, create_session)
# Update nrows in case it was asynch modified
_store_workflow_nrows_in_session(session, workflow)
return workflow
示例4: sessionend_handler
# 需要導入模塊: from django.contrib.sessions import models [as 別名]
# 或者: from django.contrib.sessions.models import Session [as 別名]
def sessionend_handler(sender, **kwargs):
# cleanup session (temp) data
logging.debug('Session closing, cleanup ..')
session = kwargs.get('instance').session_key
if session != '':
tempdir = os.path.join('data', 'session', session)
if os.path.exists(tempdir):
logging.debug('Deleting %s ..' % tempdir)
shutil.rmtree(tempdir)
示例5: get_session_user
# 需要導入模塊: from django.contrib.sessions import models [as 別名]
# 或者: from django.contrib.sessions.models import Session [as 別名]
def get_session_user(session: Session) -> Optional[int]:
return get_session_dict_user(session.get_decoded())
示例6: user_sessions
# 需要導入模塊: from django.contrib.sessions import models [as 別名]
# 或者: from django.contrib.sessions.models import Session [as 別名]
def user_sessions(user_profile: UserProfile) -> List[Session]:
return [s for s in Session.objects.all()
if get_session_user(s) == user_profile.id]
示例7: delete_session
# 需要導入模塊: from django.contrib.sessions import models [as 別名]
# 或者: from django.contrib.sessions.models import Session [as 別名]
def delete_session(session: Session) -> None:
session_engine.SessionStore(session.session_key).delete()
示例8: delete_user_sessions
# 需要導入模塊: from django.contrib.sessions import models [as 別名]
# 或者: from django.contrib.sessions.models import Session [as 別名]
def delete_user_sessions(user_profile: UserProfile) -> None:
for session in Session.objects.all():
if get_session_user(session) == user_profile.id:
delete_session(session)
示例9: delete_realm_user_sessions
# 需要導入模塊: from django.contrib.sessions import models [as 別名]
# 或者: from django.contrib.sessions.models import Session [as 別名]
def delete_realm_user_sessions(realm: Realm) -> None:
realm_user_ids = [user_profile.id for user_profile in
UserProfile.objects.filter(realm=realm)]
for session in Session.objects.filter(expire_date__gte=timezone_now()):
if get_session_user(session) in realm_user_ids:
delete_session(session)
示例10: delete_all_deactivated_user_sessions
# 需要導入模塊: from django.contrib.sessions import models [as 別名]
# 或者: from django.contrib.sessions.models import Session [as 別名]
def delete_all_deactivated_user_sessions() -> None:
for session in Session.objects.all():
user_profile_id = get_session_user(session)
if user_profile_id is None: # nocoverage # TODO: Investigate why we lost coverage on this
continue
user_profile = get_user_profile_by_id(user_profile_id)
if not user_profile.is_active or user_profile.realm.deactivated:
logging.info("Deactivating session for deactivated user %s", user_profile.id)
delete_session(session)
示例11: set_expirable_session_var
# 需要導入模塊: from django.contrib.sessions import models [as 別名]
# 或者: from django.contrib.sessions.models import Session [as 別名]
def set_expirable_session_var(session: Session, var_name: str, var_value: Any, expiry_seconds: int) -> None:
expire_at = datetime_to_timestamp(timezone_now() + timedelta(seconds=expiry_seconds))
session[var_name] = {'value': var_value, 'expire_at': expire_at}
示例12: session_cache_items
# 需要導入模塊: from django.contrib.sessions import models [as 別名]
# 或者: from django.contrib.sessions.models import Session [as 別名]
def session_cache_items(items_for_remote_cache: Dict[str, str],
session: Session) -> None:
if settings.SESSION_ENGINE != "django.contrib.sessions.backends.cached_db":
# If we're not using the cached_db session engine, we there
# will be no store.cache_key attribute, and in any case we
# don't need to fill the cache, since it won't exist.
return
store = session_engine.SessionStore(session_key=session.session_key)
items_for_remote_cache[store.cache_key] = store.decode(session.session_data)
示例13: get_user_locking_workflow
# 需要導入模塊: from django.contrib.sessions import models [as 別名]
# 或者: from django.contrib.sessions.models import Session [as 別名]
def get_user_locking_workflow(self):
"""Get the user that is locking a workflow.
Given a workflow that is supposed to be locked, it returns the user
that is locking it.
:return: The user object that is locking this workflow
"""
session = Session.objects.get(session_key=self.session_key)
session_data = session.get_decoded()
return get_user_model().objects.get(
id=session_data.get('_auth_user_id'))
示例14: _store_workflow_nrows_in_session
# 需要導入模塊: from django.contrib.sessions import models [as 別名]
# 或者: from django.contrib.sessions.models import Session [as 別名]
def _store_workflow_nrows_in_session(
session: SessionStore,
wflow: models.Workflow
):
"""Store the workflow id and name in the request.session dictionary.
:param session: Session object to store the worklfow
:param wflow: Workflow object
:return: Nothing. Store the id and the name in the session
"""
session['ontask_workflow_rows'] = wflow.nrows
session.save()
示例15: build_request
# 需要導入模塊: from django.contrib.sessions import models [as 別名]
# 或者: from django.contrib.sessions.models import Session [as 別名]
def build_request(self, **kwargs):
request_kwargs = {
"request_id": kwargs.get("request_id", 1),
"scope": kwargs.get(
"scope",
{
"user": kwargs.get("user", AnonymousUser()),
"session": kwargs.get("session", Session()),
"headers": kwargs.get("headers", ()),
},
),
"workflow": kwargs.get("workflow", Workflow()),
"path": kwargs.get("path", "test.path"),
"arguments": kwargs.get("arguments", {}),
}
for key in list(request_kwargs["scope"].keys()) + list(request_kwargs.keys()):
try:
del kwargs[key]
except KeyError:
pass
# Turn other params, like `wf_module_id=123`, into
# `arguments={'wf_module_id':123}`
request_kwargs["arguments"].update(kwargs)
return HandlerRequest(**request_kwargs)