Add workshift variables to all dictionaries passed to templates.

def add_workshift_context(request):
    """ Add workshift variables to all dictionaries passed to templates. """
    if not request.user.is_authenticated():
        return {}

    if Semester.objects.count() < 1:
        return {
            "WORKSHIFT_ENABLED": False,
        }

    # Current semester is for navbar notifications
    try:
        current_semester = Semester.objects.get(current=True)
    except Semester.DoesNotExist:
        current_semester = None
    except Semester.MultipleObjectsReturned:
        current_semester = Semester.objects.filter(
            current=True,
        ).latest("start_date")
        workshift_emails = []
        for pos in Manager.objects.filter(workshift_manager=True, active=True):
            if pos.email:
                workshift_emails.append(pos.email)
            elif pos.incumbent.email_visible and pos.incumbent.user.email:
                workshift_emails.append(pos.incumbent.user.email)
        if workshift_emails:
            workshift_email_str = " ({})".format(
                ", ".join(
                    "<a href=\"mailto:{0}\">{0}</a>".format(i)
                    for i in workshift_emails
                ),
            )
        else:
            workshift_email_str = ""
        messages.add_message(
            request,
            messages.WARNING,
            MESSAGES["MULTIPLE_CURRENT_SEMESTERS"].format(
                admin_email=settings.ADMINS[0][1],
                workshift_emails=workshift_email_str,
            ),
        )

    today = localtime(now()).date()

    days_passed = None
    total_days = None
    semester_percentage = None
    standing = None
    happening_now = None
    workshift_profile = None

    if current_semester:
        # number of days passed in this semester
        days_passed = (today - current_semester.start_date).days

        # total number of days in this semester
        total_days = (
            current_semester.end_date - current_semester.start_date
        ).days
        semester_percentage = round((days_passed / total_days) * 100, 2)

    # Semester is for populating the current page
    try:
        semester = request.semester
    except AttributeError:
        semester = current_semester

    try:
        workshift_profile = WorkshiftProfile.objects.get(
            semester=semester,
            user=request.user,
            )
    except WorkshiftProfile.DoesNotExist:
        workshift_profile = None

    workshift_manager = utils.can_manage(request.user, semester=semester)

    upcoming_shifts = WorkshiftInstance.objects.filter(
        workshifter=workshift_profile,
        closed=False,
        date__gte=today,
        date__lte=today + timedelta(days=2),
    )

    # TODO: Add a fudge factor of an hour to this?
    time = localtime(now()).time()
    happening_now = []
    for shift in upcoming_shifts:
        if shift.week_long:
            happening_now.append(shift)
            continue
        if shift.date != today:
            continue
        if shift.start_time is None:
            if shift.end_time is not None:
                if time < shift.end_time:
                    happening_now.append(shift)
            else:
                happening_now.append(shift)
            continue
        if shift.end_time is None:
            if shift.start_time is not None:
                if time > shift.start_time:
                    happening_now.append(shift)
            else:
                happening_now.append(shift)
            continue
        if time > shift.start_time and time < shift.end_time:
            happening_now.append(shift)

    if workshift_profile:
        try:
            standing = workshift_profile.pool_hours.get(
                pool__is_primary=True,
            ).standing
        except (PoolHours.DoesNotExist, PoolHours.MultipleObjectsReturned):
            pass

    return {
        "WORKSHIFT_ENABLED": True,
        "SEMESTER": semester,
        "CURRENT_SEMESTER": current_semester,
        "WORKSHIFT_MANAGER": workshift_manager,
        "WORKSHIFT_PROFILE": workshift_profile,
        "STANDING": standing,
        "DAYS_PASSED": days_passed,
        "TOTAL_DAYS": total_days,
        "SEMESTER_PERCENTAGE": semester_percentage,
        "UPCOMING_SHIFTS": zip(upcoming_shifts, happening_now),
    }