fix: fixed 500 server error on skill-levels api (#31514)
This commit is contained in:
@@ -771,3 +771,35 @@ def get_course_data(course_key_str, fields):
|
||||
)
|
||||
if data:
|
||||
return data
|
||||
|
||||
|
||||
def get_course_run_data(course_run_id, fields):
|
||||
"""
|
||||
Retrieve information about the course run with the given course run id.
|
||||
|
||||
Arguments:
|
||||
course_run_id: key for the course run about which we are retrieving information.
|
||||
fields (List, string): The given fields that you want to retrieve from API response.
|
||||
|
||||
Returns:
|
||||
dict with details about specified course run.
|
||||
"""
|
||||
user, catalog_integration = check_catalog_integration_and_get_user(error_message_field='Course Run ID')
|
||||
if user:
|
||||
api_client = get_catalog_api_client(user)
|
||||
base_api_url = get_catalog_api_base_url()
|
||||
if course_run_id:
|
||||
course_run_cache_key = f'{catalog_integration.CACHE_KEY}.course_run.{course_run_id}'
|
||||
data = get_api_data(
|
||||
catalog_integration,
|
||||
'course_runs',
|
||||
resource_id=course_run_id,
|
||||
api_client=api_client,
|
||||
base_api_url=base_api_url,
|
||||
cache_key=course_run_cache_key if catalog_integration.is_cache_enabled else None,
|
||||
long_term_cache=True,
|
||||
many=False,
|
||||
fields=fields
|
||||
)
|
||||
if data:
|
||||
return data
|
||||
|
||||
@@ -48,17 +48,20 @@ class LearnerSkillLevelsUtilsTests(SharedModuleStoreTestCase, CatalogIntegration
|
||||
self.catalog_integration = self.create_catalog_integration()
|
||||
|
||||
@mock.patch('openedx.core.djangoapps.user_api.learner_skill_levels.utils.get_course_run_ids')
|
||||
@mock.patch('openedx.core.djangoapps.user_api.learner_skill_levels.utils.get_course_run_data')
|
||||
@mock.patch('openedx.core.djangoapps.user_api.learner_skill_levels.utils.get_course_data')
|
||||
def test_generate_skill_score_mapping(
|
||||
self,
|
||||
mock_get_course_data,
|
||||
mock_get_course_run_data,
|
||||
mock_get_course_run_ids,
|
||||
):
|
||||
"""
|
||||
Test that skill-score mapping is returned in correct format.
|
||||
"""
|
||||
user = UserFactory(username='edX')
|
||||
mock_get_course_run_ids.return_value = ['AWS+OTP-AWSD12']
|
||||
mock_get_course_run_ids.return_value = ['course-v1:AWS+OTP-AWSD12']
|
||||
mock_get_course_run_data.return_value = {'course': 'AWS+OTP'}
|
||||
mock_get_course_data.return_value = DUMMY_COURSE_DATA_RESPONSE
|
||||
result = generate_skill_score_mapping(user)
|
||||
expected_response = {"python": 3, "MongoDB": 3, "Data Science": 3}
|
||||
|
||||
@@ -102,7 +102,7 @@ DUMMY_USERNAMES_RESPONSE = {
|
||||
}
|
||||
|
||||
DUMMY_COURSE_DATA_RESPONSE = {
|
||||
"key": "AWS+OTP-AWSD12",
|
||||
"key": "AWS+OTP",
|
||||
"uuid": "fe1a9ad4-a452-45cd-80e5-9babd3d43f96",
|
||||
"title": "Demonstration Course",
|
||||
"level_type": 'Advanced',
|
||||
|
||||
@@ -10,7 +10,7 @@ from openedx.core.djangoapps.catalog.utils import (
|
||||
get_catalog_api_base_url,
|
||||
|
||||
)
|
||||
from openedx.core.djangoapps.catalog.utils import get_course_data
|
||||
from openedx.core.djangoapps.catalog.utils import get_course_data, get_course_run_data
|
||||
from openedx.core.lib.edx_api_utils import get_api_data
|
||||
|
||||
from .constants import LEVEL_TYPE_SCORE_MAPPING
|
||||
@@ -37,13 +37,17 @@ def generate_skill_score_mapping(user):
|
||||
|
||||
skill_score_mapping = {}
|
||||
for course_run_id in course_run_ids:
|
||||
course_data = get_course_data(course_run_id, ['skill_names', 'level_type'])
|
||||
# fetch course details from course run id to get course key
|
||||
course_run_data = get_course_run_data(course_run_id, ['course'])
|
||||
|
||||
# fetch course details to get level type and skills
|
||||
course_data = get_course_data(course_run_data['course'], ['skill_names', 'level_type'])
|
||||
skill_names = course_data['skill_names']
|
||||
level_type = course_data['level_type'].capitalize()
|
||||
level_type = course_data['level_type']
|
||||
|
||||
# if a level_type is None for a course, we should skip that course.
|
||||
if level_type:
|
||||
score = LEVEL_TYPE_SCORE_MAPPING[level_type]
|
||||
score = LEVEL_TYPE_SCORE_MAPPING[level_type.capitalize()]
|
||||
for skill in skill_names:
|
||||
if skill in skill_score_mapping:
|
||||
# assign scores b/w 1-3 based on level type
|
||||
|
||||
@@ -5,7 +5,7 @@ from collections import defaultdict
|
||||
from copy import deepcopy
|
||||
|
||||
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
|
||||
from rest_framework import permissions
|
||||
from rest_framework import permissions, status
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
|
||||
@@ -93,6 +93,11 @@ class LearnerSkillLevelsView(APIView):
|
||||
"""
|
||||
# get top categories for the given job
|
||||
job_skill_categories = get_top_skill_categories_for_job(job_id)
|
||||
if not job_skill_categories:
|
||||
return Response(
|
||||
status=status.HTTP_404_NOT_FOUND,
|
||||
data={'message': "The job id doesn't exist, enter a valid job id."}
|
||||
)
|
||||
|
||||
# assign scores for every skill request user has learned
|
||||
top_categories = deepcopy(job_skill_categories['skill_categories'])
|
||||
|
||||
Reference in New Issue
Block a user