Skip to content

Commit

Permalink
feat: add products and product versions for emeritus products (#3045)
Browse files Browse the repository at this point in the history
* feat: create product and product versions for external products

* feat: add emeritus course prices
  • Loading branch information
asadali145 authored Jul 15, 2024
1 parent e20bbdf commit 9a55361
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 3 deletions.
25 changes: 25 additions & 0 deletions courses/management/commands/sync_external_course_runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,31 @@ def handle(self, *args, **options): # noqa: ARG002
f"External Course Run Codes: {stats['course_runs_updated'] if stats['course_runs_updated'] else None}.\n"
)
)
self.stdout.write(
self.style.SUCCESS(
f"Number of Products Created {len(stats['products_created'])}."
)
)
self.stdout.write(
self.style.SUCCESS(
f"Course Run courseware_ids: {stats['products_created'] if stats['products_created'] else None}.\n"
)
)
self.stdout.write(
self.style.SUCCESS(
f"Number of Product Versions Created {len(stats['product_versions_created'])}."
)
)
self.stdout.write(
self.style.SUCCESS(
f"Course Run courseware_ids: {stats['product_versions_created'] if stats['product_versions_created'] else None}.\n"
)
)
self.stdout.write(
self.style.SUCCESS(
f"Course Runs without prices: {stats['course_runs_without_prices'] if stats['course_runs_without_prices'] else None}.\n"
)
)
self.stdout.write(
self.style.SUCCESS(
f"Number of Courses Pages Created {len(stats['course_pages_created'])}."
Expand Down
71 changes: 68 additions & 3 deletions courses/sync_external_courses/emeritus_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from datetime import timedelta
from enum import Enum

from django.contrib.contenttypes.models import ContentType
from django.db import transaction
from wagtail.models import Page

Expand All @@ -17,6 +18,7 @@
from courses.api import generate_course_readable_id
from courses.models import Course, CourseRun, CourseTopic, Platform
from courses.sync_external_courses.emeritus_api_client import EmeritusAPIClient
from ecommerce.models import Product, ProductVersion
from mitxpro.utils import clean_url, now_in_utc, strip_datetime

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -69,6 +71,11 @@ def __init__(self, emeritus_course_json):

self.course_run_code = emeritus_course_json.get("course_run_code")
self.course_run_tag = generate_emeritus_course_run_tag(self.course_run_code)
self.price = (
float(emeritus_course_json.get("list_price"))
if emeritus_course_json.get("list_price")
else None
)

self.start_date = strip_datetime(
emeritus_course_json.get("start_date"), EmeritusKeyMap.DATE_FORMAT.value
Expand Down Expand Up @@ -165,7 +172,7 @@ def fetch_emeritus_courses():
log.error("Something unexpected happened!")


def update_emeritus_course_runs(emeritus_courses): # noqa: C901
def update_emeritus_course_runs(emeritus_courses): # noqa: C901, PLR0915
"""
Updates or creates the required course data i.e. Course, CourseRun,
ExternalCoursePage, CourseTopic, WhoShouldEnrollPage, and LearningOutcomesPage
Expand All @@ -184,6 +191,9 @@ def update_emeritus_course_runs(emeritus_courses): # noqa: C901
"course_pages_updated": set(),
"course_runs_skipped": set(),
"course_runs_expired": set(),
"products_created": set(),
"product_versions_created": set(),
"course_runs_without_prices": set(),
}

for emeritus_course_json in emeritus_courses:
Expand Down Expand Up @@ -249,6 +259,35 @@ def update_emeritus_course_runs(emeritus_courses): # noqa: C901
else:
stats["course_runs_updated"].add(course_run.external_course_run_id)

log.info(
f"Creating or Updating Product and Product Version, course run courseware_id: {course_run.external_course_run_id}, Price: {emeritus_course.price}" # noqa: G004
)

if emeritus_course.price:
product_created, product_version_created = (
create_or_update_product_and_product_version(
emeritus_course, course_run
)
)
if product_created:
stats["products_created"].add(course_run.external_course_run_id)
log.info(
f"Created Product for course run: {course_run.courseware_id}" # noqa: G004
)

if product_version_created:
stats["product_versions_created"].add(
course_run.external_course_run_id
)
log.info(
f"Created Product Version for course run: {course_run.courseware_id}, Price: {emeritus_course.price}" # noqa: G004
)
else:
log.info(
f"Price is Null for course run code: {emeritus_course.course_run_code}" # noqa: G004
)
stats["course_runs_without_prices"].add(emeritus_course.course_run_code)

log.info(
f"Creating or Updating course page, title: {emeritus_course.course_title}, course_code: {emeritus_course.course_run_code}" # noqa: G004
)
Expand Down Expand Up @@ -293,6 +332,32 @@ def update_emeritus_course_runs(emeritus_courses): # noqa: C901
return stats


def create_or_update_product_and_product_version(emeritus_course, course_run):
"""
Creates or Updates Product and Product Version for the course run.
Args:
emeritus_course(EmeritusCourse): EmeritusCourse object
course_run(CourseRun): CourseRun object
Returns:
tuple: (product is created, product version is created)
"""
current_price = course_run.current_price
if not current_price or current_price != emeritus_course.price:
product, product_created = Product.objects.get_or_create(
content_type=ContentType.objects.get_for_model(CourseRun),
object_id=course_run.id,
)
ProductVersion.objects.create(
product=product,
price=emeritus_course.price,
description=course_run.courseware_id,
)
return product_created, True
return False, False


def generate_emeritus_course_run_tag(course_run_code):
"""
Returns the course run tag generated using the Emeritus Course run code.
Expand Down Expand Up @@ -388,7 +453,7 @@ def create_or_update_emeritus_course_run(course, emeritus_course):
live=True,
)
log.info(
f"Created Course Run, title: {emeritus_course.course_title}, external_course_run_id: {emeritus_course.course_run_code}" # noqa: G004
f"Created Course Run, title: {emeritus_course.course_title}, external_course_run_id: {course_run.external_course_run_id}" # noqa: G004
)
return course_run, True
elif (
Expand All @@ -409,7 +474,7 @@ def create_or_update_emeritus_course_run(course, emeritus_course):
course_run.end_date = emeritus_course.end_date
course_run.save()
log.info(
f"Updated Course Run, title: {emeritus_course.course_title}, external_course_run_id: {emeritus_course.course_run_code}" # noqa: G004
f"Updated Course Run, title: {emeritus_course.course_title}, external_course_run_id: {course_run.external_course_run_id}" # noqa: G004
)
return course_run, False

Expand Down

0 comments on commit 9a55361

Please sign in to comment.