Skip to content

Commit b7fced4

Browse files
feat: add a 'url_slug' property to CatalogCourse
1 parent 1c5643a commit b7fced4

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/openedx_catalog/admin.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ class CatalogCourseAdmin(admin.ModelAdmin):
2727
"""
2828

2929
list_filter = ["org__short_name", "language"]
30-
list_display = ["display_name", "org_display", "course_code", "runs_summary", "created_date", "language"]
30+
list_display = [
31+
"display_name",
32+
"org_display",
33+
"course_code",
34+
"runs_summary",
35+
"url_slug",
36+
"created_date",
37+
"language",
38+
]
3139

3240
def get_readonly_fields(self, request, obj: CatalogCourse | None = None) -> tuple[str, ...]:
3341
if obj: # editing an existing object

src/openedx_catalog/models/catalog_course.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,18 @@ def org_code(self, org_code: str) -> None:
178178
# not possible on MySQL, using the default collation used by the Organizations app.
179179
# So we do not worry about that possibility here.
180180

181+
@property
182+
def url_slug(self): # Do we need this? Would an opaque key be better?
183+
"""
184+
An ID that can be used to identify this catalog course in URLs or APIs.
185+
In the future, this may be an editable SlugField, so don't assume that
186+
it never changes.
187+
"""
188+
# '+' is a bad separator because it can mean " " in URLs.
189+
# '-', '.', and '_' cannot be used since they're allowed in the org code
190+
# So for now we use ':', and in the future we may make the whole slug customizable.
191+
return f"{self.org_code}:{self.course_code}"
192+
181193
def clean(self):
182194
"""Validate/normalize fields when edited via Django admin"""
183195
# Set a default value for display_name:

tests/openedx_catalog/test_models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ def test_course_code_required(self) -> None:
7474
# Using .update() will bypass all checks and defaults in save()/clean(), to see if the DB enforces this:
7575
CatalogCourse.objects.filter(pk=cc.pk).update(course_code="")
7676

77+
# url_slug field tests:
78+
def test_url_slug(self) -> None:
79+
"""Test that url_slug is generated automatically"""
80+
cc = CatalogCourse.objects.create(org_code="Org1", course_code="Python100")
81+
assert cc.url_slug == "Org1:Python100"
82+
7783
# display_name field tests:
7884

7985
def test_display_name_default(self) -> None:

0 commit comments

Comments
 (0)