# -*- coding: utf-8 -*-
# Generated by Django 1.10.6 on 2017-06-21 13:28
from __future__ import unicode_literals

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

def add_fs_examples(apps, schema_editor):
    Sound = apps.get_model('datasets', 'Sound')
    TaxonomyNode = apps.get_model('datasets', 'TaxonomyNode')
    Taxonomy = apps.get_model('datasets', 'Taxonomy')
    all_taxonomies = Taxonomy.objects.all()

    # loop on all taxonomy
    for taxonomy in all_taxonomies:
        all_taxonomy_nodes = taxonomy.taxonomynode_set.all()
        #loop for adding example Sound relations
        for taxonomy_node in all_taxonomy_nodes:
            for freesound_id in taxonomy.data[taxonomy_node.node_id]['positive_examples_FS']:
                try:
                    sound = Sound.objects.get(freesound_id=freesound_id)
                    taxonomy_node.freesound_examples.add(sound)
                except ObjectDoesNotExist:
                    continue


class Migration(migrations.Migration):

    dependencies = [
        ('datasets', '0018_auto_20170620_1305'),
    ]

    operations = [migrations.RunPython(add_fs_examples, migrations.RunPython.noop),
    ]

Add migration for freesound examples