-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: job to assign skills to Degreed courses (#1958)
- Loading branch information
1 parent
bb02232
commit bae4c25
Showing
5 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
Your project description goes here. | ||
""" | ||
|
||
__version__ = "4.8.4" | ||
__version__ = "4.8.5" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...rated_channels/integrated_channel/management/commands/assign_skills_to_degreed_courses.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
""" | ||
Assign skills to degreed courses | ||
""" | ||
from logging import getLogger | ||
|
||
from requests.exceptions import ConnectionError, RequestException, Timeout # pylint: disable=redefined-builtin | ||
|
||
from django.contrib import auth | ||
from django.core.management.base import BaseCommand, CommandError | ||
|
||
from enterprise.api_client.enterprise_catalog import EnterpriseCatalogApiClient | ||
from integrated_channels.degreed2.client import Degreed2APIClient | ||
from integrated_channels.exceptions import ClientError | ||
from integrated_channels.integrated_channel.management.commands import IntegratedChannelCommandMixin | ||
from integrated_channels.utils import generate_formatted_log | ||
|
||
User = auth.get_user_model() | ||
LOGGER = getLogger(__name__) | ||
|
||
|
||
class Command(IntegratedChannelCommandMixin, BaseCommand): | ||
""" | ||
Add skill metadata to existing Degreed courses. | ||
./manage.py lms assign_skills_to_degreed_courses | ||
""" | ||
|
||
def add_arguments(self, parser): | ||
""" | ||
Add required arguments to the parser. | ||
""" | ||
parser.add_argument( | ||
'--catalog_user', | ||
dest='catalog_user', | ||
required=True, | ||
metavar='ENTERPRISE_CATALOG_API_USERNAME', | ||
help='Use this user to access the Enterprise Catalog API.' | ||
) | ||
super().add_arguments(parser) | ||
|
||
def _prepare_json_payload_for_skills_endpoint(self, course_skills): | ||
""" | ||
Prepares a json payload for skills in the Degreed expected format. | ||
""" | ||
course_skills_json = [] | ||
for skill in course_skills: | ||
skill_data = {"type": "skills", "id": skill} | ||
course_skills_json.append(skill_data) | ||
return { | ||
"data": course_skills_json | ||
} | ||
|
||
def handle(self, *args, **options): | ||
""" | ||
Update all existing Degreed courses to assign skills metadata. | ||
""" | ||
options['channel'] = 'DEGREED2' | ||
username = options['catalog_user'] | ||
|
||
try: | ||
user = User.objects.get(username=username) | ||
except User.DoesNotExist as no_user_error: | ||
raise CommandError('A user with the username {} was not found.'.format(username)) from no_user_error | ||
|
||
enterprise_catalog_client = EnterpriseCatalogApiClient(user) | ||
|
||
for degreed_channel_config in self.get_integrated_channels(options): | ||
enterprise_customer = degreed_channel_config.enterprise_customer | ||
enterprise_customer_catalogs = degreed_channel_config.customer_catalogs_to_transmit or \ | ||
enterprise_customer.enterprise_customer_catalogs.all() | ||
try: | ||
content_metadata_in_catalogs = enterprise_catalog_client.get_content_metadata( | ||
enterprise_customer, | ||
enterprise_customer_catalogs | ||
) | ||
except (RequestException, ConnectionError, Timeout) as exc: | ||
LOGGER.exception( | ||
'Failed to retrieve enterprise catalogs content metadata due to: [%s]', str(exc) | ||
) | ||
continue | ||
|
||
degreed_client = Degreed2APIClient(degreed_channel_config) | ||
|
||
for content_item in content_metadata_in_catalogs: | ||
|
||
course_id = content_item.get('key', []) | ||
course_skills = content_item.get('skill_names', []) | ||
json_payload = self._prepare_json_payload_for_skills_endpoint(course_skills) | ||
|
||
# assign skills metadata to degreed course by first fetching degreed course id | ||
try: | ||
degreed_client.assign_course_skills(course_id, json_payload) | ||
except ClientError as error: | ||
LOGGER.error( | ||
generate_formatted_log( | ||
degreed_channel_config.channel_code(), | ||
degreed_channel_config.enterprise_customer.uuid, | ||
None, | ||
None, | ||
f'Degreed2APIClient assign_course_skills failed for course {course_id} ' | ||
f'with message: {error.message}' | ||
) | ||
) | ||
except RequestException as error: | ||
LOGGER.error( | ||
generate_formatted_log( | ||
degreed_channel_config.channel_code(), | ||
degreed_channel_config.enterprise_customer.uuid, | ||
None, | ||
None, | ||
f'Degreed2APIClient request to assign skills failed with message: {error.message}' | ||
) | ||
) |