Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Dec 3, 2024
1 parent 4177d33 commit e51cf7c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 22 deletions.
5 changes: 1 addition & 4 deletions courses/management/commands/sync_external_course_runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,8 @@ def handle(self, *args, **options): # noqa: ARG002
stats = update_external_course_runs(emeritus_course_runs, keymap)
self.log_stats(stats)
self.stdout.write(
self.style.SUCCESS(
f"External course sync successful for {vendor_name}."
)
self.style.SUCCESS(f"External course sync successful for {vendor_name}.")
)


def log_stats(self, stats):
"""
Expand Down
25 changes: 20 additions & 5 deletions courses/sync_external_courses/external_course_sync_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
)
from courses.api import generate_course_readable_id
from courses.models import Course, CourseRun, CourseTopic, Platform
from courses.sync_external_courses.external_course_sync_api_client import ExternalCourseSyncAPIClient
from courses.sync_external_courses.external_course_sync_api_client import (
ExternalCourseSyncAPIClient,
)
from ecommerce.models import Product, ProductVersion
from mitxpro.utils import clean_url, now_in_utc, strip_datetime

Expand All @@ -33,10 +35,12 @@
EMERITUS_PLATFORM_NAME = "Emeritus"
GLOBAL_ALUMNI_PLATFORM_NAME = "Global Alumni"


class BaseKeyMap:
"""
Base class for course sync keys with common attributes.
"""

DATE_FORMAT = "%Y-%m-%d"
REQUIRED_FIELDS = [
"course_title",
Expand Down Expand Up @@ -69,6 +73,7 @@ class EmeritusKeyMap(BaseKeyMap):
"""
Emeritus course sync keys.
"""

def __init__(self):
super().__init__(platform_name=EMERITUS_PLATFORM_NAME, report_names=["Batch"])

Expand All @@ -77,8 +82,11 @@ class GlobalAlumniKeyMap(BaseKeyMap):
"""
Global Alumni course sync keys.
"""

def __init__(self):
super().__init__(platform_name=GLOBAL_ALUMNI_PLATFORM_NAME, report_names=["GA - Batch"])
super().__init__(
platform_name=GLOBAL_ALUMNI_PLATFORM_NAME, report_names=["GA - Batch"]
)


class ExternalCourseSyncAPIJobStatus(Enum):
Expand Down Expand Up @@ -149,7 +157,9 @@ def __init__(self, external_course_json, keymap):
self.image_name = external_course_json.get("image_name", None)
self.CEUs = str(external_course_json.get("ceu") or "")
self.learning_outcomes_list = (
parse_external_course_data_str(external_course_json.get("learning_outcomes"))
parse_external_course_data_str(
external_course_json.get("learning_outcomes")
)
if external_course_json.get("learning_outcomes")
else []
)
Expand Down Expand Up @@ -221,7 +231,10 @@ def fetch_external_courses(keymap):
)
while True:
job_status = external_course_sync_api_client.get_job_status(job_id)
if job_status["job"]["status"] == ExternalCourseSyncAPIJobStatus.READY.value:
if (
job_status["job"]["status"]
== ExternalCourseSyncAPIJobStatus.READY.value
):
# If true, the query_result is ready to be collected.
log.info("Job complete... requesting results...")
query_response = external_course_sync_api_client.get_query_result(
Expand Down Expand Up @@ -507,7 +520,9 @@ def generate_external_course_run_courseware_id(course_run_tag, course_readable_i
return f"{course_readable_id}+{course_run_tag}"


def create_or_update_external_course_page(course_index_page, course, external_course, keymap):
def create_or_update_external_course_page(
course_index_page, course, external_course, keymap
):
"""
Creates or updates external course page for External course.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

import pytest

from courses.sync_external_courses.external_course_sync_api_client import ExternalCourseSyncAPIClient
from courses.sync_external_courses.external_course_sync_api_client import (
ExternalCourseSyncAPIClient,
)
from mitxpro.test_utils import MockResponse
from mitxpro.utils import now_in_utc

