0004_auto_20160205_1329.py 1.65 KB
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


def migrate_frames(apps, schemaeditor):
    History = apps.get_model('history', 'History')
    HistoryFrame = apps.get_model('history', 'HistoryFrame')
    frame_keys = {}
    for history_item in History.objects.exclude(lexeme=None):
        tb = history_item.transaction_began
        lexeme = history_item.lexeme
        key = (tb, lexeme)
        user = history_item.user
        if key in frame_keys:
            frame_user, frame = frame_keys[key]
            assert frame_user == user
        else:
            frame = HistoryFrame.objects.create(
                transaction_began=tb,
                lexeme=lexeme,
                user=user)
            frame_keys[key] = (user, frame)
        history_item.frame_id = frame
        history_item.save()
    frame_keys = {}
    for history_item in History.objects.exclude(pattern=None):
        tb = history_item.transaction_began
        pattern = history_item.pattern
        key = (tb, pattern)
        user = history_item.user
        if key in frame_keys:
            frame_user, frame = frame_keys[key]
            assert frame_user == user
        else:
            frame = HistoryFrame.objects.create(
                transaction_began=tb,
                pattern=pattern,
                user=user)
            frame_keys[key] = (user, frame)
        history_item.frame_id = frame
        history_item.save()


class Migration(migrations.Migration):

    dependencies = [
        ('history', '0003_auto_20160122_1651'),
    ]

    operations = [
        migrations.RunPython(migrate_frames, lambda x, y: None),
    ]