# -*- coding: utf-8 -*-
# Generated by Django 1.10.1 on 2016-10-12 02:35
#
# This one adds good high-rez images to the dataset,
#  Removes bad images from hard disk
# 
#
#################################################
from __future__ import unicode_literals

from django.db import migrations
from os.path import isfile
from os import remove
from ..models import Character

def get_hi_rez(charstr: str) -> str:
    char_split = charstr.split('.')
    char_hirez = char_split[0] + '.hi-rez.' + char_split[1]
    return char_hirez

def fix_nulls(apps) -> None:
    Page = apps.get_model("calligraphy", "Page")
    Char = apps.get_model("calligraphy", "Character")
    pages = Page.objects.all()
    for page in pages:
        if page.i_valid_transform is not None:
            chars = Char.objects.filter(parent_page=page)
            if page.i_valid_transform:
                for char in chars:
                    char.image_high_rez = get_hi_rez(str(char.image))
                    char.save()
            else:
                for char in chars:
                    char_hz = get_hi_rez(str(char.image))
                    if isfile(char_hz):
                        remove(char_hz)



def do_stuff(apps, schemd_editor) -> None:
    fix_nulls(apps)

class Migration(migrations.Migration):

    dependencies = [
        ('calligraphy', '0027_auto_20161011_1859'),
    ]

    operations = [ migrations.RunPython(do_stuff)
    ]
