# -*- coding: utf-8 -*-
# Generated by Django 1.11.23 on 2019-09-04 18:12
from __future__ import unicode_literals

from django.core.exceptions import ObjectDoesNotExist
from django.db import migrations
import django_extensions.db.fields


class Migration(migrations.Migration):

    def migrate_data_forward(apps, schema_editor):
        Course = apps.get_model('publisher', 'course')
        DiscoveryCourse = apps.get_model('course_metadata', 'course')
        for instance in Course.objects.all():
            # clear any existing slugs to ensure uniqueness
            instance.url_slug = ''
            # recreate organization() and partner() methods
            organization = instance.organizations.all().first()
            partner = organization.partner if organization else None
            key = '{org}+{number}'.format(org=instance.organizations.first().key, number=instance.number)
            try:
                discovery_course = DiscoveryCourse.everything.get(partner=partner, key=key, draft=False)
                instance.url_slug = discovery_course.url_slug
            except ObjectDoesNotExist:
                # no matching discovery course just means this one hasn't been published yet
                pass
            # will set the url_slug if not already set
            instance.save()

    dependencies = [
        ('publisher', '0080_remove_publisher_waffle_switches'),
        ('course_metadata', '0194_initialize_course_url_slug')
    ]

    operations = [
        migrations.RunPython(
            migrate_data_forward,
            migrations.RunPython.noop
        ),
        migrations.AlterField(
            model_name='course',
            name='url_slug',
            field=django_extensions.db.fields.AutoSlugField(blank=True, editable=False, help_text='Leave this field blank to have the value generated automatically.', populate_from='title', unique=True),
        ),
    ]

@@ -327,63 +327,8 @@
 r):%0A
-        Course = apps.get_model('publisher', 'course')%0A
     
@@ -346,16 +346,19 @@
 ryCourse
+Run
  = apps.
@@ -397,19 +397,23 @@
 urse
+Run
 ')%0A
+%0A
         
 for 
@@ -412,456 +412,707 @@
     
-for instance in Course.objects.all():%0A            # clear any existing slugs to ensure uniqueness%0A            instance.url_slug = ''%0A            # recreate organization() and partner() methods%0A            organization = instance.organizations.all().first()%0A            partner = organization.partner if organization else None%0A            key = '%7Borg%7D+%7Bnumber%7D'.format(org=instance.organizations.first().key, number=instance.number)%0A            try:
+# need to recreate generated properties since the model managers aren't serialized%0A        def get_discovery_course_run(course_run_instance):%0A            if course_run_instance.lms_course_id:%0A                return DiscoveryCourseRun.everything.filter(draft=False, key=course_run_instance.lms_course_id).first()%0A            else:%0A                return None%0A%0A        # find the course metadata counterpart by finding the counterpart of the most recent course run and extracting%0A        # the course object%0A        def get_discovery_course(course_instance):%0A            for course_run in course_instance.publisher_course_runs.all():%0A                # recreate discovery_counterpart generated property
 %0A   
@@ -1124,16 +1124,27 @@
         
+course_run_
 discover
@@ -1152,23 +1152,32 @@
 _cou
-rse = D
+nterpart = get_d
 iscovery
 Cour
@@ -1176,199 +1176,385 @@
 very
-C
+_c
 ourse
-.everything.get(partner=partner, key=key, draft=False)%0A                instance.url_slug = discovery_course.url_slug%0A            except ObjectDoesNotExist:%0A                # no matching
+_run(course_run)%0A                if course_run_discovery_counterpart:%0A                    return course_run_discovery_counterpart.course%0A            return None%0A%0A        Course = apps.get_model('publisher', 'course')%0A%0A        for instance in Course.objects.all():%0A            # clear any existing slugs to ensure uniqueness%0A            instance.url_slug = ''%0A           
  dis
@@ -1563,137 +1563,143 @@
 very
- 
+_
 course 
-just means this one hasn't been published yet%0A                pass%0A            # will set the url_slug if not already set
+= get_discovery_course(instance)%0A            if discovery_course:%0A                instance.url_slug = discovery_course.url_slug
 %0A   

fix publisher migration (#2145)