示例#1
0
def search_results_page():
    """
    Search Results Page
    Displays a list of courses which match the search results
    """
    query = request.args.get("q", "")
    if query.strip() == "":
        return redirect(url_for("index"))

    # Check if there is a course/subject with the exact name
    subject_match = SUBJECT_RE.match(query.upper())
    if subject_match:
        subject_id = subject_match.group(1)
        try:
            # This will throw if it cannot find the subject
            search.subject(subject_id)
            return redirect(url_for("subject_page", subject_id=subject_id))
        except NotFoundError:
            pass  # The course doesn't exist

    course_match = COURSE_RE.match(query.upper())
    if course_match:
        subject_id = course_match.group(1) or course_match.group(3)
        course_id = course_match.group(2) or course_match.group(4)
        try:
            search.course(subject_id, course_id)
            return redirect(url_for("course_page", subject_id=subject_id, course_id=course_id))
        except NotFoundError:
            pass  # The course doesn't exist

    # It is a generic results page
    return results_page(query)
示例#2
0
def course_page(subject_id, course_id):
    """
    Single course detail page
    Displays information about the course, its sections, textbooks etc.
    """
    # TODO: Redirect to uppercase subject_id and course_id

    try:
        subject = search.subject(subject_id)
        course = search.course(subject_id, course_id)
        textbooks = search.textbooks(subject_id, course_id)
        sections = search.sections(subject_id, course_id)
    except NotFoundError:
        return abort(404, "There doesn't appear to be a course named {} {}".format(subject_id, course_id))

    terms = {}
    for section in sections:
        # section['_id'] TODO: Ensure this is set
        term_name = "{year} {season}".format(**section)

        # Add it to that term's list
        term = terms.get(term_name, [])
        term.append(section)
        terms[term_name] = term

    sorted_terms = sorted(terms.iteritems(), key=lambda x: util.term_ordering(x[0]))

    course_query = "subject: {} number: {}".format(subject_id, course_id)

    return render_template(
        "course.html", subject=subject, course=course, terms=sorted_terms, textbooks=textbooks, query=course_query
    )