|
33 | 33 | ) |
34 | 34 | from cms.api import get_home_page |
35 | 35 | from courses.constants import UAI_COURSEWARE_ID_PREFIX |
36 | | -from courses.models import Course, CourseRun |
| 36 | +from courses.models import Course, CourseRun, Department |
37 | 37 | from ecommerce.constants import ( |
38 | 38 | DISCOUNT_TYPE_FIXED_PRICE, |
39 | 39 | PAYMENT_TYPE_SALES, |
@@ -76,27 +76,111 @@ def ensure_b2b_organization_index() -> OrganizationIndexPage: |
76 | 76 | return org_index_page |
77 | 77 |
|
78 | 78 |
|
79 | | -def import_and_create_contract_run(contract: ContractPage, course_run_id: str): |
| 79 | +def import_and_create_contract_run( # noqa: PLR0913 |
| 80 | + contract: ContractPage, |
| 81 | + course_run_id: str, |
| 82 | + departments: list[Department | str], |
| 83 | + *, |
| 84 | + live: bool = True, |
| 85 | + use_specific_course: str | None = None, |
| 86 | + create_depts: bool = False, |
| 87 | + block_countries: list[str] | None = None, |
| 88 | + create_cms_page: bool = True, |
| 89 | + publish_cms_page: bool = False, |
| 90 | + include_in_learn_catalog: bool = False, |
| 91 | + ingest_content_files_for_ai: bool = True, |
| 92 | + skip_edx: bool = False, |
| 93 | + require_designated_source_run: bool = False, |
| 94 | +): |
80 | 95 | """ |
81 | 96 | Create a contract run for the given course, importing it from edX if necessary. |
82 | 97 |
|
83 | | - Check for the specified course run. If it exists, create the contract run in |
84 | | - the usual fashion. If it doesn't, check for it in edX and import it into |
85 | | - MITx Online first, then create the contract run. |
86 | | -
|
87 | | - If the specified run is imported, it will have the "is_source_run" flag set. |
| 98 | + Wraps the create_contract_run function in some logic to check for the specified |
| 99 | + course, and then import it. If the course is imported from edX, this will set |
| 100 | + a few flags to reasonable values and call the import_courserun_from_edx |
| 101 | + function, then call create_contract_run using the imported run as the source |
| 102 | + course. If the course is already in the system, this just calls |
| 103 | + create_contract_run with the source run and the use_specific_course flag set |
| 104 | + to force it to use the specified run. |
| 105 | +
|
| 106 | + The kwargs combine the set from import_courserun_from_edx and |
| 107 | + create_contract_run. Some of the defaults are different, owning to the use case |
| 108 | + for this function. Those differences are: |
| 109 | + - You must pass in a list of departments. |
| 110 | + - By default, the live flag is set to True. |
| 111 | + - Learn AI ingestion will be set to True. |
| 112 | + - require_designated_source_run is set to False. (We assume you want the |
| 113 | + specified course, regardless of the source flag.) |
| 114 | +
|
| 115 | + In addition, if the course is imported, the corresponding run that is created |
| 116 | + for it will have the is_source_run flag set to True. This cannot be |
| 117 | + overridden. By using this function, we are assuming you really want the |
| 118 | + specified run as the source. |
| 119 | +
|
| 120 | + This won't pass a price to the import call, so the imported run won't have a |
| 121 | + product. This _will_ set a price on the contract run, because the contract |
| 122 | + run must have a price, even if that price is zero. The price will be whatever |
| 123 | + is specified in the contract (or zero). |
| 124 | +
|
| 125 | + Calling this function will result in a contract course run being created. If |
| 126 | + the course is imported, then you will end up with a course with two runs: |
| 127 | + the one that was imported and the new contract run. |
88 | 128 |
|
89 | 129 | Args: |
90 | 130 | contract (ContractPage): The contract to create the run for. |
91 | 131 | course_run_id (str): The readable ID for the source course run. |
92 | | - Keyword Args: |
| 132 | + departments (list[Department | str]): Departments to add to the new course. |
| 133 | + Keyword Args (passed to import_courserun_from_edx and create_contract_run): |
| 134 | + live (bool): Make the new course run live, and the course if one is created. |
| 135 | + use_specific_course (str|None): Readable ID of a specific course to use as the base course. |
| 136 | + create_depts (bool): Create departments. |
| 137 | + block_countries (list[str] | None): Country codes to add to the block list for the course. |
| 138 | + create_cms_page (bool): Create a CMS page for the course. Only applies if a course is being created. |
| 139 | + publish_cms_page (bool): Publish the new CMS page. Only takes effect if creating a CMS page. |
| 140 | + include_in_learn_catalog (bool): Set the "include_in_learn_catalog" flag on the new page. |
| 141 | + ingest_content_files_for_ai (bool): Set the "ingest_content_files_for_ai" flag on the new page. |
93 | 142 | skip_edx (bool): Don't try to create a course run in edX. |
94 | 143 | require_designated_source_run (bool): Require a flagged source run. |
95 | 144 | Returns: |
96 | 145 | CourseRun: The created CourseRun object. |
97 | 146 | Product: The created Product object. |
98 | 147 | """ |
99 | 148 |
|
| 149 | + run_qs = CourseRun.objects.filter(courseware_id=course_run_id) |
| 150 | + |
| 151 | + if run_qs.exists(): |
| 152 | + run = run_qs.get() |
| 153 | + else: |
| 154 | + from courses.api import import_courserun_from_edx |
| 155 | + |
| 156 | + rundata = import_courserun_from_edx( |
| 157 | + course_key=course_run_id, |
| 158 | + live=live, |
| 159 | + use_specific_course=use_specific_course, |
| 160 | + departments=departments, |
| 161 | + create_depts=create_depts, |
| 162 | + block_countries=block_countries, |
| 163 | + price=None, |
| 164 | + create_cms_page=create_cms_page, |
| 165 | + publish_cms_page=publish_cms_page, |
| 166 | + include_in_learn_catalog=include_in_learn_catalog, |
| 167 | + ingest_content_files_for_ai=ingest_content_files_for_ai, |
| 168 | + is_source_run=True, |
| 169 | + ) |
| 170 | + |
| 171 | + if not rundata: |
| 172 | + msg = f"Import and create contract run for {course_run_id} failed - could not import from edX." |
| 173 | + raise ValueError(msg) |
| 174 | + |
| 175 | + run = rundata[0] |
| 176 | + |
| 177 | + return create_contract_run( |
| 178 | + contract, |
| 179 | + run.course, |
| 180 | + skip_edx=skip_edx, |
| 181 | + require_designated_source_run=require_designated_source_run, |
| 182 | + ) |
| 183 | + |
100 | 184 |
|
101 | 185 | def create_contract_run( |
102 | 186 | contract: ContractPage, |
|
0 commit comments