Displays a table of the workshifts for the week, shift assignments,
    accumulated statistics (Down hours), reminders for any upcoming shifts, and
    links to sign off on shifts. Also links to the rest of the workshift pages.

def semester_view(request, semester, profile=None):
    """
    Displays a table of the workshifts for the week, shift assignments,
    accumulated statistics (Down hours), reminders for any upcoming shifts, and
    links to sign off on shifts. Also links to the rest of the workshift pages.
    """
    template_dict = {}
    template_dict["page_name"] = "Workshift for {} {}".format(
        semester.get_season_display(),
        semester.year,
    )

    today = localtime(now()).date()
    template_dict["semester_percentage"] = int(
        (today - semester.start_date).days /
        (semester.end_date - semester.start_date).days * 100
    )

    # We want a form for verification, a notification of upcoming shifts, and a
    # chart displaying the entire house's workshift for the day as well as
    # weekly shifts.
    #
    # The chart should have left and right arrows on the sides to switch the
    # day, with a dropdown menu to select the day from a calendar. Ideally,
    # switching days should use AJAX to appear more seemless to users.

    # Recent History
    day = _get_date(request, "day", today)
    start_date = _get_date(request, "start_date", day)
    end_date = _get_date(request, "end_date", day)

    template_dict["day"] = day
    template_dict["start_date"] = start_date
    template_dict["end_date"] = end_date
    template_dict["long_range"] = (end_date - start_date).days > 7

    if day > semester.start_date:
        template_dict["prev_day"] = (start_date - timedelta(days=1)).strftime("%Y-%m-%d")
    if day < semester.end_date:
        template_dict["next_day"] = (end_date + timedelta(days=1)).strftime("%Y-%m-%d")

    if Semester.objects.count() > 1:
        switch_form = SwitchSemesterForm(
            data=request.POST if "switch_semester" in request.POST else None
        )
        template_dict["switch_form"] = switch_form

        if switch_form.is_valid():
            semester = switch_form.save()
            return HttpResponseRedirect(semester.get_view_url())

    # Grab the shifts for just today, as well as week-long shifts
    day_shifts = WorkshiftInstance.objects.filter(
        date__gte=start_date,
        date__lte=end_date,
    ).exclude(
        Q(weekly_workshift__week_long=True) |
        Q(info__week_long=True),
    )

    last_monday = start_date - timedelta(days=start_date.weekday())
    next_sunday = end_date - timedelta(days=end_date.weekday() + 1) + timedelta(weeks=1)

    week_shifts = WorkshiftInstance.objects.filter(
        Q(weekly_workshift__week_long=True) |
        Q(info__week_long=True),
        date__gte=last_monday, date__lte=next_sunday,
    )

    template_dict["last_monday"] = last_monday.strftime("%Y-%m-%d")
    template_dict["next_sunday"] = next_sunday.strftime("%Y-%m-%d")

    # Add a login form for anonymous user
    if profile is None and request.user.username == ANONYMOUS_USERNAME:
        anonymous_form = AnonymousUserLogin(
            data=request.POST or None,
            prefix="anon",
            semester=semester,
        )

        if anonymous_form.is_valid():
            profile = anonymous_form.save()

        template_dict["anonymous_form"] = anonymous_form

    template_dict["day_shifts"] = [
        (shift, _get_forms(
            profile, shift, request,
            undo=utils.can_manage(request.user, semester=semester, pool=shift.pool),
        ))
        for shift in day_shifts
    ]
    template_dict["week_shifts"] = [
        (shift, _get_forms(
            profile, shift, request,
            undo=utils.can_manage(request.user, semester=semester, pool=shift.pool),
        ))
        for shift in week_shifts
    ]

    # Save any forms that were submitted
    all_forms = [
        form
        for instance, forms in template_dict["day_shifts"]
        for form in forms
    ] + [
        form
        for instance, forms in template_dict["week_shifts"]
        for form in forms
    ]
    for form in all_forms:
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(
                semester.get_view_url() + _get_date_params(request)
            )
        else:
            for error in form.errors.values():
                messages.add_message(request, messages.ERROR, error)

    return render_to_response(
        "semester.html",
        template_dict,
        context_instance=RequestContext(request),
    )