create_forms.py
1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#-*- coding:utf-8 -*-
from django.db import connection, transaction
from django.core.management.base import BaseCommand
from common.util import uniprint
from dictionary.lexeme_form_query import FROM_CLAUSES, value_combinations, attr_clauses_combinations
from dictionary.models import TableTemplate
class Command(BaseCommand):
args = 'none'
help = 'Creates forms for filtering'
def handle(self, **options):
create_forms()
VARIANT = '1'
def create_forms():
cursor = connection.cursor()
transaction.commit_unless_managed()
transaction.enter_transaction_management()
transaction.managed()
cursor.execute('''truncate dictionary_lexemeform''')
tts = TableTemplate.objects.filter(variant_id=VARIANT).prefetch_related(
'attributes__values', 'cell_attributes__values')
for tt in tts:
uniprint(u'creating forms for: %s' % tt.name)
attr_clauses, cell_attr_combinations, tt_attr_combinations = \
attr_clauses_combinations(tt)
for tt_c in tt_attr_combinations:
for cell_c in cell_attr_combinations:
select_query = '''
select distinct l.id as lexeme_id,
tc.prefix||o.rdzen||z.zak||tc.suffix as form
''' + FROM_CLAUSES + 'where ' + ' and '.join(attr_clauses)
cursor.execute('''
insert into dictionary_lexemeform (lexeme_id, form)
(%s)''' % select_query,
(tt.id,) + tt_c + cell_c + cell_c)
print 'commiting to database'
transaction.commit()
transaction.leave_transaction_management()