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),
    ]

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),
    ]

Fix pb migration, by faking treebeard
Fix pb migration, by faking treebeard
  from __future__ import unicode_literals
  
  from django.db import migrations
  
  
  def create_classifications(apps, schema_editor):
-     from ..models import Classification  # Use treebeard API
+     from ..models import Classification as RealClassification  # Use treebeard API
+ 
-     # Classification = apps.get_model('publicbody', 'Classification')
+     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):
  
      dependencies = [
          ('publicbody', '0006_auto_20171224_0732'),
      ]
  
      operations = [
          migrations.RunPython(create_classifications),
      ]
  
Fix pb migration, by faking treebeard
## Code Before:
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),
    ]

## Instruction:
Fix pb migration, by faking treebeard
## Code After:
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),
    ]
