<commit_before># -*- 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 hasattr(settings, 'SNOOP_OCR_ROOT'):
        return
    old_root = 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')

    try:
        default_collection = (
            Collection.objects
            .using(db_alias)
            .order_by('id')
            .first()
        )
    except Collection.NotFound:
        return

    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
        path = Path(old_root) / tag
        collection.ocr[tag] = str(path.resolve())
        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 = [<commit_msg>`first` returns None, so check for it instead<commit_after># -*- 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 hasattr(settings, 'SNOOP_OCR_ROOT'):
        return
    old_root = 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:
        return

    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
        path = Path(old_root) / tag
        collection.ocr[tag] = str(path.resolve())
        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 = [
{'fraction_non_alphanumeric': 0.1087689713322091, 'fraction_numerical': 0.012928611579539067, 'mean_word_length': 2.5985844287158746, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2001340', 'n_tokens_mistral': 1110, 'n_tokens_neox': 1043, 'n_words': 207}