# -*- coding: utf-8 -*-
# Generated by Django 1.10.3 on 2016-11-21 20:07
from __future__ import unicode_literals

from pathlib import Path
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


def generate_default_ocr_sets(apps, schema_editor):
    if not Ocr.objects.exists():
        return

    if not hasattr(settings, 'SNOOP_OCR_ROOT'):
        raise RuntimeError("SNOOP_OCR_ROOT is not set in the django config")
    old_root = Path(settings.SNOOP_OCR_ROOT)

    db_alias = schema_editor.connection.alias
    Ocr = apps.get_model('snoop', 'Ocr')
    Collection = apps.get_model('snoop', 'Collection')
    Document = apps.get_model('snoop', 'Document')

    default_collection = (
        Collection.objects
        .using(db_alias)
        .order_by('id')
        .first()
    )
    if not default_collection:
        raise RuntimeError("There are OCR objects but no Collection object!")

    tags = {ocr.tag for ocr in Ocr.objects.using(db_alias).distinct('tag')}
    for tag in tags:
        ocrDocument = Ocr.objects.using(db_alias).filter(tag=tag).first()
        try:
            document = (
                Document.objects
                .using(db_alias)
                .filter(md5=ocrDocument.md5)
                .first()
            )
            collection = document.collection
        except Document.NotFound:
            collection = default_collection
        collection.ocr[tag] = str(old_root / tag)
        collection.save()
        Ocr.objects.using(db_alias).filter(tag=tag).update(collection=collection)
    Ocr.objects.using(db_alias).filter(collection_id__isnull=True).update(collection_id=default_collection.id)

def do_nothing(*args):
    pass

class Migration(migrations.Migration):

    dependencies = [
        ('snoop', '0009_collection_ocr'),
    ]

    operations = [
        migrations.AddField(
            model_name='ocr',
            name='collection',
            field=models.ForeignKey(
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                related_name='ocr_documents',
                to='snoop.Collection'),
        ),
        migrations.RunPython(generate_default_ocr_sets, do_nothing),
        migrations.AlterField(
            model_name='ocr',
            name='collection',
            field=models.ForeignKey(
                null=False,
                on_delete=django.db.models.deletion.CASCADE,
                related_name='ocr_documents',
                to='snoop.Collection'),
        ),
        migrations.AlterUniqueTogether(
            name='ocr',
            unique_together=set([('collection', 'tag', 'md5')]),
        ),
    ]

@@ -517,54 +517,8 @@
 T)%0A%0A
-    db_alias = schema_editor.connection.alias%0A
     
@@ -719,33 +719,8 @@
 cts%0A
-        .using(db_alias)%0A
     
@@ -915,32 +915,16 @@
 objects.
-using(db_alias).
 distinct
@@ -987,32 +987,16 @@
 objects.
-using(db_alias).
 filter(t
@@ -1086,41 +1086,8 @@
 cts%0A
-                .using(db_alias)%0A
     
@@ -1385,32 +1385,16 @@
 objects.
-using(db_alias).
 filter(t
@@ -1451,24 +1451,8 @@
 cts.
-using(db_alias).
 filt

remove `db_alias`