diff --git a/app.py b/app.py index 2f4fc99..d6fbade 100644 --- a/app.py +++ b/app.py @@ -46,6 +46,7 @@ ) logger.addHandler(sh) logger.setLevel(logging.INFO) +logger.info('Backend READY') ok_server = app.config['OK_SERVER'] @@ -388,6 +389,12 @@ def delete_lecture(course_ok_id, lecture_url_name, ok_id=None): 'lecture_url_name': lecture_url_name } ) + db[Resource.collection].remove( + { + 'course_ok_id': course_ok_id, + 'lecture_url_name': lecture_url_name + } + ) db_obj = db[Lecture.collection].find( { "course_ok_id": course_ok_id @@ -395,12 +402,12 @@ def delete_lecture(course_ok_id, lecture_url_name, ok_id=None): ).sort("date", 1) if request.args["piazza_active"] == "active": Piazza.delete_post( - piazza_course_id=request.form["piazza_course_id"], - cid=request.form["post_id"]) + piazza_course_id=request.args["piazza_course_id"], + cid=request.args["lecture_piazza_id"]) Piazza.recreate_master_post( - request.form["piazza_master_post_id"], - piazza_course_id=request.form[ - "piazza_course_id"], db=db) + request.args["piazza_master_post_id"], + piazza_course_id=request.args["piazza_course_id"], + db=db) return jsonify(success=True), 200 return jsonify(success=False, message="Can only delete a lecture on Hermes for an OK course you are a part of"), 403 @@ -638,7 +645,7 @@ def ask_piazza_question(course_ok_id, lecture_url_name, video_index, return jsonify(success=False, message="Piazza Post is not active, please tell an instructor to a. recreate the post on Hermes or b. Delete this lecture"), 403 return jsonify(success=False, - message="Please enter a question"), 403 + message="Please enter a question"), 400 return jsonify(success=False, message="Can only create ask a question for an OK course you are a part of"), 403 @@ -743,9 +750,88 @@ def create_vitamin(course_ok_id, lecture_url_name, video_index, ok_id=None): message="Can only create a vitamin on Hermes for an OK course you are a part of"), 403 -@app.route( - '/course//lecture//video//create_resource', - methods=["POST"]) +@app.route('/course//lecture//video//edit_vitamin/', methods=["POST"]) +@validate_and_pass_on_ok_id +def edit_vitamin(course_ok_id, lecture_url_name, video_index, vitamin_index, ok_id=None): + """Creates a vitamin in the specified video within a lecture of a course.""" + user_courses = get_updated_user_courses() + int_course_ok_id = int(course_ok_id) + for course in user_courses: + if course['course_id'] == int_course_ok_id: + if course['role'] == consts.INSTRUCTOR: + try: + vitamin = request.get_json().get('vitamin') + # new_vitamin is a DB/BSON object + vitamin.pop('_id') + db[Vitamin.collection].update_one( + { + 'course_ok_id': course_ok_id, + 'lecture_url_name': lecture_url_name, + 'video_index': video_index, + 'vitamin_index': vitamin_index + }, + { + '$set': vitamin + }, + upsert=False + ) + return jsonify(success=True), 200 + except ValueError as e: + return jsonify(success=False, message=str(e)), 500 + return jsonify(success=False, message="Only instructors can create vitamins"), 403 + return jsonify(success=False, message="Can only create a vitamin on Hermes for an OK course you are a part of"), 403 + +@app.route('/course//lecture//video//edit_resource/', methods=["POST"]) +@validate_and_pass_on_ok_id +def edit_resource(course_ok_id, lecture_url_name, video_index, resource_index, ok_id=None): + """Creates a vitamin in the specified video within a lecture of a course.""" + user_courses = get_updated_user_courses() + int_course_ok_id = int(course_ok_id) + for course in user_courses: + if course['course_id'] == int_course_ok_id: + if course['role'] == consts.INSTRUCTOR: + try: + db[Resource.collection].update_one( + { + 'course_ok_id': course_ok_id, + 'lecture_url_name': lecture_url_name, + 'video_index': video_index, + 'resource_index': resource_index + }, + { + '$set': request.form.to_dict() + }, + upsert=False + ) + return jsonify(success=True), 200 + except ValueError as e: + return jsonify(success=False, message=str(e)), 500 + return jsonify(success=False, message="Only instructors can create vitamins"), 403 + return jsonify(success=False, message="Can only create a vitamin on Hermes for an OK course you are a part of"), 403 + +@app.route('/course//lecture//video//edit') +@validate_and_pass_on_ok_id +def edit_video(course_ok_id, lecture_url_name, video_index, ok_id=None): + """Gets all vitamins on a video""" + user_courses = get_updated_user_courses() + int_course_ok_id = int(course_ok_id) + for course in user_courses: + if course['course_id'] == int_course_ok_id: + return bson_dump({ + "vitamins": db[Vitamin.collection].find({ + 'course_ok_id': course_ok_id, + 'lecture_url_name': lecture_url_name, + 'video_index': video_index + }).sort("seconds", 1), + "resources": db[Resource.collection].find({ + 'course_ok_id': course_ok_id, + 'lecture_url_name': lecture_url_name, + 'video_index': video_index + }) + }) + return jsonify(success=False, message="Can only get vitamins on Hermes for an OK course you are a part of"), 403 + +@app.route('/course//lecture//video//create_resource', methods=["POST"]) @validate_and_pass_on_ok_id def create_resource(course_ok_id, lecture_url_name, video_index, ok_id=None): """Creates a resource in the specified video within a lecture of a course.""" @@ -761,13 +847,13 @@ def create_resource(course_ok_id, lecture_url_name, video_index, ok_id=None): 'video_index': video_index }): try: - link = request.form.to_dict()['link'] Resource.add_resource( - course_ok_id=course_ok_id, - lecture_url_name=lecture_url_name, - video_index=video_index, - link=link, - db=db + course_ok_id = course_ok_id, + lecture_url_name = lecture_url_name, + video_index = video_index, + db = db, + resource_data = request.form.to_dict() + ) return jsonify(success=True), 200 except ValueError as e: diff --git a/utils/db_utils.py b/utils/db_utils.py index d73f4c0..c2e0cfc 100644 --- a/utils/db_utils.py +++ b/utils/db_utils.py @@ -260,7 +260,7 @@ def __init__(self, **attr): DBObject.__init__(self, **attr) @staticmethod - def add_resource(course_ok_id, lecture_url_name, video_index, link, db): + def add_resource(course_ok_id, lecture_url_name, video_index, db, resource_data): video = db[Video.collection].find_one( { "course_ok_id": course_ok_id, @@ -271,11 +271,11 @@ def add_resource(course_ok_id, lecture_url_name, video_index, link, db): resource_index = video['num_resources'] insert( Resource( - link = link, resource_index = resource_index, course_ok_id = course_ok_id, lecture_url_name = lecture_url_name, - video_index = video_index + video_index = video_index, + **resource_data ), db )