convert_tables.py 1.77 KB
# -*- coding: utf-8 -*-
from django.core.management import BaseCommand
from common.util import GroupDict
from dictionary.models import Cell, NewTableTemplate, LexicalClass, PatternType, NewCell


class Command(BaseCommand):
    args = 'none'
    help = 'blah'

    def handle(self, **options):
        convert_tables()

TABLE_TEMPLATES = [
    ('rzeczowniki', '1', ('subst', 'osc', 'skrs'), ('f', 'm', 'n', '0'), ()),
    ('pron', '1', ('subst',), ('z0', "z0'", 'z1', 'z1p', 'z2'), ()),
]

def convert_tables():
    for name, variant, poses, p_types, attrs in TABLE_TEMPLATES:
        cells = Cell.objects.filter(
            table_template__variant__id=variant,
            table_template__inflection_characteristic__part_of_speech__symbol__in=
                poses)
        if p_types:
            cells = cells.filter(
                table_template__pattern_type__symbol__in=p_types)
        # TODO attrs - zinterpretować jako charfle?
        new_template = NewTableTemplate(name=name, variant_id=variant)
        new_template.save()
        for pos in poses:
            new_template.parts_of_speech.add(pos) #add
        lexical_classes = LexicalClass.objects.filter(
            partofspeech__symbol__in=poses)
        pattern_types = PatternType.objects.filter(
            lexical_class__in=lexical_classes, symbol__in=p_types)
        for pt in pattern_types:
            new_template.pattern_types.add(pt) #add
        # TODO attrs
        cell_groups = GroupDict()
        for cell in cells:
            cell_groups.add(
                (cell.prefix, cell.base_form_label, cell.suffix), cell)
        for key, cell_group in cell_groups.iteritems():
            new_cell = NewCell(
                table_template=new_template, base_form_label=key[1],
                prefix=key[0], suffix=key[1])