0004_auto_20151210_2115.py 2.72 KB
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models

from accounts.util import bot_history


def adjsubst_crs(apps, schema_editor):
    bot_history()
    Lexeme = apps.get_model('dictionary', 'Lexeme')
    CrossReferenceType = apps.get_model('dictionary', 'CrossReferenceType')
    CrossReference = apps.get_model('dictionary', 'CrossReference')
    LexemeAttribute = apps.get_model('dictionary', 'LexemeAttribute')
    LexemeAttributeValue = apps.get_model('dictionary', 'LexemeAttributeValue')
    LexemeAV = apps.get_model('dictionary', 'LexemeAV')
    adjsubst, created = CrossReferenceType.objects.get_or_create(
        symbol='adjsubst', desc=u'od rzeczownika', index=30,
        from_pos_id='adj', to_pos_id='subst')
    substadj, created = CrossReferenceType.objects.get_or_create(
        symbol='substadj', desc=u'przymiotnik', index=30,
        from_pos_id='subst', to_pos_id='adj')

    rzeczownik = LexemeAttribute.objects.get(name='rzeczownik')
    nouns = LexemeAttributeValue.objects.filter(attribute=rzeczownik)
    empty_value = nouns.get(value=u'')

    def make_references(adj, subst):
        CrossReference.objects.get_or_create(
            from_lexeme=adj, to_lexeme=subst, type=adjsubst)
        CrossReference.objects.get_or_create(
            from_lexeme=subst, to_lexeme=adj, type=substadj)

    for noun in nouns.exclude(value__contains=' '):
        matching = Lexeme.objects.filter(
            entry=noun.value, part_of_speech='subst')
        if len(matching) == 1:
            match = matching.get()
            for l in noun.lexemes.all():
                make_references(l, match)
            LexemeAV.objects.filter(attribute_value=noun).update(
                attribute_value=empty_value)

    for multinoun in nouns.filter(value__regex='^\w+(; \w+)+$'):
        bad_entries = []
        entries = multinoun.value.split('; ')
        for entry in entries:
            matching = Lexeme.objects.filter(
                entry=entry, part_of_speech='subst')
            if len(matching) == 1:
                match = matching.get()
                for l in multinoun.lexemes.all():
                    make_references(l, match)
            else:
                bad_entries.append(entry)
        if bad_entries != entries:
            new_value = '; '.join(bad_entries)
            val, created = LexemeAttributeValue.objects.get_or_create(
                value=new_value, attribute=rzeczownik)
            LexemeAV.objects.filter(attribute_value=multinoun).update(
                attribute_value=val)


class Migration(migrations.Migration):

    dependencies = [
        ('dictionary', '0003_auto_20151202_1539'),
    ]

    operations = [
        migrations.RunPython(adjsubst_crs),
    ]