fix: field adjustments to product recommendations endpoint (#32593)
* fix: field adjustments to product recommendations endpoint * chore: formatting for files * chore: indenting fix * chore: removed f-strings
This commit is contained in:
@@ -60,16 +60,18 @@ class AboutPageProductRecommendationsSerializer(serializers.Serializer):
|
||||
class LearnerDashboardProductRecommendationsSerializer(serializers.Serializer):
|
||||
"""Serializer for product recommendations for the Learner Dashboard"""
|
||||
title = serializers.CharField()
|
||||
courseRunKey = serializers.SerializerMethodField()
|
||||
marketingUrl = serializers.URLField(source="marketing_url")
|
||||
courseType = serializers.CharField(source="course_type")
|
||||
image = CourseImageSerializer()
|
||||
prospectusPath = serializers.SerializerMethodField()
|
||||
owners = serializers.ListField(
|
||||
child=CourseOwnersSerializer(), allow_empty=True
|
||||
)
|
||||
courseType = serializers.CharField(source="course_type")
|
||||
|
||||
def get_prospectusPath(self, instance):
|
||||
url_slug = instance.get("url_slug")
|
||||
return f"course/{url_slug}"
|
||||
def get_courseRunKey(self, instance):
|
||||
active_course_run_key = instance.get('active_course_run_key')
|
||||
|
||||
return active_course_run_key if active_course_run_key else instance.get('course_runs')[0]['key']
|
||||
|
||||
|
||||
class AboutPageRecommendationsSerializer(serializers.Serializer):
|
||||
|
||||
@@ -48,10 +48,12 @@ mock_course_data = [
|
||||
mock_cross_product_data = [
|
||||
{
|
||||
"title": "Title 0",
|
||||
"courseRunKey": "course-v1:Test+2023_T0",
|
||||
"marketingUrl": "https://www.marketing_url0.com",
|
||||
"courseType": "executive-education",
|
||||
"image": {
|
||||
"src": "https://www.logo_image_url0.com"
|
||||
},
|
||||
"prospectusPath": "course/https://www.marketing_url0.com",
|
||||
"owners": [
|
||||
{
|
||||
"key": "org-0",
|
||||
@@ -59,14 +61,15 @@ mock_cross_product_data = [
|
||||
"logoImageUrl": "https://discovery.com/organization/logos/org-0.png"
|
||||
}
|
||||
],
|
||||
"courseType": "executive-education"
|
||||
},
|
||||
{
|
||||
"title": "Title 1",
|
||||
"courseRunKey": "course-v1:Test+2023_T1",
|
||||
"marketingUrl": "https://www.marketing_url1.com",
|
||||
"courseType": "executive-education",
|
||||
"image": {
|
||||
"src": "https://www.logo_image_url1.com"
|
||||
},
|
||||
"prospectusPath": "course/https://www.marketing_url1.com",
|
||||
"owners": [
|
||||
{
|
||||
"key": "org-1",
|
||||
@@ -74,7 +77,6 @@ mock_cross_product_data = [
|
||||
"logoImageUrl": "https://discovery.com/organization/logos/org-1.png"
|
||||
}
|
||||
],
|
||||
"courseType": "executive-education"
|
||||
},
|
||||
]
|
||||
|
||||
@@ -82,10 +84,12 @@ mock_amplitude_data = [
|
||||
*mock_cross_product_data,
|
||||
{
|
||||
"title": "Title 2",
|
||||
"courseRunKey": "course-v1:Test+2023_T2",
|
||||
"marketingUrl": "https://www.marketing_url2.com",
|
||||
"courseType": "executive-education",
|
||||
"image": {
|
||||
"src": "https://www.logo_image_url2.com"
|
||||
},
|
||||
"prospectusPath": "course/https://www.marketing_url2.com",
|
||||
"owners": [
|
||||
{
|
||||
"key": "org-2",
|
||||
@@ -93,14 +97,15 @@ mock_amplitude_data = [
|
||||
"logoImageUrl": "https://discovery.com/organization/logos/org-2.png"
|
||||
}
|
||||
],
|
||||
"courseType": "executive-education"
|
||||
},
|
||||
{
|
||||
"title": "Title 3",
|
||||
"courseRunKey": "course-v1:Test+2023_T3",
|
||||
"marketingUrl": "https://www.marketing_url3.com",
|
||||
"courseType": "executive-education",
|
||||
"image": {
|
||||
"src": "https://www.logo_image_url3.com"
|
||||
},
|
||||
"prospectusPath": "course/https://www.marketing_url3.com",
|
||||
"owners": [
|
||||
{
|
||||
"key": "org-3",
|
||||
@@ -108,7 +113,6 @@ mock_amplitude_data = [
|
||||
"logoImageUrl": "https://discovery.com/organization/logos/org-3.png"
|
||||
}
|
||||
],
|
||||
"courseType": "executive-education"
|
||||
}
|
||||
]
|
||||
|
||||
@@ -125,6 +129,11 @@ def get_general_recommendations():
|
||||
"course_type": "credit-verified-audit",
|
||||
"logo_image_url": "https://discovery.com/organization/logos/org-1.png",
|
||||
"marketing_url": "https://www.marketing_url.com",
|
||||
"course_runs": [
|
||||
{
|
||||
"key": "course-v1:MITx+6.00.1x+2T2023",
|
||||
}
|
||||
],
|
||||
"owners": [
|
||||
{
|
||||
"key": "MITx",
|
||||
|
||||
@@ -97,7 +97,7 @@ class TestCrossProductRecommendationsSerializers(TestCase):
|
||||
AmplitudeRecommendationsSerializer, and CrossProductAndAmplitudeRecommendations Serializer
|
||||
"""
|
||||
|
||||
def mock_recommended_courses(self, num_of_courses=2, amplitude_courses=False):
|
||||
def mock_recommended_courses(self, num_of_courses=2):
|
||||
"""Course data mock"""
|
||||
|
||||
recommended_courses = []
|
||||
@@ -132,20 +132,12 @@ class TestCrossProductRecommendationsSerializers(TestCase):
|
||||
"marketing_url": f"https://www.marketing_url{index}.com",
|
||||
"availability": "Current",
|
||||
},
|
||||
"active_course_run_key": f"course-v1:Test+2023_T{index}",
|
||||
"marketing_url": f"https://www.marketing_url{index}.com",
|
||||
"location_restriction": None
|
||||
},
|
||||
)
|
||||
|
||||
if amplitude_courses:
|
||||
keys_to_remove = ["active_course_run", "key", "uuid"]
|
||||
amplitude_courses = []
|
||||
|
||||
for course in recommended_courses:
|
||||
new_course = {key: value for key, value in course.items() if key not in keys_to_remove}
|
||||
amplitude_courses.append(new_course)
|
||||
|
||||
return amplitude_courses
|
||||
|
||||
return recommended_courses
|
||||
|
||||
def test_successful_cross_product_recommendation_serialization(self):
|
||||
@@ -178,7 +170,7 @@ class TestCrossProductRecommendationsSerializers(TestCase):
|
||||
"""Test that course data serializes correctly for CrossProductAndAmplitudeRecommendationSerializer"""
|
||||
|
||||
cross_product_courses = self.mock_recommended_courses(num_of_courses=2)
|
||||
amplitude_courses = self.mock_recommended_courses(num_of_courses=4, amplitude_courses=True)
|
||||
amplitude_courses = self.mock_recommended_courses(num_of_courses=4)
|
||||
|
||||
serialized_data = CrossProductAndAmplitudeRecommendationsSerializer({
|
||||
"crossProductCourses": cross_product_courses,
|
||||
|
||||
@@ -387,13 +387,12 @@ class TestProductRecommendationsView(APITestCase):
|
||||
"image": {
|
||||
"src": "https://www.logo_image_url.com",
|
||||
},
|
||||
"url_slug": "https://www.marketing_url.com",
|
||||
"course_type": "executive-education",
|
||||
"owners": [
|
||||
{
|
||||
"key": "org-1",
|
||||
"name": "org 1",
|
||||
"logo_image_url": "https://discovery.com/organization/logos/org-1.png",
|
||||
"key": "org-1",
|
||||
"name": "org 1",
|
||||
"logo_image_url": "https://discovery.com/organization/logos/org-1.png",
|
||||
},
|
||||
],
|
||||
"course_runs": [
|
||||
@@ -405,6 +404,8 @@ class TestProductRecommendationsView(APITestCase):
|
||||
"status": "published"
|
||||
}
|
||||
],
|
||||
"marketing_url": "https://www.marketing_url.com/course/some-course",
|
||||
"advertised_course_run_uuid": f"course-v1:{key}+2023_T2",
|
||||
}
|
||||
if keys_with_restriction and key in keys_with_restriction:
|
||||
course.update({
|
||||
|
||||
@@ -213,10 +213,11 @@ class ProductRecommendationsView(APIView):
|
||||
"title",
|
||||
"owners",
|
||||
"image",
|
||||
"url_slug",
|
||||
"course_type",
|
||||
"course_runs",
|
||||
"location_restriction",
|
||||
"marketing_url",
|
||||
"advertised_course_run_uuid",
|
||||
]
|
||||
|
||||
def _get_amplitude_recommendations(self, user, user_country_code):
|
||||
@@ -262,6 +263,9 @@ class ProductRecommendationsView(APIView):
|
||||
and course.get("course_runs", [])
|
||||
and not _has_country_restrictions(course, user_country_code)
|
||||
):
|
||||
active_course_run = get_active_course_run(course)
|
||||
if active_course_run:
|
||||
course.update({"active_course_run_key": active_course_run.get("key")})
|
||||
|
||||
filtered_cross_product_courses.append(course)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user