v0.166.0
 1from functools import cached_property
 2from typing import TYPE_CHECKING
 3
 4from plain.views import View
 5
 6from .requests import get_request_session
 7
 8if TYPE_CHECKING:
 9    from .core import SessionStore
10
11__all__ = ["SessionView"]
12
13
14class SessionView(View):
15    """View with session access."""
16
17    @cached_property
18    def session(self) -> SessionStore:
19        """Get the session for this request."""
20        return get_request_session(self.request)
21
22    def get_template_context(self) -> dict:
23        """Add session to template context."""
24        context = super().get_template_context()  # ty: ignore[unresolved-attribute]
25        context["session"] = self.session
26        return context