Skip to content

Commit

Permalink
Merge pull request #1027 from openedx/saleem-latif/ENT-6271
Browse files Browse the repository at this point in the history
ENT-6271: Added the ability to get enterprise portal URL in catalog report.
  • Loading branch information
saleem-latif authored Jan 28, 2025
2 parents 6983739 + 7be839e commit f7aa867
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
34 changes: 29 additions & 5 deletions enterprise_catalog/apps/api/v1/export_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
'Short Description',
'Number of courses',
'Associated Catalogs',
'Program UUID',
'URL',
]

CSV_COURSE_RUN_HEADERS = [
Expand Down Expand Up @@ -121,6 +123,7 @@
'subjects',
'subtitle',
'title',
'uuid',
]

DATE_FORMAT = "%Y-%m-%d"
Expand Down Expand Up @@ -149,7 +152,7 @@ def fetch_catalog_types(hit):
return [catalog for catalog in CATALOG_TYPES if catalog in hit.get('enterprise_catalog_query_titles')]


def program_hit_to_row(hit):
def program_hit_to_row(hit, use_learner_portal_url=False):
"""
Helper function to construct a CSV row according to a single Algolia result program hit.
"""
Expand All @@ -167,13 +170,21 @@ def program_hit_to_row(hit):
catalogs = fetch_catalog_types(hit)
csv_row.append(', '.join(catalogs))

uuid = hit.get('uuid')
csv_row.append(uuid)
if use_learner_portal_url and uuid:
csv_row.append(f'https://enterprise.edx.org/r/program/{uuid}')
else:
csv_row.append(hit.get('marketing_url'))

return csv_row


def _base_csv_row_data(hit):
""" Returns the formatted, shared attributes common across all course types. """
title = hit.get('title')
aggregation_key = hit.get('aggregation_key')
key = hit.get('key')
language = hit.get('language')
transcript_languages = ', '.join(hit.get('transcript_languages', []))
marketing_url = hit.get('marketing_url')
Expand Down Expand Up @@ -211,6 +222,7 @@ def _base_csv_row_data(hit):
'end_date': end_date,
'enroll_by': enroll_by,
'aggregation_key': aggregation_key,
'key': key,
'advertised_course_run_key': advertised_course_run_key,
'language': language,
'transcript_languages': transcript_languages,
Expand All @@ -227,7 +239,7 @@ def _base_csv_row_data(hit):
}


def course_hit_to_row(hit):
def course_hit_to_row(hit, use_learner_portal_url=False):
"""
Helper function to construct a CSV row according to a single Algolia result course hit.
"""
Expand Down Expand Up @@ -256,7 +268,13 @@ def course_hit_to_row(hit):
csv_row.append(row_data.get('content_price'))
csv_row.append(row_data.get('language'))
csv_row.append(row_data.get('transcript_languages'))
csv_row.append(row_data.get('marketing_url'))

key = row_data.get('key')
if use_learner_portal_url and key:
csv_row.append(f'https://enterprise.edx.org/r/course/{key}')
else:
csv_row.append(row_data.get('marketing_url'))

csv_row.append(row_data.get('short_description'))
csv_row.append(row_data.get('subjects'))
csv_row.append(row_data.get('advertised_course_run_key'))
Expand All @@ -275,7 +293,7 @@ def course_hit_to_row(hit):
return csv_row


def exec_ed_course_to_row(hit):
def exec_ed_course_to_row(hit, use_learner_portal_url=False):
"""
Helper function to construct a CSV row according to a single executive education course hit.
"""
Expand All @@ -291,7 +309,13 @@ def exec_ed_course_to_row(hit):
csv_row.append(row_data.get('content_price'))
csv_row.append(row_data.get('language'))
csv_row.append(row_data.get('transcript_languages'))
csv_row.append(row_data.get('marketing_url'))

key = row_data.get('key')
if use_learner_portal_url and key:
csv_row.append(f'https://enterprise.edx.org/r/course/{key}')
else:
csv_row.append(row_data.get('marketing_url'))

csv_row.append(row_data.get('short_description'))
csv_row.append(row_data.get('subjects'))
csv_row.append(row_data.get('advertised_course_run_key'))
Expand Down
11 changes: 11 additions & 0 deletions enterprise_catalog/apps/api/v1/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,17 @@ def test_success(self, mock_algolia_client):
response = self.client.get(f'{url}?{facets}')
assert response.status_code == 200

@mock.patch('enterprise_catalog.apps.api.v1.views.catalog_workbook.get_initialized_algolia_client')
def test_use_learner_portal_url(self, mock_algolia_client):
"""
Tests basic, successful output
"""
mock_algolia_client.return_value.algolia_index.search.side_effect = [self.mock_algolia_hits, {'hits': []}]
url = self._get_contains_content_base_url()
facets = 'language=English&use_learner_portal_url=true'
response = self.client.get(f'{url}?{facets}')
assert response.status_code == 200


class EnterpriseCatalogContainsContentItemsTests(APITestMixin):
"""
Expand Down
7 changes: 4 additions & 3 deletions enterprise_catalog/apps/api/v1/views/catalog_workbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def get(self, request, **kwargs):
GET entry point for the `CatalogWorkbookView`
"""
facets = export_utils.querydict_to_dict(request.query_params)
use_learner_portal_url = facets.pop('use_learner_portal_url', False)
algoliaQuery = export_utils.facets_to_query(facets)

invalid_facets = export_utils.validate_query_facets(facets)
Expand Down Expand Up @@ -100,7 +101,7 @@ def get(self, request, **kwargs):
export_utils.write_headers_to_sheet(
exec_ed_worksheet, export_utils.CSV_EXEC_ED_COURSE_HEADERS, header_format
)
course_row = export_utils.exec_ed_course_to_row(hit)
course_row = export_utils.exec_ed_course_to_row(hit, use_learner_portal_url)
exec_ed_row_num = write_row_data(course_row, exec_ed_worksheet, exec_ed_row_num)
else:
if not edx_course_results_found:
Expand All @@ -109,7 +110,7 @@ def get(self, request, **kwargs):
export_utils.write_headers_to_sheet(
course_worksheet, export_utils.CSV_COURSE_HEADERS, header_format
)
course_row = export_utils.course_hit_to_row(hit)
course_row = export_utils.course_hit_to_row(hit, use_learner_portal_url)
course_row_num = write_row_data(course_row, course_worksheet, course_row_num)
for course_run in export_utils.course_hit_runs(hit):
if not course_run_results_found:
Expand All @@ -127,7 +128,7 @@ def get(self, request, **kwargs):
export_utils.write_headers_to_sheet(
program_worksheet, export_utils.CSV_PROGRAM_HEADERS, header_format
)
program_row = export_utils.program_hit_to_row(hit)
program_row = export_utils.program_hit_to_row(hit, use_learner_portal_url)
program_row_num = write_row_data(program_row, program_worksheet, program_row_num)
search_options['page'] = search_options['page'] + 1
page = algolia_client.algolia_index.search(algoliaQuery, search_options)
Expand Down

0 comments on commit f7aa867

Please sign in to comment.