def school(request, school_id=None, page=0):
    """
    This is a school page. If no id is provided, it displays a listing of
    all schoosl with information about each. If one is provided, it provides
    an overview about that school.

    If a page is provided, it skips forward that page * items per page.
    """
    template = loader.get_template("browse/school.html")
    context = RequestContext(request)

    context["school"] = get_object_or_404(School, id=school_id)

    context["school_location"] = context["school"].human_location

    # FIXME: Paginate
    context["review_votes"] = \
        _get_all_review_votes(request, {"target__school_id": school_id})[0:5]

    context["courses"] = Course.objects.filter(
        department__school__id=school_id)

    context["professors"] = Professor.objects.filter(school_id=school_id)

    return HttpResponse(template.render(context))
def index(request, message=""):
    template = "browse/index.html"

    context = RequestContext(request)

    context["review_votes"] = _get_all_review_votes(request)[0:5]

    return render(request, template, context)
def professor(request, professor_id=None, page=0):
    """
    This is a professor profile page (not a user). It provides information
    about courses taught, latest reviews, and aggregate ratings. If an id is
    not specified, it provides a listing of all professors (paginated).

    If a page is provided, it skips forward that page * items per page.
    """
    template = loader.get_template("browse/professor.html")
    context = RequestContext(request)

    context["professor"] = get_object_or_404(Professor, id=professor_id)

    # FIXME: Paginate
    context["review_votes"] = _get_all_review_votes(request,
                                                    {"target": professor_id})
    context["courses"] = []
    courses = (Review.objects.filter(target_id=professor_id).values("course")
               .distinct())

    for course in courses:
        context["courses"].append(Course.objects.get(id=course["course"]))

    context["schools"] = [course.department.school for course
                          in context["courses"]]

    reviewdata = (Review.objects.filter(target_id=professor_id))
    profdata = DataPool(series=[{'options': {'source': reviewdata}, 'terms':
                                 ['id', 'rating_value', 'rating_difficulty',
                                  'rating_overall']}])

    chart = Chart(datasource=profdata, series_options=[{'options':
                                                        {'type': 'line',
                                                         'stacking': False},
                                                        'terms':
                                                        {'id':
                                                         ['rating_value',
                                                          'rating_difficulty',
                                                          'rating_overall']}}],
                  chart_options={'title': {'text': 'Ratings of Professor'},
                                 'xAxis': {'title': {'text': 'Recent Reviews'},
                                           'ceiling': 25},
                                 'yAxis': {'title': {'text': 'Ratings'},
                                           'min': 0, 'max': 5}})

    context["chart"] = chart

    return HttpResponse(template.render(context))
def reviews(request, type="all", first_id=None, second_id=None, page=1):
    """
    This is the general-purpose review-viewing page. It allows for returning
    views of specific requests from the user.

    :Parameters:
        * *page*: (``int``) --
            The page to start listing from.
        * *type*: (``str``) --
            The type of review view page to get. Valid types are:
                * all (default)
                * by_school
                * by_professor
                * by_school_professor
        * *first_id*: (``int``) --
            The first id of the requested view type (ie professor).
        * *second_id*: (``int``) --
            The second id of the requested view type (ie school).
    """
    template = "browse/reviews.html"
    context = RequestContext(request)
    context["pages"], context["page"], all, start, end = paginate(page, Review)
    context["review_votes"] = _get_all_review_votes(request)[start:end]
    context["schoolForm"] = SchoolForm()

    if type == "by_school":
        context["message"] =\
            "This is the page that lists all reviews for school {1}"\
            "(pg {0}).".format(page, first_id)
    elif type == "by_professor":
        context["message"] =\
            "This is the page that lists all reviews for professor"\
            " {1} (pg {0}).".format(page, first_id)

    elif type == "by_school_professor":
        context["message"] =\
            "This is the page for reviews of professor {1} from "\
            "school {0} (pg {2})."\
            .format(first_id, second_id, page)
    else:
        context["message"] =\
            "This is the page that lists all reviews (pg {0})."\
            .format(page)
    return render(request, template, context)
def profile(request, id=None, page=0):
    """
    This is our profile page. It's passed, optionally, a user id as "id" in
    kwargs. If it gets an id, it spits out a profile. If it doesn't, it spits
    out a listing of all users with links to profiles.

    If a page is provided, it skips forward that page * items per page.
    """
    template = "browse/profile.html"
    context = RequestContext(request)

    # Specific profile was requested
    if id is not None:
        # Could use 404 shortcut, or a simple redirect for invalid user id's
        context["profile"] = get_object_or_404(User, id=id)
    # User logged in and should see own profile
    elif request.user.is_authenticated():
        context["profile"] = request.user
    # No Profile specified and user is not logged in
    else:
        # One option is redirect as such.
        messages.info(request, "Please login to view your profile.")
        return redirect("home")

    context["review_votes"] = _get_all_review_votes(
        request,
        {"owner": context["profile"].id})

    voteCount = 0
    for review, v in context["review_votes"]:
        votes = review.reviewvote_set.all()
        voteCount += sum([1 if vote.quality else -1 for vote in votes])

    context["user_rating"] = voteCount

    return render(request, template, context)