Commit 946e73f4 by Clinton Blackburn

Updated CourseRun.slug field

- The value is no longer auto-generated. Instead it is imported from the marketing site.
- The marketing_url no longer assumes a slug is present.
parent 449d57e3
...@@ -400,18 +400,12 @@ class CourseMarketingSiteDataLoader(AbstractMarketingSiteDataLoader): ...@@ -400,18 +400,12 @@ class CourseMarketingSiteDataLoader(AbstractMarketingSiteDataLoader):
} }
try: try:
course_run, created = CourseRun.objects.update_or_create(key__iexact=key, defaults=defaults) course_run, __ = CourseRun.objects.update_or_create(key__iexact=key, defaults=defaults)
except TypeError: except TypeError:
# TODO Fix the data in Drupal (ECOM-5304) # TODO Fix the data in Drupal (ECOM-5304)
logger.error('Multiple course runs are identified by the key [%s] or UUID [%s].', key, uuid) logger.error('Multiple course runs are identified by the key [%s] or UUID [%s].', key, uuid)
return None return None
# NOTE (CCB): The AutoSlug field kicks in at creation time. We need to apply overrides in a separate
# operation.
if created:
course_run.slug = slug
course_run.save()
self.set_course_run_staff(course_run, data) self.set_course_run_staff(course_run, data)
self.set_course_run_transcript_languages(course_run, data) self.set_course_run_transcript_languages(course_run, data)
......
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
def clear_slug_values(apps, schema_editor):
"""
Clears all data in the CourseRun.slug column.
The data is invalid, and needs to be removed so that it can be refreshed.
"""
CourseRun = apps.get_model('course_metadata', 'CourseRun')
CourseRun.objects.all().update(slug=None)
class Migration(migrations.Migration):
dependencies = [
('course_metadata', '0022_remove_duplicate_courses'),
]
operations = [
migrations.AlterField(
model_name='courserun',
name='slug',
field=models.CharField(max_length=255, db_index=True, blank=True, null=True),
),
migrations.AlterField(
model_name='historicalcourserun',
name='slug',
field=models.CharField(max_length=255, db_index=True, blank=True, null=True),
),
migrations.RunPython(clear_slug_values, reverse_code=migrations.RunPython.noop),
]
...@@ -355,15 +355,18 @@ class CourseRun(TimeStampedModel): ...@@ -355,15 +355,18 @@ class CourseRun(TimeStampedModel):
syllabus = models.ForeignKey(SyllabusItem, default=None, null=True, blank=True) syllabus = models.ForeignKey(SyllabusItem, default=None, null=True, blank=True)
card_image_url = models.URLField(null=True, blank=True) card_image_url = models.URLField(null=True, blank=True)
video = models.ForeignKey(Video, default=None, null=True, blank=True) video = models.ForeignKey(Video, default=None, null=True, blank=True)
slug = AutoSlugField(populate_from='key', editable=True) slug = models.CharField(max_length=255, blank=True, null=True, db_index=True)
history = HistoricalRecords() history = HistoricalRecords()
@property @property
def marketing_url(self): def marketing_url(self):
if self.slug:
path = 'course/{slug}'.format(slug=self.slug) path = 'course/{slug}'.format(slug=self.slug)
return urljoin(self.course.partner.marketing_site_url_root, path) return urljoin(self.course.partner.marketing_site_url_root, path)
return None
@property @property
def title(self): def title(self):
return self.title_override or self.course.title return self.title_override or self.course.title
......
...@@ -123,7 +123,7 @@ class CourseRunFactory(factory.DjangoModelFactory): ...@@ -123,7 +123,7 @@ class CourseRunFactory(factory.DjangoModelFactory):
min_effort = FuzzyInteger(1, 10) min_effort = FuzzyInteger(1, 10)
max_effort = FuzzyInteger(10, 20) max_effort = FuzzyInteger(10, 20)
pacing_type = FuzzyChoice([name for name, __ in CourseRun.PACING_CHOICES]) pacing_type = FuzzyChoice([name for name, __ in CourseRun.PACING_CHOICES])
marketing_url = FuzzyText(prefix='https://example.com/test-course-url') slug = FuzzyText()
@factory.post_generation @factory.post_generation
def staff(self, create, extracted, **kwargs): def staff(self, create, extracted, **kwargs):
......
...@@ -11,6 +11,7 @@ from django.test import TestCase ...@@ -11,6 +11,7 @@ from django.test import TestCase
from freezegun import freeze_time from freezegun import freeze_time
from course_discovery.apps.core.models import Currency from course_discovery.apps.core.models import Currency
from course_discovery.apps.core.tests.helpers import make_image_file
from course_discovery.apps.core.utils import SearchQuerySetWrapper from course_discovery.apps.core.utils import SearchQuerySetWrapper
from course_discovery.apps.course_metadata.models import ( from course_discovery.apps.course_metadata.models import (
AbstractMediaModel, AbstractNamedModel, AbstractValueModel, AbstractMediaModel, AbstractNamedModel, AbstractValueModel,
...@@ -18,7 +19,6 @@ from course_discovery.apps.course_metadata.models import ( ...@@ -18,7 +19,6 @@ from course_discovery.apps.course_metadata.models import (
FAQ, SeatType FAQ, SeatType
) )
from course_discovery.apps.course_metadata.tests import factories from course_discovery.apps.course_metadata.tests import factories
from course_discovery.apps.core.tests.helpers import make_image_file
from course_discovery.apps.course_metadata.tests.factories import CourseRunFactory, ImageFactory from course_discovery.apps.course_metadata.tests.factories import CourseRunFactory, ImageFactory
from course_discovery.apps.ietf_language_tags.models import LanguageTag from course_discovery.apps.ietf_language_tags.models import LanguageTag
...@@ -162,6 +162,18 @@ class CourseRunTests(TestCase): ...@@ -162,6 +162,18 @@ class CourseRunTests(TestCase):
course_run = factories.CourseRunFactory(start=start, end=end) course_run = factories.CourseRunFactory(start=start, end=end)
self.assertEqual(course_run.availability, expected_availability) self.assertEqual(course_run.availability, expected_availability)
def test_marketing_url(self):
""" Verify the property constructs a marketing URL based on the marketing slug. """
expected = '{root}/course/{slug}'.format(root=self.course_run.course.partner.marketing_site_url_root.strip('/'),
slug=self.course_run.slug)
self.assertEqual(self.course_run.marketing_url, expected)
@ddt.data(None, '')
def test_marketing_url_with_empty_marketing_slug(self, slug):
""" Verify the property returns None if the CourseRun has no marketing_slug value. """
course_run = CourseRunFactory(slug=slug)
self.assertIsNone(course_run.marketing_url)
class OrganizationTests(TestCase): class OrganizationTests(TestCase):
""" Tests for the `Organization` model. """ """ Tests for the `Organization` model. """
...@@ -449,6 +461,7 @@ class ProgramTypeTests(TestCase): ...@@ -449,6 +461,7 @@ class ProgramTypeTests(TestCase):
class EndorsementTests(TestCase): class EndorsementTests(TestCase):
""" Tests of the Endorsement model. """ """ Tests of the Endorsement model. """
def setUp(self): def setUp(self):
super(EndorsementTests, self).setUp() super(EndorsementTests, self).setUp()
self.person = factories.PersonFactory() self.person = factories.PersonFactory()
...@@ -463,6 +476,7 @@ class EndorsementTests(TestCase): ...@@ -463,6 +476,7 @@ class EndorsementTests(TestCase):
class CorporateEndorsementTests(TestCase): class CorporateEndorsementTests(TestCase):
""" Tests of the CorporateEndorsement model. """ """ Tests of the CorporateEndorsement model. """
def setUp(self): def setUp(self):
super(CorporateEndorsementTests, self).setUp() super(CorporateEndorsementTests, self).setUp()
self.corporation_name = 'test org' self.corporation_name = 'test org'
...@@ -478,6 +492,7 @@ class CorporateEndorsementTests(TestCase): ...@@ -478,6 +492,7 @@ class CorporateEndorsementTests(TestCase):
class FAQTests(TestCase): class FAQTests(TestCase):
""" Tests of the FAQ model. """ """ Tests of the FAQ model. """
def test_str(self): def test_str(self):
question = 'test question' question = 'test question'
faq = FAQ.objects.create(question=question, answer='test') faq = FAQ.objects.create(question=question, answer='test')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment