Skip to content

Commit

Permalink
add a trailing slash to non contentfile OCW resources (#1877)
Browse files Browse the repository at this point in the history
* add a trailing slash to non contentfile OCW resources

* refactoring / simplification

* add a migration that ensures all OCW courses and runs have a trailing slash on their url
  • Loading branch information
gumaerc authored Dec 9, 2024
1 parent b61a01c commit 99e9ec7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
6 changes: 2 additions & 4 deletions learning_resources/etl/ocw.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,11 +470,9 @@ def extract_course(

log.info("Digesting %s...", url_path)

run_slug = url_path.strip("/")

return {
**course_json,
"last_modified": last_modified,
"slug": run_slug,
"url": urljoin(settings.OCW_BASE_URL, run_slug),
"slug": url_path.strip("/"),
"url": urljoin(settings.OCW_BASE_URL, url_path),
}
26 changes: 26 additions & 0 deletions learning_resources/migrations/0079_ocw_pages_trailing_slash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.2.16 on 2024-12-06 22:02

from django.db import migrations


def ensure_trailing_slashes(apps, schema_editor):
LearningResource = apps.get_model("learning_resources", "LearningResource")

for resource in LearningResource.objects.filter(
resource_type="course", offered_by_id="ocw"
):
resource.url = f"{resource.url.rstrip('/')}/"
resource.save()
for run in resource.runs.all():
run.url = f"{run.url.rstrip('/')}/"
run.save()


class Migration(migrations.Migration):
dependencies = [
("learning_resources", "0078_alter_contentfile_content_type"),
]

operations = [
migrations.RunPython(ensure_trailing_slashes, migrations.RunPython.noop),
]

0 comments on commit 99e9ec7

Please sign in to comment.