from grandchallenge.workstations.models import Session


def workstation_session(request):
    """ Adds workstation_session. request.user must be set """

    s = None

    if not request.user.is_anonymous:
        s = (
            Session.objects.filter(creator=request.user)
            .exclude(status__in=[Session.QUEUED, Session.STOPPED])
            .order_by("-created")
            .select_related("workstation_image__workstation")
            .first()
        )

    return {"workstation_session": s}

from grandchallenge.workstations.models import Session


def workstation_session(request):
    """ Adds workstation_session. request.user must be set """

    s = None

    try:
        if not request.user.is_anonymous:
            s = (
                Session.objects.filter(creator=request.user)
                .exclude(status__in=[Session.QUEUED, Session.STOPPED])
                .order_by("-created")
                .select_related("workstation_image__workstation")
                .first()
            )
    except AttributeError:
        # No user
        pass

    return {"workstation_session": s}

Handle no user at all
Handle no user at all

  from grandchallenge.workstations.models import Session
  
  
  def workstation_session(request):
      """ Adds workstation_session. request.user must be set """
  
      s = None
  
+     try:
-     if not request.user.is_anonymous:
+         if not request.user.is_anonymous:
-         s = (
+             s = (
-             Session.objects.filter(creator=request.user)
+                 Session.objects.filter(creator=request.user)
-             .exclude(status__in=[Session.QUEUED, Session.STOPPED])
+                 .exclude(status__in=[Session.QUEUED, Session.STOPPED])
-             .order_by("-created")
+                 .order_by("-created")
-             .select_related("workstation_image__workstation")
+                 .select_related("workstation_image__workstation")
-             .first()
+                 .first()
-         )
+             )
+     except AttributeError:
+         # No user
+         pass
  
      return {"workstation_session": s}
  
Handle no user at all
## Code Before:
from grandchallenge.workstations.models import Session


def workstation_session(request):
    """ Adds workstation_session. request.user must be set """

    s = None

    if not request.user.is_anonymous:
        s = (
            Session.objects.filter(creator=request.user)
            .exclude(status__in=[Session.QUEUED, Session.STOPPED])
            .order_by("-created")
            .select_related("workstation_image__workstation")
            .first()
        )

    return {"workstation_session": s}

## Instruction:
Handle no user at all
## Code After:
from grandchallenge.workstations.models import Session


def workstation_session(request):
    """ Adds workstation_session. request.user must be set """

    s = None

    try:
        if not request.user.is_anonymous:
            s = (
                Session.objects.filter(creator=request.user)
                .exclude(status__in=[Session.QUEUED, Session.STOPPED])
                .order_by("-created")
                .select_related("workstation_image__workstation")
                .first()
            )
    except AttributeError:
        # No user
        pass

    return {"workstation_session": s}
