From ba1bd06ff46986901d88297e3d40347e46680022 Mon Sep 17 00:00:00 2001 From: Kartik Kapur Date: Sat, 2 Feb 2019 18:07:09 -0800 Subject: [PATCH 1/2] watch videos --- app.py | 22 +++++++++++++++++++--- utils/schemas.sql | 22 ++++++++-------------- utils/sql_client.py | 14 +++++++------- 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/app.py b/app.py index 9748303..6a530f5 100644 --- a/app.py +++ b/app.py @@ -931,7 +931,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"] + user_ok_id = get_user_data()["email"] for course in user_courses: if course['course_id'] == int_course_ok_id: vitamin = db[Vitamin.collection].find_one({ @@ -944,8 +944,8 @@ def answer_vitamin(course_ok_id, lecture_url_name, video_index, vitamin_index, time = datetime.now() sql_client.answer_vitamin(user_ok_id, course_ok_id, - vitamin['answer'], video_index, - vitamin_index, lecture_url_name) + vitamin['answer'], lecture_url_name, video_index, + vitamin_index) submission = request.get_json().get('answer') if submission == vitamin['answer']: return jsonify(success=True, message="Correct!"), 200 @@ -956,3 +956,19 @@ def answer_vitamin(course_ok_id, lecture_url_name, video_index, vitamin_index, return jsonify(success=False, message="Invalid vitamin"), 404 return jsonify(success=False, message="Can only answer a vitamin on Hermes for an OK course you are a part of"), 403 + +@app.route( + '/course//lecture//video//watch_video', + methods=["POST"]) +@validate_and_pass_on_ok_id +def watch_video(course_ok_id, lecture_url_name, video_index, ok_id=None): + user_courses = get_updated_user_courses() + int_course_ok_id = int(course_ok_id) + user_ok_id = get_user_data()["email"] + for course in user_courses: + if course['course_id'] == int_course_ok_id: + sql_client.watch_video(user_ok_id, course_ok_id, lecture_url_name,video_index) + eturn jsonify(success=True, + message="Watched Video"), 200 + return jsonify(success=False, + message="Can only view a lecture on Hermes for an OK course you are a part of"), 403 diff --git a/utils/schemas.sql b/utils/schemas.sql index b816889..a1e6290 100644 --- a/utils/schemas.sql +++ b/utils/schemas.sql @@ -1,11 +1,5 @@ DEALLOCATE ALL; -CREATE TABLE IF NOT EXISTS attendance ( - user_email varchar NOT NULL, - course_ok_id varchar NOT NULL, - lecture_url_name varchar NOT NULL -); - CREATE TABLE IF NOT EXISTS piazza_questions ( user_email varchar NOT NULL, course_ok_id varchar NOT NULL, @@ -17,21 +11,21 @@ CREATE TABLE IF NOT EXISTS piazza_questions ( ); CREATE TABLE IF NOT EXISTS vitamin_answers ( - user_ok_id varchar NOT NULL, + user_email varchar NOT NULL, course_ok_id varchar NOT NULL, time_answered timestamp NOT NULL, answer varchar NOT NULL, + lecture_url_name varchar NOT NULL, video_index integer NOT NULL, - vitamin_index integer NOT NULL, - lecture_url_name varchar NOT NULL + vitamin_index integer NOT NULL ); CREATE TABLE IF NOT EXISTS videos_watched ( - user_ok_id varchar NOT NULL, + user_email 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 + time_watched timestamp NOT NULL, + lecture_url_name varchar NOT NULL, + video_index integer NOT NULL ); PREPARE post_question AS @@ -44,4 +38,4 @@ 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) + INSERT INTO videos_watched VALUES ($1, $2, localtimestamp, $3, $4) diff --git a/utils/sql_client.py b/utils/sql_client.py index ad86e61..d6d96f4 100644 --- a/utils/sql_client.py +++ b/utils/sql_client.py @@ -34,25 +34,25 @@ def retrieve_questions_for_timestamp(self, start_second, end_second, course_ok_i cur.close() return rows - def answer_vitamin(self,user_ok_id,course_ok_id,answer, - video_index, vitamin_index,lecture_url_name): + def answer_vitamin(self,user_email,course_ok_id,answer, + lecture_url_name, video_index, vitamin_index): 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) + (user_email, course_ok_id, answer,video_index, + lecture_url_name,vitamin_index) ) self.conn.commit() cur.close() - def watch_video(self,user_ok_id, course_ok_id,time_watched,video_index, lecture_url_name): + def watch_video(self,user_email, course_ok_id,lecture_url_name, video_index): 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) + 'EXECUTE watch_video (%s, %s, %s, %s)', + (user_email, course_ok_id,lecture_url_name, video_index) ) self.conn.commit() cur.close() From 4a6df1e43bcb7a79586ff12f279285287c361184 Mon Sep 17 00:00:00 2001 From: Kartik Kapur Date: Sat, 2 Feb 2019 21:31:15 -0800 Subject: [PATCH 2/2] finishvideos --- Dockerfile | 10 ---------- app.py | 12 +++++++++--- app.yaml | 37 +++++++++++++++++++++++++++++++++++++ utils/sql_client.py | 6 ++++++ 4 files changed, 52 insertions(+), 13 deletions(-) delete mode 100644 Dockerfile create mode 100644 app.yaml diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 0e03beb..0000000 --- a/Dockerfile +++ /dev/null @@ -1,10 +0,0 @@ -FROM ubuntu:latest -MAINTAINER Ajay Raj "araj@berkeley.edu" -RUN apt-get update -y -RUN apt-get install -y python3 python3-dev python3-pip nginx -COPY . /app -WORKDIR /app -RUN pip3 install -r requirements.txt -RUN python3 -m nltk.downloader stopwords -ENTRYPOINT ["python3"] -CMD ["app.py"] diff --git a/app.py b/app.py index 6a530f5..7fa1c6d 100644 --- a/app.py +++ b/app.py @@ -30,7 +30,7 @@ import consts app = Flask(__name__) -CORS(app, resources={r"/*": {"origins": os.environ.get('HERMES_UI_URL')}}) +CORS(app) app.config.from_object(Config) client = MongoClient(os.environ.get('MONGODB_URI')) @@ -968,7 +968,13 @@ def watch_video(course_ok_id, lecture_url_name, video_index, ok_id=None): for course in user_courses: if course['course_id'] == int_course_ok_id: sql_client.watch_video(user_ok_id, course_ok_id, lecture_url_name,video_index) - eturn jsonify(success=True, - message="Watched Video"), 200 + return jsonify(success=True,message="Watched Video"), 200 return jsonify(success=False, message="Can only view a lecture on Hermes for an OK course you are a part of"), 403 + +@app.route( + '/course//lecture//video//watch_video', + methods=["GET"]) +@validate_and_pass_on_ok_id +def get_lecture_attendence(course_ok_id, lecture_url_name, video_index, ok_id=None): + return diff --git a/app.yaml b/app.yaml new file mode 100644 index 0000000..4aadcaf --- /dev/null +++ b/app.yaml @@ -0,0 +1,37 @@ +runtime: python +env: flex +entrypoint: gunicorn -b :$PORT main:app + +runtime_config: + python_version: 3 + +# This sample incurs costs to run on the App Engine flexible environment. +# The settings below are to reduce costs during testing and are not appropriate +# for production use. For more information, see: +# https://cloud.google.com/appengine/docs/flexible/python/configuring-your-app-with-app-yaml +manual_scaling: + instances: 1 +resources: + cpu: 1 + memory_gb: 0.5 + disk_size_gb: 10 + +env_variables: + file: ${file(./deploy/private_env)} + MONGODB_URI: file.MONGODB_URI + DATABASE_NAME: private.MONGODB_URI + DEV_TOKEN: private.DEV_TOKEN + CLIENT_ID: private.CLIENT_ID + CLIENT_SECRET: private.CLIENT_SECRET + PIAZZA_EMAIL: private.PIAZZA_EMAIL + PIAZZA_PASSWORD: private.PIAZZA_PASSWORD + SQL_DATABASE_URL: private.SQL_DATABASE_URL + + FLASK_APP: app.py + OK_SERVER: https://okpy.org + OK_MODE: authenticate + TESTING_OK_ID: lejRej + TRANSCRIPTION_MODE: api + OAUTHLIB_INSECURE_TRANSPORT: 1 + FLASK_DEBUG: 1 + HERMES_UI_URL: http://localhost:3000 diff --git a/utils/sql_client.py b/utils/sql_client.py index d6d96f4..11fc3e4 100644 --- a/utils/sql_client.py +++ b/utils/sql_client.py @@ -56,3 +56,9 @@ def watch_video(self,user_email, course_ok_id,lecture_url_name, video_index): ) self.conn.commit() cur.close() + + def get_lecture_attendence(self, user_email, course_ok_id, lecture_url_name): + cur = self.conn.cursor() + cur.execute( + 'EXECUTE lecture_attendence(%s, %s,%s,%s)' + )