Skip to content

Commit

Permalink
🐛(edx_imports) add utils to extract course_id from resource_link
Browse files Browse the repository at this point in the history
OpenEdX resource link can have several shape, we add an util to manage all cases
 properly.
  • Loading branch information
jbpenrath committed Jul 25, 2024
1 parent 8d99ab1 commit b428a10
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
8 changes: 2 additions & 6 deletions src/backend/joanie/edx_imports/tasks/certificates.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Celery tasks for importing Open edX certificates to Joanie organizations."""
import re

# pylint: disable=too-many-locals,too-many-statements,too-many-branches,broad-exception-caught
# ruff: noqa: SLF001,PLR0915,PLR0912,BLE001
Expand All @@ -18,6 +17,7 @@
from joanie.edx_imports.edx_database import OpenEdxDB
from joanie.edx_imports.utils import (
download_signature_image,
extract_course_id,
extract_organization_code,
format_percent,
make_date_aware,
Expand Down Expand Up @@ -338,11 +338,7 @@ def populate_signatory_certificates(certificate_id=None, course_id=None):
def _populate_signatory_certificate(certificate, **kwargs):
localized_context = certificate.localized_context.copy()
resource_link = certificate.enrollment.course_run.resource_link
key = kwargs.get("course_id") or (
re.match("^.*/courses/(?P<course_id>.*)/course/?$", resource_link).group(
"course_id"
)
)
key = kwargs.get("course_id") or extract_course_id(resource_link)

if not key:
return _STATE_ERRORS
Expand Down
14 changes: 13 additions & 1 deletion src/backend/joanie/edx_imports/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Utility functions for the Open edX import tasks"""

import re

# pylint: disable=too-many-statements,not-callable,too-many-locals
# ruff: noqa: PLR0915,SLF001

from datetime import datetime
from http import HTTPStatus
from logging import getLogger
Expand Down Expand Up @@ -86,6 +88,16 @@ def extract_language_code(edx_user):
return get_language_settings(language).get("code")


def extract_course_id(resource_link):
"""Extract the course id from a resource link"""
matches = re.match("^.*/courses/(?P<course_id>.*)/(course|info)/?$", resource_link)

if not matches:
return None

return matches.group("course_id")


def format_percent(current, total):
"""Format a percentage"""
percent = (current / total) * 100
Expand Down
28 changes: 28 additions & 0 deletions src/backend/joanie/tests/edx_imports/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Test case for utils"""

from django.test import TestCase

from joanie.edx_imports.utils import extract_course_id


class UtilsTestCase(TestCase):
"""Test case for utils"""

def test_utils_extract_course_id_no_match(self):
"""If nothing match, None should be returned"""
self.assertIsNone(extract_course_id("https://openedx.com"))

def test_utils_extract_course_id_matches(self):
"""It should support all kind of OpenEdX resource link."""
resource_links = [
("https://openedx.com/courses/%s/course", "course-v1:fun+101+run01"),
("https://openedx.com/courses/%s/info", "course-v1:fun+101+run01"),
("https://openedx.com/courses/%s/course/", "course-v1:fun+101+run01"),
("https://openedx.com/courses/%s/info/", "course-v1:fun+101+run01"),
("https://openedx.com/courses/%s/course/", "fun/101/run01"),
("https://openedx.com/courses/%s/info/", "fun/101/run01"),
]

for url, course_id in resource_links:
resource_link = url % course_id
self.assertEqual(extract_course_id(resource_link), course_id)

0 comments on commit b428a10

Please sign in to comment.