Example #1
0
def index_course_questions(course):
    course = model.Course.getBy(db.session, code=course.upper())
    indexed_questions = course.index_questions()
    db.session.add_all(indexed_questions)
    db.session.commit()
    invalidate_view("course.get_popular", course=course.code)
    return success()
Example #2
0
def index_course_questions(course):
    course = model.Course.getBy(db.session, code=course.upper())
    indexed_questions = course.index_questions()
    db.session.add_all(indexed_questions)
    db.session.commit()
    invalidate_view("course.get_popular", course=course.code)
    return success()
Example #3
0
def create_comment(entity, content, parent):
    with query(model.Entity):
        entity = db.session.query(model.Entity).filter(model.Entity.id == entity).one()

        if parent != missing:
            parent = db.session.query(model.Comment).filter(model.Comment.id == parent).one()
        else:
            parent = None

    comment = g.user.comment(entity, content, parent)
    db.session.add(comment)
    db.session.commit()
    db.session.refresh(comment) # Conversion from Entity type to Comment

    # Now we invalidate some cache so they pick up the new comments
    if entity.type == "question":
        # Empty the course popular questions
        invalidate_view("course.get_popular")

        db.session.refresh(entity)

        question = entity
        paper = entity.paper
        course = paper.course

        # Invalidate the question's paper
        invalidate_view("paper.get_paper", course=course.code, year=paper.year_start, period=paper.period)
        invalidate_view("question.do_question", course=course.code, year=paper.year_start, period=paper.period, question=".".join(map(str, question.path)))

    # Invalidate the comment cache for specific entity
    invalidate_view("comment.get_comments", entity=entity.id)

    return respond({ "comment": comment })
Example #4
0
def edit_comment(entity, comment):
    with query(model.Entity):
        entity = db.session.query(
            model.Entity).filter(model.Entity.id == entity).one()
        comment = db.session.query(
            model.Comment).filter(model.Comment.id == comment).one()

    # Ensure the user is the author
    if not g.user.id == comment.user.id:
        raise Unauthorized()

    if request.method == "DELETE":
        comment.content = ""
        comment.deleted = True
    elif request.method == "PUT":
        args = parser.parse({"content": fields.Str(required=True)})
        comment.content = args["content"]

    # Fix up the cache
    if entity.type == "question":
        db.session.refresh(entity)

        question = entity
        paper = entity.paper
        course = paper.course

        invalidate_view("question.do_question",
                        course=course.code,
                        year=paper.year_start,
                        period=paper.period,
                        question=".".join(map(str, question.path)))

    # Invalidate the comment cache for specific entity
    invalidate_view("comment.get_comments", entity=entity.id)

    db.session.add(comment)
    db.session.commit()
    db.session.refresh(comment)
    getattr(comment, "children")

    return respond({"entity": entity, "comment": comment})
Example #5
0
def edit_comment(entity, comment):
    with query(model.Entity):
        entity = db.session.query(model.Entity).filter(model.Entity.id == entity).one()
        comment = db.session.query(model.Comment).filter(model.Comment.id == comment).one()

    # Ensure the user is the author
    if not g.user.id == comment.user.id:
        raise Unauthorized()

    if request.method == "DELETE":
        comment.content = ""
        comment.deleted = True
    elif request.method == "PUT":
        args = parser.parse({ "content": fields.Str(required=True) })
        comment.content = args["content"]

    # Fix up the cache    
    if entity.type == "question":
        db.session.refresh(entity)

        question = entity
        paper = entity.paper
        course = paper.course

        invalidate_view("question.do_question", course=course.code, year=paper.year_start, period=paper.period, question=".".join(map(str, question.path)))

    # Invalidate the comment cache for specific entity
    invalidate_view("comment.get_comments", entity=entity.id)
    
    db.session.add(comment)
    db.session.commit()
    db.session.refresh(comment)
    getattr(comment, "children")

    return respond({ 
        "entity": entity,
        "comment": comment 
    })
Example #6
0
def create_comment(entity, content, parent):
    with query(model.Entity):
        entity = db.session.query(
            model.Entity).filter(model.Entity.id == entity).one()

        if parent != missing:
            parent = db.session.query(
                model.Comment).filter(model.Comment.id == parent).one()
        else:
            parent = None

    comment = g.user.comment(entity, content, parent)
    db.session.add(comment)
    db.session.commit()
    db.session.refresh(comment)  # Conversion from Entity type to Comment

    # Now we invalidate some cache so they pick up the new comments
    if entity.type == "question":
        # Empty the course popular questions
        invalidate_view("course.get_popular")

        db.session.refresh(entity)

        question = entity
        paper = entity.paper
        course = paper.course

        # Invalidate the question's paper
        invalidate_view("paper.get_paper",
                        course=course.code,
                        year=paper.year_start,
                        period=paper.period)
        invalidate_view("question.do_question",
                        course=course.code,
                        year=paper.year_start,
                        period=paper.period,
                        question=".".join(map(str, question.path)))

    # Invalidate the comment cache for specific entity
    invalidate_view("comment.get_comments", entity=entity.id)

    return respond({"comment": comment})
Example #7
0
def do_question(course, year, period, question):
    # Ensure we parse the args before we do any database access
    if request.method == "PUT":
        args = parser.parse(PUT_PARAMS, request)
    elif request.method == "POST":
        args = parser.parse(POST_PARAMS, request)
    else:
        args = None

    question_path = question
    question = model.Question.get_by_path(db.session, course, year, period, map(int, question.split(".")))

    if request.method == "GET":
        return respond({
            "question": question,
            "children": question.flatten_tree(include_self=False)
        })
    else:
        if request.method == "PUT":
            updated = []

            # Update a question
            if "content" in args:
                question.set_content(g.user, args["content"])

            if "marks" in args:
                question.marks = args["marks"]

            if "index_type" in args:
                question.update_index_type(args["index_type"])

                # Update the indexes of all siblings
                if question.parent:
                    for sibling in question.parent.children:
                        sibling.update_index_type(args["index_type"])
                        updated.append(sibling)

            if "is_section" in args:
                question.is_section = args["is_section"]

            updated.append(question)
            db.session.add_all(updated)
            db.session.commit()
            map(db.session.refresh, updated)

            response = { "questions": updated }
        elif request.method == "POST":
            # Create a child question    
            paper = model.Paper.find(db.session, course, year, period)
            new_question = model.Question(paper, index=args["index"], parent=question)

            if "content" in args:
                new_question.set_content(g.user, args["content"])

            db.session.add(new_question)

            with query(model.Question):
                db.session.commit()

            db.session.refresh(new_question)
            getattr(new_question, "paper")
            getattr(new_question, "parent")

            response = { "question": new_question }
        elif request.method == "DELETE":
            if len(question.children):
                raise Forbidden("Question still has children. Please remove children before removing.")

            modified = []
            db.session.delete(question)

            # Decrement any indexes further up the list
            if question.parent:
                db.session.flush()
                for sibling in question.parent.children:
                    if sibling.index > question.index:
                        sibling.update_index(sibling.index - 1)
                        modified.append(sibling)

                db.session.add_all(modified)
            
            db.session.commit()
            map(db.session.refresh, modified)

            response = { "questions": modified }

        # Invalidate the cache
        invalidate_view("question.do_question", course=course, year=year, period=period, question=question_path)
        invalidate_view("question.get_similar", course=course, year=year, period=period, question=question_path)
        invalidate_view("paper.get_paper", course=course, year=year, period=period)
        invalidate_view("course.get_popular", course=course)


        return respond(response)