Skip to content

Commit

Permalink
Merge branch 'dev' into finishvideos
Browse files Browse the repository at this point in the history
  • Loading branch information
KartikKapur authored Jan 28, 2019
2 parents 03f281f + ffe98d3 commit bb9397d
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 18 deletions.
116 changes: 101 additions & 15 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
)
logger.addHandler(sh)
logger.setLevel(logging.INFO)
logger.info('Backend READY')

ok_server = app.config['OK_SERVER']

Expand Down Expand Up @@ -388,19 +389,25 @@ 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
}
).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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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/<course_ok_id>/lecture/<lecture_url_name>/video/<int:video_index>/create_resource',
methods=["POST"])
@app.route('/course/<course_ok_id>/lecture/<lecture_url_name>/video/<int:video_index>/edit_vitamin/<int:vitamin_index>', 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/<course_ok_id>/lecture/<lecture_url_name>/video/<int:video_index>/edit_resource/<int:resource_index>', 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/<course_ok_id>/lecture/<lecture_url_name>/video/<int:video_index>/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/<course_ok_id>/lecture/<lecture_url_name>/video/<int:video_index>/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."""
Expand All @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions utils/db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
)
Expand Down

0 comments on commit bb9397d

Please sign in to comment.