diff --git a/app.py b/app.py index 811f4db..c1f22d8 100644 --- a/app.py +++ b/app.py @@ -541,20 +541,19 @@ def create_piazza_bot(course_ok_id, ok_id=None): piazza_master_post_id = request.form['piazza_master_post_id'] if piazza_master_post_id: # A Master post has already been created - Course.update_course(course_ok_id, db, - piazza_active="active") - - Piazza.pin_post(post_id=piazza_master_post_id, - piazza_course_id=piazza_course_id) - - Piazza.add_unadded_lectures(db, course_ok_id) - Piazza.recreate_master_post( - master_id=piazza_master_post_id, - course_ok_id=course_ok_id, - piazza_course_id=piazza_course_id, - db=db) - return jsonify(success=True), 200 - + Course.update_course(course_ok_id, db,piazza_active="active") + if Piazza.post_exists(post_id=piazza_master_post_id, + piazza_course_id=piazza_course_id): + Piazza.pin_post(post_id=piazza_master_post_id, + piazza_course_id=piazza_course_id) + Piazza.add_unadded_lectures(piazza_course_id, piazza_master_post_id, db, course_ok_id) + Piazza.recreate_master_post( + master_id=piazza_master_post_id, + course_ok_id=course_ok_id, + piazza_course_id=piazza_course_id, + db=db + ) + return jsonify(success=True), 200 try: master_post = Piazza.create_master_post( piazza_course_id=piazza_course_id, @@ -562,7 +561,7 @@ def create_piazza_bot(course_ok_id, ok_id=None): ) master_id = master_post["nr"] - Piazza.add_unadded_lectures(db, course_ok_id) + Piazza.add_unadded_lectures(piazza_course_id, piazza_master_post_id, db, course_ok_id) Piazza.recreate_master_post(master_id=master_id, course_ok_id=course_ok_id, piazza_course_id=piazza_course_id, @@ -609,7 +608,8 @@ def ask_piazza_question(course_ok_id, lecture_url_name, video_index, data = get_user_data() name = data["name"] email = data["email"] - try: + if piazza.post_exists(post_id=request.form["piazza_lecture_post_id"], + piazza_course_id=request.form["piazza_course_id"]): identity_msg = "posted on behalf of " + name post_id = Piazza.create_followup_question( piazza_lecture_post_id, request.form["video_url"], tag, @@ -628,10 +628,9 @@ def ask_piazza_question(course_ok_id, lecture_url_name, video_index, identity=name) except Exception as e: pass - except Exception as e: - 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=True), 200 + return jsonify(success=True), 200 + 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 return jsonify(success=False, @@ -646,8 +645,6 @@ def disable_piazza(course_ok_id, ok_id=None): for course in user_courses: if course['course_id'] == int_course_ok_id: - Piazza.unpin_post(post_id=request.form["piazza_master_post_id"], - piazza_course_id=request.form["piazza_course_id"]) db[Course.collection].update_one({ "course_ok_id": course_ok_id}, { @@ -656,16 +653,18 @@ def disable_piazza(course_ok_id, ok_id=None): } } ) - try: - Piazza.unpin_post(post_id=request.form["piazza_master_id"], + if Piazza.post_exists(post_id=request.form["piazza_master_post_id"], + piazza_course_id=request.form[ + "piazza_course_id"]): + Piazza.unpin_post(post_id=request.form["piazza_master_post_id"], piazza_course_id=request.form[ "piazza_course_id"]) - except: + else: db[Course.collection].update_one({ "course_ok_id": course_ok_id}, { "$set": { - "piazza_course_id": "" + "piazza_master_post_id": "" } } ) @@ -836,6 +835,7 @@ def answer_vitamin(course_ok_id, lecture_url_name, video_index, vitamin_index, """Submits the user's answer to a given vitamin and returns if the user got it correct or not.""" user_courses = get_updated_user_courses() int_course_ok_id = int(course_ok_id) + user_ok_id = get_user_data()["id"] for course in user_courses: if course['course_id'] == int_course_ok_id: vitamin = db[Vitamin.collection].find_one({ @@ -845,7 +845,9 @@ def answer_vitamin(course_ok_id, lecture_url_name, video_index, vitamin_index, 'vitamin_index': vitamin_index }) if vitamin: - ## Add SQL part for data collection/participation here. + time = datetime.now() + + sql_client.answer_vitamin(user_ok_id, course_ok_id, time.strftime("%Y-%m-%d %H:%M:%S"), vitamin['answer'], video_index, vitamin_index, lecture_url_name) submission = request.get_json().get('answer') if submission == vitamin['answer']: return jsonify(success=True, message="Correct!"), 200 diff --git a/utils/piazza_client.py b/utils/piazza_client.py index 88c6a09..832ffd9 100644 --- a/utils/piazza_client.py +++ b/utils/piazza_client.py @@ -229,16 +229,31 @@ def delete_post(network=None, piazza_course_id=None, cid=None, post_data=None, except: return -def add_unadded_lectures(db,course_ok_id): +def add_unadded_lectures(piazza_course_id, piazza_master_post_id, db,course_ok_id): not_on_piazza_db_obj = db[Lecture.collection].find({ "lecture_piazza_id": "", "course_ok_id": course_ok_id }) for lecture in not_on_piazza_db_obj: - lecture_post = Piazza.create_lecture_post( + lecture_post = create_lecture_post( lecture_title=lecture["name"], date=lecture["date"], piazza_course_id=piazza_course_id, - master_id=piazza_master_post_id + master_id=piazza_master_post_id, + course_ok_id=course_ok_id, + lecture_url_name=lecture["lecture_url_name"], + db=db ) + +def post_exists(post_id, network=None, piazza_course_id = None): + try: + if not network: + network = piazza.network(piazza_course_id) + rpc = network._rpc + post_data = network.get_post(post_id) + post_data['history'][0] + return True + except Exception as e: + print(e) + return False diff --git a/utils/sql_client.py b/utils/sql_client.py index 0e069a7..1516e87 100644 --- a/utils/sql_client.py +++ b/utils/sql_client.py @@ -30,18 +30,23 @@ def retrieve_questions_for_timestamp(self, start_second, end_second, course_ok_i (start_second, end_second, course_ok_id, lecture_url_name, video_index) ) self.conn.commit() - cursor = cur.fetchall() + rows = cur.fetchall() cur.close() - return cursor + return rows def answer_vitamin(self,user_ok_id,course_ok_id, time_answered,answer, video_index, vitamin_index,lecture_url_name): cur = self.conn.cursor() + print("here") + cur.execute( 'EXECUTE answer_vitamin (%s, %s, %s, %s, %s, %s, %s)', (user_ok_id, course_ok_id, time_answered, answer,video_index, vitamin_index, lecture_url_name) ) + self.conn.commit() + cur.close() + def watch_video(self,user_ok_id, course_ok_id,time_watched,video_index, lecture_url_name): cur = self.conn.cursor() @@ -49,3 +54,5 @@ def watch_video(self,user_ok_id, course_ok_id,time_watched,video_index, lecture_ 'EXECUTE watch_video (%s, %s, %s, %s, %s)', (user_ok_id, course_ok_id,time_watched,video_index, lecture_url_name) ) + self.conn.commit() + cur.close()