def test_question_get_notes(auth_client, paper_with_course_and_questions, session): paper = paper_with_course_and_questions course = paper.course question = paper.questions[0] note_link = NoteLink(link="http://foo.com", question=question) note_upload = NoteUpload(file_path="/foo/bar", question=question) session.add_all([note_link, note_upload]) session.commit() resp = auth_client.get( "/course/{code}/paper/{year}/{period}/q/{question}/notes".format( code=paper.course.code.lower(), year=paper.year_start, period=paper.period.lower(), question=".".join(map(str, question.path)))) with assert_api_response(resp) as data: assert "question" in data assert "notes" in data notes = data["notes"] assert len(notes) == 2 assert find(notes, lambda n: n["type"] == "note_link") assert find(notes, lambda n: n["type"] == "note_upload")
def test_question_create_as_child(auth_client, session, paper_with_course_and_questions): paper = paper_with_course_and_questions course = paper.course question = paper.questions[0] resp = auth_client.post("/course/{code}/paper/{year}/{period}/q/{question}".format( code=paper.course.code.lower(), year=paper.year_start, period=paper.period.lower(), question=".".join(map(str, question.path)) ), data={ "index": 4, "index_type": "alpha", "content": "Hello world" }) with assert_api_response(resp) as data: assert "question" in data question_data = data["question"] assert "id" in question_data assert "revision" in question_data assert "parent" in question_data revision_data = question_data["revision"] assert revision_data["content"] == "Hello world" session.refresh(question) assert find(question.children, lambda q: q.id == question_data["id"])
def test_question_create_as_child(auth_client, session, paper_with_course_and_questions): paper = paper_with_course_and_questions course = paper.course question = paper.questions[0] resp = auth_client.post( "/course/{code}/paper/{year}/{period}/q/{question}".format( code=paper.course.code.lower(), year=paper.year_start, period=paper.period.lower(), question=".".join(map(str, question.path))), data={ "index": 4, "index_type": "alpha", "content": "Hello world" }) with assert_api_response(resp) as data: assert "question" in data question_data = data["question"] assert "id" in question_data assert "revision" in question_data assert "parent" in question_data revision_data = question_data["revision"] assert revision_data["content"] == "Hello world" session.refresh(question) assert find(question.children, lambda q: q.id == question_data["id"])
def test_profile_delete_course(auth_client, user_with_courses, session): existingCourse = user_with_courses.courses[0] resp = auth_client.delete("/profile/courses", data={ "course": existingCourse.id }) assert_api_success(resp) session.refresh(user_with_courses) assert len(user_with_courses.courses) == 4 assert not find(user_with_courses.courses, lambda mod: mod.id == existingCourse.id)
def test_profile_delete_course(auth_client, user_with_courses, session): existingCourse = user_with_courses.courses[0] resp = auth_client.delete("/profile/courses", data={"course": existingCourse.id}) assert_api_success(resp) session.refresh(user_with_courses) assert len(user_with_courses.courses) == 4 assert not find(user_with_courses.courses, lambda mod: mod.id == existingCourse.id)
def test_question_get_notes(auth_client, paper_with_course_and_questions, session): paper = paper_with_course_and_questions course = paper.course question = paper.questions[0] note_link = NoteLink(link="http://foo.com", question=question) note_upload = NoteUpload(file_path="/foo/bar", question=question) session.add_all([note_link, note_upload]) session.commit() resp = auth_client.get("/course/{code}/paper/{year}/{period}/q/{question}/notes".format( code=paper.course.code.lower(), year=paper.year_start, period=paper.period.lower(), question=".".join(map(str, question.path)) )) with assert_api_response(resp) as data: assert "question" in data assert "notes" in data notes = data["notes"] assert len(notes) == 2 assert find(notes, lambda n: n["type"] == "note_link") assert find(notes, lambda n: n["type"] == "note_upload")
def popular_questions(self): """Find the most popular questions. This loops through all the questions, find's the similar questions and ranks them by sum(similarity) """ session = object_session(self) Similar = get_model("Similar") Question = get_model("Question") Paper = get_model("Paper") # SQL: # exam_papers=# select question_id, sum(similarity) as similarity from similar_questions # where similarity > 0.6 and question_id != similar_question_id # group by question_id order by similarity DESC; popular = (session.query( Similar.question_id.label("question_id"), func.sum(Similar.similarity).label("cum_similarity")).group_by( Similar.question_id)).subquery() questions = session.query(Question)\ .join(popular, Question.id == popular.c.question_id)\ .join(Paper, Paper.id == Question.paper_id)\ .filter(Paper.course_id == self.id)\ .order_by(popular.c.cum_similarity.desc())\ .limit(25)\ .all() # Now we pick only one of a similar group of questions # A graph DB would be handy right about now grouped = [] for question in questions: inside = False # Loop over each similar questions in the already selected questions for grouped_question in grouped: inside = bool( find(grouped_question.similar, lambda q: q.similar_question_id == question.id)) if not inside or len(grouped) == 0: grouped.append(question) return grouped
def popular_questions(self): """Find the most popular questions. This loops through all the questions, find's the similar questions and ranks them by sum(similarity) """ session = object_session(self) Similar = get_model("Similar") Question = get_model("Question") Paper = get_model("Paper") # SQL: # exam_papers=# select question_id, sum(similarity) as similarity from similar_questions # where similarity > 0.6 and question_id != similar_question_id # group by question_id order by similarity DESC; popular = (session.query( Similar.question_id.label("question_id"), func.sum(Similar.similarity).label("cum_similarity") ).group_by(Similar.question_id)).subquery() questions = session.query(Question)\ .join(popular, Question.id == popular.c.question_id)\ .join(Paper, Paper.id == Question.paper_id)\ .filter(Paper.course_id == self.id)\ .order_by(popular.c.cum_similarity.desc())\ .limit(25)\ .all() # Now we pick only one of a similar group of questions # A graph DB would be handy right about now grouped = [] for question in questions: inside = False # Loop over each similar questions in the already selected questions for grouped_question in grouped: inside = bool(find(grouped_question.similar, lambda q: q.similar_question_id == question.id)) if not inside or len(grouped) == 0: grouped.append(question) return grouped
def test_question_delete_leaf(auth_client, paper_with_course_and_questions, session): paper = paper_with_course_and_questions question = find(paper.questions, lambda q: not len(q.children)) resp = auth_client.delete("/course/{code}/paper/{year}/{period}/q/{question}".format( code=paper.course.code.lower(), year=paper.year_start, period=paper.period.lower(), question=".".join(map(str, question.path)) )) with assert_api_response(resp) as data: assert "questions" in data try: question = session.query(Question)\ .filter(Question.id == question.id)\ .one() assert not question except NoResultFound: pass
def get_and_update_courses(): if request.method == "GET": # Return the user's courses return respond({ "courses": g.user.courses }) else: args = parser.parse(patchParams, request) id = args["course"] try: course = model.Course.getBy(db.session, id=id) except NotFound: return UnacceptableParameter("Course with id '%d' does not exist." % id) # Find the course userCourse = find(g.user.courses, lambda c: c.id == course.id) if request.method == "PATCH": # Patch request i.e. append to the collection # Ensure it's not already in the users courses if userCourse: return success() # Ignorance is bliss # Add the course g.user.courses.append(course) else: # Delete the course from the collection # Ensure we have the course if not userCourse: return success() # Remove the course g.user.courses.remove(userCourse) db.session.add(g.user) db.session.commit() return success()
def test_question_delete_leaf(auth_client, paper_with_course_and_questions, session): paper = paper_with_course_and_questions question = find(paper.questions, lambda q: not len(q.children)) resp = auth_client.delete( "/course/{code}/paper/{year}/{period}/q/{question}".format( code=paper.course.code.lower(), year=paper.year_start, period=paper.period.lower(), question=".".join(map(str, question.path)))) with assert_api_response(resp) as data: assert "questions" in data try: question = session.query(Question)\ .filter(Question.id == question.id)\ .one() assert not question except NoResultFound: pass
def active_session(self): return find(self.sessions, lambda session: session.active)