# -*- coding: utf-8 -*-
# Generated by Django 1.9.7 on 2016-07-20 22:24
#
# Transforms character coordinates into usefull data
#
###############################################################################


from __future__ import unicode_literals

from django.db import migrations, models

def transform_coordinates(apps) -> None:
    Page = apps.get_model("calligraphy", "Page")
    Char = apps.get_model("calligraphy", "Character")
    pages = Page.objects.all()
    for page in pages:                                  # Not strictly necessary, but easier
        chars = Char.objects.filter(parent_page=page.id)
        for char in chars:
            if(page.image_type_httpRequest == "new"):
                char.x1 = char.x1 * 5
                char.x2 = char.x2 * 5
                char.y1 = char.y1 * 5
                char.y2 = char.y2 * 5
            else:                                       # (type=="old"")
                char.x1 = (char.x1 * 6.27) - (3 * page.image_width / 800)
                char.x2 = (char.x2 * 6.27) + (3 * page.image_width / 800)
                char.y1 = (char.y1 * 6.27) - (3 * page.image_width / 800)
                char.y2 = (char.y2 * 6.27) + (3 * page.image_width / 800)
            char.save()

def transform_data(apps, schemd_editor) -> None:
    transform_coordinates(apps)

class Migration(migrations.Migration):

    dependencies = [
        ('calligraphy', '0015_auto_20160719_0158'),
    ]

    operations = [ migrations.RunPython(transform_data)
    ]
