From bd34d5c657d577ff5f203a0991402dfc52652571 Mon Sep 17 00:00:00 2001 From: Rachel Lougee Date: Fri, 22 Mar 2024 13:26:49 -0400 Subject: [PATCH 1/5] extract duration from EncodeJob when posting video to edx --- ui/api.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/ui/api.py b/ui/api.py index 12fdf7e9..8b5169e7 100644 --- a/ui/api.py +++ b/ui/api.py @@ -3,6 +3,7 @@ """ from uuid import uuid4 +import json import requests from celery import chain from django.conf import settings @@ -37,8 +38,8 @@ def process_dropbox_data(dropbox_upload_data): video = models.Video.objects.create( source_url=dropbox_link["link"], title=dropbox_link["name"][ - : models.Video._meta.get_field("title").max_length - ], + : models.Video._meta.get_field("title").max_length + ], collection=collection, ) models.VideoFile.objects.create( @@ -94,6 +95,10 @@ def post_video_to_edx(video_files): for edx_endpoint in edx_endpoints: try: edx_endpoint.refresh_access_token() + submitted_encode_job = video_files[0].video.encode_jobs.filter(state='Submitted').first() + duration = 0.0 + if submitted_encode_job: + duration = json.loads(submitted_encode_job.message)['Output'].get('Duration') resp = requests.post( edx_endpoint.full_api_url, json={ @@ -102,7 +107,7 @@ def post_video_to_edx(video_files): "encoded_videos": encoded_videos, "courses": [{video_files[0].video.collection.edx_course_id: None}], "status": "file_complete", - "duration": 0.0, + "duration": duration }, headers={ "Authorization": "JWT {}".format(edx_endpoint.access_token), From 7738ef7e334cb39947ed56153c36c30927263c20 Mon Sep 17 00:00:00 2001 From: Rachel Lougee Date: Fri, 22 Mar 2024 15:33:42 -0400 Subject: [PATCH 2/5] format --- ui/api.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/ui/api.py b/ui/api.py index 8b5169e7..f89ea83c 100644 --- a/ui/api.py +++ b/ui/api.py @@ -38,8 +38,8 @@ def process_dropbox_data(dropbox_upload_data): video = models.Video.objects.create( source_url=dropbox_link["link"], title=dropbox_link["name"][ - : models.Video._meta.get_field("title").max_length - ], + : models.Video._meta.get_field("title").max_length + ], collection=collection, ) models.VideoFile.objects.create( @@ -95,10 +95,14 @@ def post_video_to_edx(video_files): for edx_endpoint in edx_endpoints: try: edx_endpoint.refresh_access_token() - submitted_encode_job = video_files[0].video.encode_jobs.filter(state='Submitted').first() + submitted_encode_job = ( + video_files[0].video.encode_jobs.filter(state="Submitted").first() + ) duration = 0.0 if submitted_encode_job: - duration = json.loads(submitted_encode_job.message)['Output'].get('Duration') + duration = json.loads(submitted_encode_job.message)["Output"].get( + "Duration" + ) resp = requests.post( edx_endpoint.full_api_url, json={ @@ -107,7 +111,7 @@ def post_video_to_edx(video_files): "encoded_videos": encoded_videos, "courses": [{video_files[0].video.collection.edx_course_id: None}], "status": "file_complete", - "duration": duration + "duration": duration, }, headers={ "Authorization": "JWT {}".format(edx_endpoint.access_token), From 06a24b7e16c23845790facd165b58092b1b16d53 Mon Sep 17 00:00:00 2001 From: Rachel Lougee Date: Tue, 26 Mar 2024 08:56:14 -0400 Subject: [PATCH 3/5] changes --- ui/api.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ui/api.py b/ui/api.py index f89ea83c..438449b2 100644 --- a/ui/api.py +++ b/ui/api.py @@ -14,7 +14,7 @@ from cloudsync import tasks from odl_video import logging from ui import models -from ui.utils import get_error_response_summary_dict +from ui.utils import get_error_response_summary_dict, get_et_job log = logging.getLogger(__name__) @@ -72,7 +72,7 @@ def post_video_to_edx(video_files): """ encoded_videos = [] for video_file in video_files: - assert video_file.can_add_to_edx, "This video file cannot be added to edX" + # assert video_file.can_add_to_edx, "This video file cannot be added to edX" encoded_videos.append( { "url": video_file.cloudfront_url, @@ -96,13 +96,13 @@ def post_video_to_edx(video_files): try: edx_endpoint.refresh_access_token() submitted_encode_job = ( - video_files[0].video.encode_jobs.filter(state="Submitted").first() + video_files[0].video.encode_jobs.filter(state=0).first() ) duration = 0.0 if submitted_encode_job: - duration = json.loads(submitted_encode_job.message)["Output"].get( - "Duration" - ) + et_job = get_et_job(submitted_encode_job.id) + duration = et_job["Output"].get("Duration") + resp = requests.post( edx_endpoint.full_api_url, json={ From 6e783bae48380a95e5a0a80decdf96a37801dc25 Mon Sep 17 00:00:00 2001 From: Rachel Lougee Date: Tue, 26 Mar 2024 09:00:37 -0400 Subject: [PATCH 4/5] uncomment debug --- ui/api.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ui/api.py b/ui/api.py index 438449b2..81277761 100644 --- a/ui/api.py +++ b/ui/api.py @@ -3,7 +3,6 @@ """ from uuid import uuid4 -import json import requests from celery import chain from django.conf import settings @@ -72,7 +71,7 @@ def post_video_to_edx(video_files): """ encoded_videos = [] for video_file in video_files: - # assert video_file.can_add_to_edx, "This video file cannot be added to edX" + assert video_file.can_add_to_edx, "This video file cannot be added to edX" encoded_videos.append( { "url": video_file.cloudfront_url, From 676a31e3e32a7bd69a49c2450c897653dfa575fa Mon Sep 17 00:00:00 2001 From: Rachel Lougee Date: Tue, 26 Mar 2024 17:00:29 -0400 Subject: [PATCH 5/5] code review feedback --- ui/api.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/ui/api.py b/ui/api.py index 81277761..c0d0cbe9 100644 --- a/ui/api.py +++ b/ui/api.py @@ -1,6 +1,7 @@ """ API methods """ +from ast import literal_eval from uuid import uuid4 import requests @@ -13,7 +14,7 @@ from cloudsync import tasks from odl_video import logging from ui import models -from ui.utils import get_error_response_summary_dict, get_et_job +from ui.utils import get_error_response_summary_dict log = logging.getLogger(__name__) @@ -99,8 +100,12 @@ def post_video_to_edx(video_files): ) duration = 0.0 if submitted_encode_job: - et_job = get_et_job(submitted_encode_job.id) - duration = et_job["Output"].get("Duration") + duration = ( + literal_eval(submitted_encode_job.message) + .get("Output", {}) + .get("Duration", 0.0) + or 0.0 + ) resp = requests.post( edx_endpoint.full_api_url,