Skip to content

Commit

Permalink
Merge pull request #29 from Hermes-Berkeley-Org/finishvideos
Browse files Browse the repository at this point in the history
Finishvideos
  • Loading branch information
KartikKapur authored Jan 28, 2019
2 parents ffe98d3 + bb9397d commit 121a43f
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 36 deletions.
61 changes: 35 additions & 26 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,26 +550,30 @@ def create_piazza_bot(course_ok_id, ok_id=None):
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

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,
content=request.form.get("content") or ""
)
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,
Expand Down Expand Up @@ -616,7 +620,9 @@ 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,
Expand All @@ -625,7 +631,6 @@ def ask_piazza_question(course_ok_id, lecture_url_name, video_index,
identity_msg=identity_msg
)["id"]
try:

sql_client.post_question(
user_email=email,
course_ok_id=course_ok_id,
Expand All @@ -636,10 +641,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"), 400
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"), 400
return jsonify(success=False,
Expand All @@ -654,8 +658,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},
{
Expand All @@ -664,16 +666,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": ""
}
}
)
Expand Down Expand Up @@ -923,6 +927,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({
Expand All @@ -932,7 +937,11 @@ 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,
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
Expand Down
37 changes: 29 additions & 8 deletions utils/piazza_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def create_master_post(content="", network=None, piazza_course_id=None):
return master_post


def create_lecture_post(lecture_title, date,db,master_id,course_ok_id, lecture_url_name,
def create_lecture_post(lecture_title, date, db, master_id, course_ok_id,
lecture_url_name,
network=None, piazza_course_id=None, content=None):
"""
Creates a lecture post on piazza for a given lecture number. Takes in
Expand Down Expand Up @@ -85,7 +86,8 @@ def create_lecture_post(lecture_title, date,db,master_id,course_ok_id, lecture_u


def create_followup_question(lecture_post_id, url, tag, question, network=None,
piazza_course_id=None, identity_msg="posted Anonymously"):
piazza_course_id=None,
identity_msg="posted Anonymously"):
"""Adds a followup question to a given lecture post. Takes in a lecture number,
course id, and contents of a question.
piazza_course_id: (the id in the url)- piazza.com/<piazza_course_id>
Expand Down Expand Up @@ -188,15 +190,15 @@ def recreate_master_post(master_id, course_ok_id, db, network=None,
cid=master_id, content=content)



def pin_post(post_id, network=None, piazza_course_id=None):
if not network:
network = piazza.network(piazza_course_id)
rpc = network._rpc
post = network.get_post(post_id)
post_content = post["history"][0]["content"]
post_content += "<p>#pin</p>"
edit_post(network=network, piazza_course_id=piazza_course_id, cid=post_id, content=post_content)
edit_post(network=network, piazza_course_id=piazza_course_id, cid=post_id,
content=post_content)


def unpin_post(post_id, network=None, piazza_course_id=None):
Expand All @@ -205,7 +207,8 @@ def unpin_post(post_id, network=None, piazza_course_id=None):
rpc = network._rpc
post = network.get_post(post_id)
post_content = post["history"][0]["content"].replace("#pin", "")
edit_post(network=network, piazza_course_id=piazza_course_id, cid=post_id, content=post_content)
edit_post(network=network, piazza_course_id=piazza_course_id, cid=post_id,
content=post_content)


def delete_post(network=None, piazza_course_id=None, cid=None, post_data=None,
Expand All @@ -229,16 +232,34 @@ 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
24 changes: 24 additions & 0 deletions utils/schemas.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,32 @@ CREATE TABLE IF NOT EXISTS piazza_questions (
identity varchar NOT NULL
);

CREATE TABLE IF NOT EXISTS vitamin_answers (
user_ok_id varchar NOT NULL,
course_ok_id varchar NOT NULL,
time_answered timestamp NOT NULL,
answer varchar NOT NULL,
video_index integer NOT NULL,
vitamin_index integer NOT NULL,
lecture_url_name varchar NOT NULL
);

CREATE TABLE IF NOT EXISTS videos_watched (
user_ok_id varchar NOT NULL,
course_ok_id varchar NOT NULL,
time_watched timestamp NOT NULL,
video_index integer NOT NULL,
lecture_url_name varchar NOT NULL
);

PREPARE post_question AS
INSERT INTO piazza_questions VALUES ($1, $2, $3, $4, $5, $6, $7);

PREPARE retrieve_questions_for_timestamp as
SELECT * FROM piazza_questions WHERE seconds >= $1 AND seconds <= $2 AND course_ok_id=($3) AND lecture_url_name=($4) AND video_index=($5);

PREPARE answer_vitamin as
INSERT INTO vitamin_answers VALUES ($1, $2, localtimestamp, $3, $4, $5, $6);

PREPARE watch_video as
INSERT INTO videos_watched VALUES ($1, $2, $3, $4, $5)
27 changes: 25 additions & 2 deletions utils/sql_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ 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 rows

def answer_vitamin(self,user_ok_id,course_ok_id,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)',
(user_ok_id, course_ok_id, 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()
cur.execute(
'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()
return cursor

0 comments on commit 121a43f

Please sign in to comment.