2015-08-19 20:53:55 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
from django.contrib.auth import SESSION_KEY, get_user_model
|
2016-06-04 19:52:48 +02:00
|
|
|
from django.contrib.sessions.models import Session
|
|
|
|
|
2016-06-28 17:28:19 +02:00
|
|
|
from typing import Mapping, Optional
|
2016-06-04 20:38:42 +02:00
|
|
|
from six import text_type
|
|
|
|
|
2015-08-19 20:53:55 +02:00
|
|
|
|
|
|
|
def get_session_dict_user(session_dict):
|
2016-06-28 17:28:19 +02:00
|
|
|
# type: (Mapping[text_type, int]) -> Optional[int]
|
2015-08-19 20:53:55 +02:00
|
|
|
# Compare django.contrib.auth._get_user_session_key
|
|
|
|
try:
|
|
|
|
return get_user_model()._meta.pk.to_python(session_dict[SESSION_KEY])
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_session_user(session):
|
2016-06-04 19:52:48 +02:00
|
|
|
# type: (Session) -> int
|
2015-08-19 20:53:55 +02:00
|
|
|
return get_session_dict_user(session.get_decoded())
|