# Generated by Django 2.0.8 on 2018-09-20 20:06
import logging

from django.db import migrations


def convert_slugs_to_references(apps, schema_editor):
    Project = apps.get_model("concordia", "Project")
    CampaignTaskDetails = apps.get_model("importer", "CampaignTaskDetails")

    for ctd in CampaignTaskDetails.objects.all():
        try:
            ctd.project = Project.objects.get(
                slug=ctd.project_slug, campaign__slug=ctd.campaign_slug
            )
            ctd.save()
        except Project.DoesNotExist:
            logging.error("%s references a non-existent project! Deleting it!", ctd)
            ctd.delete()


class Migration(migrations.Migration):

    dependencies = [("importer", "0008_campaigntaskdetails_project")]

    operations = [migrations.RunPython(convert_slugs_to_references)]
