create_forms.py 2.77 KB
#-*- coding:utf-8 -*-

from django.db import connection, transaction
from django.core.management.base import BaseCommand


class Command(BaseCommand):
    args = 'none'
    help = 'Creates forms for filtering'

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


def create_forms():
    cursor = connection.cursor()

    transaction.commit_unless_managed()
    transaction.enter_transaction_management()
    transaction.managed()

    cursor.execute('''truncate dictionary_lexemeform''')
    # ew. można odfiltrować te z wygenerowanymi?
    select_query = '''
select distinct l.id as lexeme_id, tc.prefix||o.rdzen||z.zak||tc.suffix as form
    from leksemy l
    join odmieniasie o on l.id = o.l_id
    join wzory w on (o.w_id = w.id)
    join dictionary_tabletemplate_pattern_types tt_pt on
        w.typ = tt_pt.patterntype_id
    join dictionary_tabletemplate tt on
        (tt_pt.tabletemplate_id = tt.id)
    join dictionary_tabletemplate_parts_of_speech tt_pos on
        (tt.id = tt_pos.tabletemplate_id and
            l.pos = tt_pos.partofspeech_id)
    join dictionary_tablecell tc on tt.id = tc.table_template_id
    join dictionary_tablecell_pattern_types tc_pt on
        (tc.id = tc_pt.tablecell_id and w.typ = tc_pt.patterntype_id)
    left join dictionary_tablecell_genders tc_g on
        tc.id = tc_g.tablecell_id
    join zakonczenia z on
        (o.w_id = z.w_id and tc.base_form_label_id = z.efobaz)
where true = all (
        select attr_val.id in (select lexemeattributevalue_id from
                dictionary_tabletemplate_attribute_values tt_attr_val
            where tt_attr_val.tabletemplate_id = tt.id)
        from dictionary_lexemeav lav
            join dictionary_lexemeattributevalue attr_val
                on lav.attribute_value_id = attr_val.id
            join dictionary_tabletemplate_attributes tt_attr
                on (attr_val.attribute_id = tt_attr.lexemeattribute_id and
                    tt.id = tt_attr.tabletemplate_id)
        where lav.lexeme_id = l.id) and
    true = all (
        select attr_val.id in (select lexemeattributevalue_id from
                dictionary_tablecell_attribute_values tc_attr_val
            where tc_attr_val.tablecell_id = tc.id)
        from dictionary_lexemeav lav
            join dictionary_lexemeattributevalue attr_val
                on lav.attribute_value_id = attr_val.id
            join dictionary_tabletemplate_cell_attributes tc_attr
                on (attr_val.attribute_id = tc_attr.lexemeattribute_id and
                    tt.id = tc_attr.tabletemplate_id)
        where lav.lexeme_id = l.id) and
    tt.variant_id = '1'
    ''' # jaki wariant?
    cursor.execute('''insert into dictionary_lexemeform (lexeme_id, form)
        (%s)''' % select_query)

    transaction.commit()
    transaction.leave_transaction_management()