Source code for ferrosoft.apps.emiflow.services.session

"""Per-user-session storage of the *current transport chain*.

When the user is mid-way through editing a transport chain across
multiple views, the chain's primary key is stashed in the Django session
so subsequent views can resume without it being passed through every
URL. All helpers degrade silently when the request has no session (e.g.
during management commands or unit tests).
"""
from uuid import UUID

from django.http import HttpRequest

from ferrosoft.apps.emiflow.models import TransportChain

_current_transport_key = "emiflow_current_transport"


[docs] def set_current_transport(request: HttpRequest, transport: TransportChain): """Remember ``transport`` as the user's currently-edited chain.""" if hasattr(request, "session"): request.session[_current_transport_key] = str(transport.pk)
[docs] def get_current_transport(request: HttpRequest) -> UUID | None: """Return the primary key of the current chain, or ``None`` when unset.""" if hasattr(request, "session"): id_raw = request.session.get(_current_transport_key, None) return None if id_raw is None else UUID(id_raw) return None
[docs] def clear_session(request: HttpRequest): """Forget the current chain. No-op when nothing is stored.""" if hasattr(request, "session") and request.session.exists(_current_transport_key): request.session.delete(_current_transport_key)