def post(self, course_uuid, assignment_uuid): course = Course.get_active_by_uuid_or_404(course_uuid) assignment = Assignment.get_active_by_uuid_or_404(assignment_uuid) require( CREATE, ComparisonExample(assignment=Assignment(course_id=course.id)), title="Comparison Example Not Saved", message= "Sorry, your role in this course does not allow you to save practice answers." ) new_comparison_example = ComparisonExample(assignment_id=assignment.id) params = new_comparison_example_parser.parse_args() answer1_uuid = params.get("answer1_id") answer2_uuid = params.get("answer2_id") if answer1_uuid: answer1 = Answer.get_active_by_uuid_or_404(answer1_uuid) answer1.practice = True new_comparison_example.answer1 = answer1 else: abort( 400, title="Comparison Example Not Saved", message= "Please add two answers with content to the practice answers and try again." ) if answer2_uuid: answer2 = Answer.get_active_by_uuid_or_404(answer2_uuid) answer2.practice = True new_comparison_example.answer2 = answer2 else: abort( 400, title="Comparison Example Not Saved", message= "Please add two answers with content to the practice answers and try again." ) on_comparison_example_create.send( self, event_name=on_comparison_example_create.name, user=current_user, course_id=course.id, data=marshal( new_comparison_example, dataformat.get_comparison_example(with_answers=False))) db.session.add(new_comparison_example) db.session.commit() return marshal(new_comparison_example, dataformat.get_comparison_example())
def post(self, course_uuid, assignment_uuid, comparison_example_uuid): course = Course.get_active_by_uuid_or_404(course_uuid) assignment = Assignment.get_active_by_uuid_or_404(assignment_uuid) comparison_example = ComparisonExample.get_active_by_uuid_or_404(comparison_example_uuid) require(EDIT, comparison_example) params = existing_comparison_example_parser.parse_args() answer1_uuid = params.get("answer1_id") answer2_uuid = params.get("answer2_id") if answer1_uuid: answer1 = Answer.get_active_by_uuid_or_404(answer1_uuid) answer1.practice = True comparison_example.answer1 = answer1 else: return {"error": "Comparison examples must have 2 answers"}, 400 if answer2_uuid: answer2 = Answer.get_active_by_uuid_or_404(answer2_uuid) answer2.practice = True comparison_example.answer2 = answer2 else: return {"error": "Comparison examples must have 2 answers"}, 400 on_comparison_example_modified.send( self, event_name=on_comparison_example_modified.name, user=current_user, course_id=course.id, data=get_model_changes(comparison_example)) db.session.add(comparison_example) db.session.commit() return marshal(comparison_example, dataformat.get_comparison_example())
def delete(self, course_uuid, assignment_uuid, comparison_example_uuid): course = Course.get_active_by_uuid_or_404(course_uuid) assignment = Assignment.get_active_by_uuid_or_404(assignment_uuid) comparison_example = ComparisonExample.get_active_by_uuid_or_404( comparison_example_uuid) require( DELETE, comparison_example, title="Comparison Example Not Deleted", message= "Sorry, your role in this course does not allow you to delete practice answers." ) formatted_comparison_example = marshal( comparison_example, dataformat.get_comparison_example(with_answers=False)) comparison_example.active = False db.session.add(comparison_example) db.session.commit() on_comparison_example_delete.send( self, event_name=on_comparison_example_delete.name, user=current_user, course_id=course.id, data=formatted_comparison_example) return {'id': comparison_example.uuid}
def get(self, course_uuid, assignment_uuid): course = Course.get_active_by_uuid_or_404(course_uuid) assignment = Assignment.get_active_by_uuid_or_404(assignment_uuid) require( READ, ComparisonExample(course_id=course.id), title="Comparison Example Unavailable", message= "Sorry, your role in this course does not allow you to view practice answers." ) # Get all comparison examples for this assignment comparison_examples = ComparisonExample.query \ .filter_by( active=True, assignment_id=assignment.id ) \ .all() on_comparison_example_list_get.send( self, event_name=on_comparison_example_list_get.name, user=current_user, course_id=course.id, data={'assignment_id': assignment.id}) return { "objects": marshal(comparison_examples, dataformat.get_comparison_example()) }
def post(self, course_uuid, assignment_uuid, comparison_example_uuid): course = Course.get_active_by_uuid_or_404(course_uuid) assignment = Assignment.get_active_by_uuid_or_404(assignment_uuid) comparison_example = ComparisonExample.get_active_by_uuid_or_404( comparison_example_uuid) require( EDIT, comparison_example, title="Comparison Example Not Saved", message= "Sorry, your role in this course does not allow you to save practice answers." ) params = existing_comparison_example_parser.parse_args() answer1_uuid = params.get("answer1_id") answer2_uuid = params.get("answer2_id") if answer1_uuid: answer1 = Answer.get_active_by_uuid_or_404(answer1_uuid) answer1.practice = True comparison_example.answer1 = answer1 else: abort( 400, title="Comparison Example Not Saved", message= "Please add two answers with content to the practice answers and try again." ) if answer2_uuid: answer2 = Answer.get_active_by_uuid_or_404(answer2_uuid) answer2.practice = True comparison_example.answer2 = answer2 else: abort( 400, title="Comparison Example Not Saved", message= "Please add two answers with content to the practice answers and try again." ) model_changes = get_model_changes(comparison_example) db.session.add(comparison_example) db.session.commit() on_comparison_example_modified.send( self, event_name=on_comparison_example_modified.name, user=current_user, course_id=course.id, data=model_changes) return marshal(comparison_example, dataformat.get_comparison_example())
def post(self, course_uuid, assignment_uuid): course = Course.get_active_by_uuid_or_404(course_uuid) assignment = Assignment.get_active_by_uuid_or_404(assignment_uuid) require(CREATE, ComparisonExample(assignment=Assignment(course_id=course.id)), title="Comparison Example Not Saved", message="Sorry, your role in this course does not allow you to save practice answers.") new_comparison_example = ComparisonExample(assignment_id=assignment.id) params = new_comparison_example_parser.parse_args() answer1_uuid = params.get("answer1_id") answer2_uuid = params.get("answer2_id") if answer1_uuid: answer1 = Answer.get_active_by_uuid_or_404(answer1_uuid) answer1.practice = True new_comparison_example.answer1 = answer1 else: abort(400, title="Comparison Example Not Saved", message="Please add two answers with content to the practice answers and try again.") if answer2_uuid: answer2 = Answer.get_active_by_uuid_or_404(answer2_uuid) answer2.practice = True new_comparison_example.answer2 = answer2 else: abort(400, title="Comparison Example Not Saved", message="Please add two answers with content to the practice answers and try again.") on_comparison_example_create.send( self, event_name=on_comparison_example_create.name, user=current_user, course_id=course.id, data=marshal(new_comparison_example, dataformat.get_comparison_example(with_answers=False))) db.session.add(new_comparison_example) db.session.commit() return marshal(new_comparison_example, dataformat.get_comparison_example())
def post(self, course_uuid, assignment_uuid): course = Course.get_active_by_uuid_or_404(course_uuid) assignment = Assignment.get_active_by_uuid_or_404(assignment_uuid) require(CREATE, ComparisonExample(assignment=Assignment(course_id=course.id))) new_comparison_example = ComparisonExample(assignment_id=assignment.id) params = new_comparison_example_parser.parse_args() answer1_uuid = params.get("answer1_id") answer2_uuid = params.get("answer2_id") if answer1_uuid: answer1 = Answer.get_active_by_uuid_or_404(answer1_uuid) answer1.practice = True new_comparison_example.answer1 = answer1 else: return {"error": "Comparison examples must have 2 answers"}, 400 if answer2_uuid: answer2 = Answer.get_active_by_uuid_or_404(answer2_uuid) answer2.practice = True new_comparison_example.answer2 = answer2 else: return {"error": "Comparison examples must have 2 answers"}, 400 on_comparison_example_create.send( self, event_name=on_comparison_example_create.name, user=current_user, course_id=course.id, data=marshal(new_comparison_example, dataformat.get_comparison_example(with_answers=False))) db.session.add(new_comparison_example) db.session.commit() return marshal(new_comparison_example, dataformat.get_comparison_example())
def delete(self, course_uuid, assignment_uuid, comparison_example_uuid): course = Course.get_active_by_uuid_or_404(course_uuid) assignment = Assignment.get_active_by_uuid_or_404(assignment_uuid) comparison_example = ComparisonExample.get_active_by_uuid_or_404(comparison_example_uuid) require(DELETE, comparison_example) formatted_comparison_example = marshal(comparison_example, dataformat.get_comparison_example(with_answers=False)) comparison_example.active = False db.session.add(comparison_example) db.session.commit() on_comparison_example_delete.send( self, event_name=on_comparison_example_delete.name, user=current_user, course_id=course.id, data=formatted_comparison_example) return {'id': comparison_example.uuid}
def post(self, course_uuid, assignment_uuid, comparison_example_uuid): course = Course.get_active_by_uuid_or_404(course_uuid) assignment = Assignment.get_active_by_uuid_or_404(assignment_uuid) comparison_example = ComparisonExample.get_active_by_uuid_or_404(comparison_example_uuid) require(EDIT, comparison_example, title="Comparison Example Not Saved", message="Sorry, your role in this course does not allow you to save practice answers.") params = existing_comparison_example_parser.parse_args() answer1_uuid = params.get("answer1_id") answer2_uuid = params.get("answer2_id") if answer1_uuid: answer1 = Answer.get_active_by_uuid_or_404(answer1_uuid) answer1.practice = True comparison_example.answer1 = answer1 else: abort(400, title="Comparison Example Not Saved", message="Please add two answers with content to the practice answers and try again.") if answer2_uuid: answer2 = Answer.get_active_by_uuid_or_404(answer2_uuid) answer2.practice = True comparison_example.answer2 = answer2 else: abort(400, title="Comparison Example Not Saved", message="Please add two answers with content to the practice answers and try again.") model_changes = get_model_changes(comparison_example) db.session.add(comparison_example) db.session.commit() on_comparison_example_modified.send( self, event_name=on_comparison_example_modified.name, user=current_user, course_id=course.id, data=model_changes) return marshal(comparison_example, dataformat.get_comparison_example())
def delete(self, course_uuid, assignment_uuid, comparison_example_uuid): course = Course.get_active_by_uuid_or_404(course_uuid) assignment = Assignment.get_active_by_uuid_or_404(assignment_uuid) comparison_example = ComparisonExample.get_active_by_uuid_or_404(comparison_example_uuid) require(DELETE, comparison_example, title="Comparison Example Not Deleted", message="Sorry, your role in this course does not allow you to delete practice answers.") formatted_comparison_example = marshal(comparison_example, dataformat.get_comparison_example(with_answers=False)) comparison_example.active = False db.session.add(comparison_example) db.session.commit() on_comparison_example_delete.send( self, event_name=on_comparison_example_delete.name, user=current_user, course_id=course.id, data=formatted_comparison_example) return {'id': comparison_example.uuid}
def post(self, course_uuid): """ Duplicate a course """ course = Course.get_active_by_uuid_or_404(course_uuid) require( EDIT, course, title="Course Not Duplicated", message= "Sorry, your role in this course does not allow you to duplicate it." ) params = duplicate_course_parser.parse_args() start_date = datetime.datetime.strptime( params.get("start_date"), '%Y-%m-%dT%H:%M:%S.%fZ') if params.get("start_date") else None end_date = datetime.datetime.strptime( params.get("end_date"), '%Y-%m-%dT%H:%M:%S.%fZ') if params.get("end_date") else None if start_date is None: abort(400, title="Course Not Saved", message="Course start time is required.") elif start_date and end_date and start_date > end_date: abort(400, title="Course Not Saved", message="Course end time must be after course start time.") assignments = [ assignment for assignment in course.assignments if assignment.active ] assignments_copy_data = params.get("assignments") if len(assignments) != len(assignments_copy_data): abort( 400, title="Course Not Saved", message= "The course is missing assignments. Please reload the page and try duplicating again." ) for assignment_copy_data in assignments_copy_data: if assignment_copy_data.get('answer_start'): assignment_copy_data[ 'answer_start'] = datetime.datetime.strptime( assignment_copy_data.get('answer_start'), '%Y-%m-%dT%H:%M:%S.%fZ') if assignment_copy_data.get('answer_end'): assignment_copy_data[ 'answer_end'] = datetime.datetime.strptime( assignment_copy_data.get('answer_end'), '%Y-%m-%dT%H:%M:%S.%fZ') if assignment_copy_data.get('compare_start'): assignment_copy_data[ 'compare_start'] = datetime.datetime.strptime( assignment_copy_data.get('compare_start'), '%Y-%m-%dT%H:%M:%S.%fZ') if assignment_copy_data.get('compare_end'): assignment_copy_data[ 'compare_end'] = datetime.datetime.strptime( assignment_copy_data.get('compare_end'), '%Y-%m-%dT%H:%M:%S.%fZ') if 'enable_self_evaluation' not in assignment_copy_data: assignment_copy_data['enable_self_evaluation'] = False if assignment_copy_data.get('self_eval_start'): assignment_copy_data[ 'self_eval_start'] = datetime.datetime.strptime( assignment_copy_data.get('self_eval_start'), '%Y-%m-%dT%H:%M:%S.%fZ') if assignment_copy_data.get('self_eval_end'): assignment_copy_data[ 'self_eval_end'] = datetime.datetime.strptime( assignment_copy_data.get('self_eval_end'), '%Y-%m-%dT%H:%M:%S.%fZ') valid, error_message = Assignment.validate_periods( start_date, end_date, assignment_copy_data.get('answer_start'), assignment_copy_data.get('answer_end'), assignment_copy_data.get('compare_start'), assignment_copy_data.get('compare_end'), assignment_copy_data.get('self_eval_start'), assignment_copy_data.get('self_eval_end')) if not valid: error_message = error_message.replace( ".", "") + " for assignment " + text_type( assignment_copy_data.get('name', '')) + "." abort(400, title="Course Not Saved", message=error_message) # duplicate course duplicate_course = Course(name=params.get("name"), year=params.get("year"), term=params.get("term"), sandbox=params.get("sandbox"), start_date=start_date, end_date=end_date) db.session.add(duplicate_course) # also need to enrol the user as an instructor new_user_course = UserCourse(course=duplicate_course, user_id=current_user.id, course_role=CourseRole.instructor) db.session.add(new_user_course) # duplicate assignments for assignment in assignments: # this should never be null due assignment_copy_data = next( (assignment_copy_data for assignment_copy_data in assignments_copy_data if assignment_copy_data.get('id') == assignment.uuid), None) if not assignment_copy_data: abort(400, title="Course Not Saved", message="Missing information for assignment " + assignment.name + ". Please try duplicating again.") duplicate_assignment = Assignment( course=duplicate_course, user_id=current_user.id, file=assignment.file, name=assignment.name, description=assignment.description, answer_start=assignment_copy_data.get('answer_start'), answer_end=assignment_copy_data.get('answer_end'), compare_start=assignment_copy_data.get('compare_start'), compare_end=assignment_copy_data.get('compare_end'), self_eval_start=assignment_copy_data.get('self_eval_start') if assignment_copy_data.get('enable_self_evaluation', False) else None, self_eval_end=assignment_copy_data.get('self_eval_end') if assignment_copy_data.get('enable_self_evaluation', False) else None, self_eval_instructions=assignment.self_eval_instructions if assignment_copy_data.get('enable_self_evaluation', False) else None, answer_grade_weight=assignment.answer_grade_weight, comparison_grade_weight=assignment.comparison_grade_weight, self_evaluation_grade_weight=assignment. self_evaluation_grade_weight, number_of_comparisons=assignment.number_of_comparisons, students_can_reply=assignment.students_can_reply, enable_self_evaluation=assignment_copy_data.get( 'enable_self_evaluation', False), enable_group_answers=assignment.enable_group_answers, pairing_algorithm=assignment.pairing_algorithm, scoring_algorithm=assignment.scoring_algorithm, peer_feedback_prompt=assignment.peer_feedback_prompt, educators_can_compare=assignment.educators_can_compare, rank_display_limit=assignment.rank_display_limit, ) db.session.add(duplicate_assignment) # duplicate assignment criteria for assignment_criterion in assignment.assignment_criteria: if not assignment_criterion.active: continue duplicate_assignment_criterion = AssignmentCriterion( assignment=duplicate_assignment, criterion_id=assignment_criterion.criterion_id) db.session.add(duplicate_assignment_criterion) # duplicate assignment comparisons examples for comparison_example in assignment.comparison_examples: answer1 = comparison_example.answer1 answer2 = comparison_example.answer2 # duplicate assignment comparisons example answers duplicate_answer1 = Answer(assignment=duplicate_assignment, user_id=current_user.id, file=answer1.file, content=answer1.content, practice=answer1.practice, active=answer1.active, draft=answer1.draft) db.session.add(duplicate_answer1) # duplicate assignment comparisons example answers duplicate_answer2 = Answer(assignment=duplicate_assignment, user_id=current_user.id, file=answer2.file, content=answer2.content, practice=answer2.practice, active=answer2.active, draft=answer2.draft) db.session.add(duplicate_answer2) duplicate_comparison_example = ComparisonExample( assignment=duplicate_assignment, answer1=duplicate_answer1, answer2=duplicate_answer2) db.session.add(duplicate_comparison_example) db.session.commit() on_course_duplicate.send(self, event_name=on_course_duplicate.name, user=current_user, course=duplicate_course, data=marshal(course, dataformat.get_course())) return marshal(duplicate_course, dataformat.get_course())