# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2017-12-24 06:44
from __future__ import unicode_literals

from django.db import migrations


def create_classifications(apps, schema_editor):
    from ..models import Classification as RealClassification  # Use treebeard API

    Classification = apps.get_model('publicbody', 'Classification')
    PublicBody = apps.get_model('publicbody', 'PublicBody')
    classifications = {}
    for pb in PublicBody.objects.exclude(classification_slug=''):
        if pb.classification_slug in classifications:
            pb.classification = classifications[pb.classification_slug]
        else:
            root = Classification(
                name=pb.classification_name,
                slug=pb.classification_slug,
                depth=1,
                path=RealClassification._get_path(None, 1, len(classifications))
            )
            root.save()
            pb.classification = root
            classifications[pb.classification_slug] = root
        pb.save()
    RealClassification.fix_tree()


class Migration(migrations.Migration):

    dependencies = [
        ('publicbody', '0006_auto_20171224_0732'),
    ]

    operations = [
        migrations.RunPython(create_classifications),
    ]

# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2017-12-24 06:44
from __future__ import unicode_literals

from django.db import migrations


def create_classifications(apps, schema_editor):
    from ..models import Classification  # Use treebeard API
    # Classification = apps.get_model('publicbody', 'Classification')
    PublicBody = apps.get_model('publicbody', 'PublicBody')
    classifications = {}
    for pb in PublicBody.objects.exclude(classification_slug=''):
        if pb.classification_slug in classifications:
            pb.classification = classifications[pb.classification_slug]
        else:
            root = Classification.add_root(
                name=pb.classification_name,
                slug=pb.classification_slug
            )
            pb.classification = root
            classifications[pb.classification_slug] = root
        pb.save()


class Migration(migrations.Migration):

    dependencies = [
        ('publicbody', '0006_auto_20171224_0732'),
    ]

    operations = [
        migrations.RunPython(create_classifications),
    ]

--- 
+++ 
@@ -6,21 +6,26 @@
 
 
 def create_classifications(apps, schema_editor):
-    from ..models import Classification  # Use treebeard API
-    # Classification = apps.get_model('publicbody', 'Classification')
+    from ..models import Classification as RealClassification  # Use treebeard API
+
+    Classification = apps.get_model('publicbody', 'Classification')
     PublicBody = apps.get_model('publicbody', 'PublicBody')
     classifications = {}
     for pb in PublicBody.objects.exclude(classification_slug=''):
         if pb.classification_slug in classifications:
             pb.classification = classifications[pb.classification_slug]
         else:
-            root = Classification.add_root(
+            root = Classification(
                 name=pb.classification_name,
-                slug=pb.classification_slug
+                slug=pb.classification_slug,
+                depth=1,
+                path=RealClassification._get_path(None, 1, len(classifications))
             )
+            root.save()
             pb.classification = root
             classifications[pb.classification_slug] = root
         pb.save()
+    RealClassification.fix_tree()
 
 
 class Migration(migrations.Migration):
Fix pb migration, by faking treebeard