cache_form_qualifiers.py 3.09 KB
#-*- coding:utf-8 -*-

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


class Command(BaseCommand):
    help = 'Cache form qualifiers 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_lexemeformqualifier''')
    select_query = '''
select distinct l.id as lexeme_id, kz.qualifier_id as qualifier_id
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)
    join kwalifikatory_zakonczen kz on
        (z.id = kz.ending_id)
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'
union
select l.id as lexeme_id, kl.qualifier_id as qualifier_id
from leksemy l
    join kwalifikatory_leksemow kl on l.id = kl.lexeme_id
union
select o.l_id as lexeme_id, ko.qualifier_id as qualifier_id
from odmieniasie o
    join kwalifikatory_odmieniasiow ko on o.id = ko.lexemeinflectionpattern_id
    ''' # jaki wariant?
    cursor.execute('''
        insert into dictionary_lexemeformqualifier (lexeme_id, qualifier_id)
        (%s)''' % select_query)

    transaction.commit()
    transaction.leave_transaction_management()