55import logging
66from typing import overload
77
8- from django .db import transaction
98from django .db .models import QuerySet
109from django .utils import timezone
1110from opaque_keys .edx .keys import CourseKey
1211from organizations .api import ensure_organization # type: ignore[import]
1312from organizations .api import exceptions as org_exceptions
1413
1514from .models import CatalogCourse , CatalogPathway , CourseRun , PathwayCategory , PathwayEnrollment
16- from .models .pathway_category import get_default_pathway_category
15+ from .models .pathway_category import DEFAULT_PATHWAY_CATEGORY_CODE
1716
1817log = logging .getLogger (__name__ )
1918
@@ -273,15 +272,31 @@ def delete_course_run(course_key: CourseKey) -> None:
273272# half; creating and versioning the *definition* of a Pathway is done through `openedx_learning.api`.
274273
275274
276- # `get_default_pathway_category` is part of this API too, and is re-exported via `__all__`. It's defined next to the
277- # model because the `CatalogPathway.category` field default needs it as well.
275+ def get_default_pathway_category () -> PathwayCategory :
276+ """
277+ Get the default `PathwayCategory`: the one `create_catalog_pathway` uses when it isn't given a category.
278+
279+ Rather than falling back to the word "Pathway" in code, the initial migration ships a database row with that title,
280+ so that operators can rename it or add categories of their own without a code change (see the openedx_learning
281+ ADR 0007). Renaming it keeps it the default, because only its ``title`` changes; its ``category_code`` can't.
282+
283+ Nothing ever creates this category except that migration. If an operator has deleted it, this raises
284+ `PathwayCategory.DoesNotExist`, and catalog pathways have to be given a category explicitly.
285+ """
286+ try :
287+ return PathwayCategory .objects .get (category_code = DEFAULT_PATHWAY_CATEGORY_CODE )
288+ except PathwayCategory .DoesNotExist as exc :
289+ raise PathwayCategory .DoesNotExist (
290+ f'The default pathway category ("{ DEFAULT_PATHWAY_CATEGORY_CODE } ") has been deleted, so catalog pathways '
291+ "must be given a category explicitly."
292+ ) from exc
278293
279294
280295def get_pathway_category (category_code : str ) -> PathwayCategory :
281296 """
282297 Get a `PathwayCategory` by its stable code.
283298
284- Its translations come with it, so ``category.localized_name `` costs no further queries, however often it's read.
299+ Its translations come with it, so ``category.localized_title `` costs no further queries, however often it's read.
285300
286301 ⚠️ Does not check permissions.
287302 """
@@ -292,7 +307,7 @@ def _catalog_pathways() -> QuerySet[CatalogPathway]:
292307 """
293308 Catalog pathways along with everything needed to show them: org, category, and the category's translations.
294309
295- With the translations prefetched, ``pathway.category.localized_name `` costs no further queries.
310+ With the translations prefetched, ``pathway.category.localized_title `` costs no further queries.
296311 """
297312 return CatalogPathway .objects .select_related ("org" , "category" ).prefetch_related ("category__translations" )
298313
@@ -314,7 +329,7 @@ def get_catalog_pathway(
314329 """
315330 Get a catalog pathway.
316331
317- Its org and category come with it, and so do the category's translations, so ``pathway.category.localized_name ``
332+ Its org and category come with it, and so do the category's translations, so ``pathway.category.localized_title ``
318333 costs no further queries.
319334
320335 ⚠️ Does not check permissions or visibility rules.
@@ -345,7 +360,7 @@ def get_catalog_pathways(
345360
346361 Both filters match exactly; an unknown org or category simply matches nothing. The result is a `QuerySet`, so that
347362 callers can narrow, search and paginate it further. Each pathway's org and category come with it, and so do the
348- category's translations, so ``pathway.category.localized_name `` costs no further queries.
363+ category's translations, so ``pathway.category.localized_title `` costs no further queries.
349364
350365 ⚠️ Does not check permissions or visibility rules. That suits authoring and administration, but a listing shown to
351366 learners will need the visibility logic described in the `openedx_catalog.api` docstring, which doesn't exist yet.
@@ -372,21 +387,17 @@ def create_catalog_pathway(
372387 """
373388 Create a `CatalogPathway`.
374389
375- The `Organization` identified by `org_code` must already exist. Pass `category=None` to use the default category.
390+ The `Organization` identified by `org_code` must already exist. Pass `category=None` to use the default category,
391+ which raises `PathwayCategory.DoesNotExist` if an operator has deleted it (see `get_default_pathway_category`).
376392
377393 This creates only the catalog half of a Pathway. Use `openedx_learning.api` to create the versioned content that
378394 implements it.
379395
380396 ⚠️ Does not check permissions.
381397 """
382- pathway = CatalogPathway (
383- pathway_code = pathway_code ,
384- title = title ,
385- description = description ,
386- # Only pass the category if given, so that the field default (which queries for the shipped category) runs
387- # only when it's actually needed.
388- ** ({"category" : category } if category is not None else {}),
389- )
398+ if category is None :
399+ category = get_default_pathway_category ()
400+ pathway = CatalogPathway (pathway_code = pathway_code , title = title , category = category , description = description )
390401 pathway .org_code = org_code # Resolves the Organization by short_name; raises Organization.DoesNotExist.
391402 pathway .save ()
392403 return pathway
@@ -427,10 +438,12 @@ def update_catalog_pathway(
427438
428439def delete_catalog_pathway (catalog_pathway : CatalogPathway | CatalogPathway .ID ) -> None :
429440 """
430- Delete a `CatalogPathway`, along with its enrollments .
441+ Delete a `CatalogPathway`.
431442
432- This will fail with a `ProtectedError` if any Pathway content still implements it, because that link is a `PROTECT`
433- foreign key on the content side.
443+ This fails with a `ProtectedError` while anyone is or ever was enrolled in it, including learners who have since
444+ unenrolled, so that deleting the wrong pathway can't wipe out enrollment history; delete those enrollments first if
445+ that's really intended. It also fails while any Pathway content still implements it, because that link is a
446+ `PROTECT` foreign key on the content side.
434447
435448 ⚠️ Does not check permissions.
436449 """
@@ -451,17 +464,16 @@ def enroll_in_pathway(user_id: int, catalog_pathway: CatalogPathway | CatalogPat
451464 Enrollment does not pin a content version: progress is always evaluated against whatever is published at the time,
452465 so that authoring changes reach learners who are already enrolled.
453466
467+ Raises `CatalogPathway.DoesNotExist` if given the ID of a pathway that doesn't exist.
468+
454469 ⚠️ Does not check permissions.
455470 """
456- pathway_id = catalog_pathway .id if isinstance (catalog_pathway , CatalogPathway ) else catalog_pathway
457- with transaction .atomic ():
458- # Lock the row so a concurrent unenroll can't slip in between reading `is_active` and writing it back.
459- enrollment , created = PathwayEnrollment .objects .select_for_update ().get_or_create (
460- user_id = user_id , catalog_pathway_id = pathway_id
461- )
462- if not created and not enrollment .is_active :
463- enrollment .is_active = True
464- enrollment .save (update_fields = ["is_active" , "modified" ])
471+ if not isinstance (catalog_pathway , CatalogPathway ):
472+ catalog_pathway = CatalogPathway .objects .get (pk = catalog_pathway )
473+ enrollment , created = PathwayEnrollment .objects .get_or_create (user_id = user_id , catalog_pathway = catalog_pathway )
474+ if not created and not enrollment .is_active :
475+ enrollment .is_active = True
476+ enrollment .save (update_fields = ["is_active" , "modified" ])
465477 return enrollment
466478
467479
@@ -495,7 +507,7 @@ def get_pathway_enrollments(user_id: int, *, include_inactive: bool = False) ->
495507
496508 Only active enrollments are returned unless ``include_inactive`` is set. Each enrollment's catalog pathway comes
497509 with it, along with the pathway's org, category and the category's translations, so a dashboard can show
498- ``enrollment.catalog_pathway.category.localized_name `` without further queries.
510+ ``enrollment.catalog_pathway.category.localized_title `` without further queries.
499511
500512 ⚠️ Does not check permissions or visibility rules.
501513 """
0 commit comments