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 4, 2024
1 parent bd77a61 commit c1abfce
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 30 deletions.
7 changes: 1 addition & 6 deletions courses/management/commands/sync_external_course_runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

from courses.sync_external_courses.external_course_sync_api import (
VENDOR_KEYMAPS,
EmeritusKeyMap,
GlobalAlumniKeyMap,
fetch_external_courses,
update_external_course_runs,
)
Expand Down Expand Up @@ -48,11 +46,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
7 changes: 3 additions & 4 deletions courses/migrations/0041_platform_sync_daily.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@


class Migration(migrations.Migration):

dependencies = [
('courses', '0040_alter_courserun_courseware_id'),
("courses", "0040_alter_courserun_courseware_id"),
]

operations = [
migrations.AddField(
model_name='platform',
name='sync_daily',
model_name="platform",
name="sync_daily",
field=models.BooleanField(default=False),
),
]
22 changes: 18 additions & 4 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 @@ -34,6 +36,7 @@ class BaseKeyMap:
"""
Base class for course sync keys with common attributes.
"""

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

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

Expand All @@ -74,14 +78,17 @@ class GlobalAlumniKeyMap(BaseKeyMap):
"""
Global Alumni course sync keys.
"""

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


VENDOR_KEYMAPS = {
"emeritus": EmeritusKeyMap(),
"global alumni": GlobalAlumniKeyMap(),
}


class ExternalCourseSyncAPIJobStatus(Enum):
"""
Status of an External Course API Job.
Expand Down Expand Up @@ -150,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 @@ -222,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 @@ -508,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
32 changes: 21 additions & 11 deletions courses/sync_external_courses/external_course_sync_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@
from courses.factories import CourseFactory, CourseRunFactory, PlatformFactory
from courses.models import Course
from courses.sync_external_courses.external_course_sync_api import (
ExternalCourse,
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 @@ -206,7 +206,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 @@ -287,7 +290,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 @@ -317,7 +321,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 @@ -340,7 +346,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 @@ -568,7 +576,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 @@ -619,7 +629,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 @@ -729,9 +741,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
8 changes: 4 additions & 4 deletions courses/tasks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_sync_courseruns_data(mocker):
def test_task_sync_external_course_runs(mocker, settings):
"""Test task_sync_external_course_runs skips platforms not in VENDOR_KEYMAPS"""
settings.FEATURES["ENABLE_EXTERNAL_COURSE_SYNC"] = True

mock_fetch_external_courses = mocker.patch("courses.tasks.fetch_external_courses")
mock_update_external_course_runs = mocker.patch(
"courses.tasks.update_external_course_runs"
Expand All @@ -37,13 +37,13 @@ def test_task_sync_external_course_runs(mocker, settings):

PlatformFactory.create(name="Emeritus", sync_daily=True)
PlatformFactory.create(name="UnknownPlatform", sync_daily=True)

task_sync_external_course_runs.delay()

mock_fetch_external_courses.assert_called_once()
mock_update_external_course_runs.assert_called_once()

log_mock.exception.assert_called_once_with(
"The platform '%s' does not have a sync API configured. Please disable the 'sync_daily' setting for this platform.",
"UnknownPlatform",
)
)

0 comments on commit c1abfce

Please sign in to comment.