Expand Down
34 changes: 22 additions & 12 deletions courses/sync_external_courses/external_course_sync_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@
from courses.factories import CourseFactory, CourseRunFactory, PlatformFactory
from courses.models import Course
from courses.sync_external_courses.external_course_sync_api import (
ExternalCourse,
EmeritusKeyMap,
EMERITUS_PLATFORM_NAME,
EmeritusKeyMap,
ExternalCourse,
create_learning_outcomes_page,
create_or_update_certificate_page,
create_or_update_external_course_page,
create_or_update_external_course_run,
create_or_update_product_and_product_version,
create_who_should_enroll_in_page,
fetch_external_courses,
generate_external_course_run_tag,
generate_external_course_run_courseware_id,
generate_external_course_run_tag,
parse_external_course_data_str,
save_page_revision,
update_external_course_runs,
Expand Down Expand Up @@ -207,7 +207,10 @@ def test_create_or_update_external_course_page( # noqa: PLR0913

external_course_page, course_page_created, course_page_updated = (
create_or_update_external_course_page(
course_index_page, course, ExternalCourse(external_course_data, keymap=EmeritusKeyMap()), keymap=EmeritusKeyMap()
course_index_page,
course,
ExternalCourse(external_course_data, keymap=EmeritusKeyMap()),
keymap=EmeritusKeyMap(),
)
)
external_course_page = external_course_page.revisions.last().as_object()
Expand Down Expand Up @@ -288,7 +291,8 @@ def test_create_or_update_certificate_page(
certificate_page.unpublish()

certificate_page, is_created, is_updated = create_or_update_certificate_page(
external_course_page, ExternalCourse(external_course_data, keymap=EmeritusKeyMap())
external_course_page,
ExternalCourse(external_course_data, keymap=EmeritusKeyMap()),
)
certificate_page = certificate_page.revisions.last().as_object()
assert certificate_page.CEUs == external_course_data["ceu"]
Expand Down Expand Up @@ -318,7 +322,9 @@ def test_create_who_should_enroll_in_page():
"looking to add critical cybersecurity knowledge and foundational lessons to their resume"
)
create_who_should_enroll_in_page(
course_page, parse_external_course_data_str(who_should_enroll_str), keymap=EmeritusKeyMap()
course_page,
parse_external_course_data_str(who_should_enroll_str),
keymap=EmeritusKeyMap(),
)
assert parse_external_course_data_str(who_should_enroll_str) == [
item.value.source for item in course_page.who_should_enroll.content
Expand All @@ -341,7 +347,9 @@ def test_create_learning_outcomes_page():
"organizations to prepare themselves against cybersecurity attacks"
)
create_learning_outcomes_page(
course_page, parse_external_course_data_str(learning_outcomes_str), keymap=EmeritusKeyMap()
course_page,
parse_external_course_data_str(learning_outcomes_str),
keymap=EmeritusKeyMap(),
)
assert parse_external_course_data_str(learning_outcomes_str) == [
item.value for item in course_page.outcomes.outcome_items
Expand Down Expand Up @@ -569,7 +577,9 @@ def test_fetch_external_courses_success(settings, mocker):
5. If job status is 1 or 2, it is in progress. Wait for 2 seconds and make a get request for Job status.
6. If job status is 3, the results are ready, make a get request to collect the results and return the data.
"""
settings.EXTERNAL_COURSE_SYNC_API_BASE_URL = "https://test_external_course_sync_api.io"
settings.EXTERNAL_COURSE_SYNC_API_BASE_URL = (
"https://test_external_course_sync_api.io"
)
settings.EXTERNAL_COURSE_SYNC_API_KEY = "test_EXTERNAL_COURSE_SYNC_API_KEY"
settings.EXTERNAL_COURSE_SYNC_API_REQUEST_TIMEOUT = 60

Expand Down Expand Up @@ -620,7 +630,9 @@ def test_fetch_external_courses_error(settings, mocker, caplog):
"""
Tests that `fetch_external_courses` specific calls to the External Course Sync API and Fails for Job status 3 and 4.
"""
settings.EXTERNAL_COURSE_SYNC_API_BASE_URL = "https://test_external_course_sync_api.com"
settings.EXTERNAL_COURSE_SYNC_API_BASE_URL = (
"https://test_external_course_sync_api.com"
)
settings.EXTERNAL_COURSE_SYNC_API_KEY = "test_EXTERNAL_COURSE_SYNC_API_KEY"
mock_get = mocker.patch(
"courses.sync_external_courses.external_course_sync_api_client.requests.get"
Expand Down Expand Up @@ -730,9 +742,7 @@ def test_save_page_revision(is_draft_page, has_unpublished_changes):
external_course_page.save_revision()

latest_revision = external_course_page.get_latest_revision_as_object()
latest_revision.external_marketing_url = (
"https://test-external-course-sync-api.io/Internet-of-things-iot-design-and-applications"
)
latest_revision.external_marketing_url = "https://test-external-course-sync-api.io/Internet-of-things-iot-design-and-applications"
save_page_revision(external_course_page, latest_revision)

assert external_course_page.live == (not is_draft_page)
Expand Down

0 comments on commit e51cf7c

Please sign in to comment.