This class provides an interface for storing, retrieving, and managing session data
with automatic expiration. It handles session data under a specific key and supports
timeout-based cleanup of expired sessions.
Args:
request (HttpRequest): The Django request object containing the session.
session_key (str): Unique key under which the session data will be stored.
seconds_till_expiry (int, optional): Number of seconds until session expiry.
Defaults to 300 seconds (5 minutes).
Source code in django_spire/contrib/session/controller.py
| def __init__(
self,
request: HttpRequest,
session_key: str,
seconds_till_expiry: int = 60 * 5
):
self.request = request
self.session_key = session_key
self.seconds_till_expiry = seconds_till_expiry
self.request.session.setdefault(self.session_key, dict())
self._session = self.request.session[self.session_key]
self._clean()
|
request = request
instance-attribute
session_key = session_key
instance-attribute
seconds_till_expiry = seconds_till_expiry
instance-attribute
timeout_datestamp
property
__getitem__
Source code in django_spire/contrib/session/controller.py
| def __getitem__(self, key: str) -> Any:
return self.data[key]
|
__setitem__
Source code in django_spire/contrib/session/controller.py
| def __setitem__(self, key: str, value: Any):
self.add_data(key, value)
|
add_data
Source code in django_spire/contrib/session/controller.py
| def add_data(self, key: str, data: Any):
self._session[key] = data
self._set_timeout_datestamp()
self._set_modified()
|
remove_data
Source code in django_spire/contrib/session/controller.py
| def remove_data(self, key: str):
self.data.pop(key)
self._set_modified()
# remove the session completely if there is no data in it.
if self._TIMEOUT_KEY in self.data and len(self.data.keys()) == 1:
self.data.pop(self._TIMEOUT_KEY)
|
purge
Source code in django_spire/contrib/session/controller.py
| def purge(self):
self.request.session.pop(self.session_key)
self._set_modified()
|
to_json
Source code in django_spire/contrib/session/controller.py
| def to_json(self):
return json.dumps(self.data, cls=DjangoJSONEncoder)
|