Commit 062df08063536e4ed641e1c4ba0e7930222280fa
1 parent
c7e0aa53
zmienione tworzenie form do filtrowania
Showing
1 changed file
with
58 additions
and
41 deletions
dictionary/management/commands/create_forms.py
... | ... | @@ -2,6 +2,7 @@ |
2 | 2 | |
3 | 3 | from django.db import connection, transaction |
4 | 4 | from django.core.management.base import BaseCommand |
5 | +from dictionary.models import TableTemplate | |
5 | 6 | |
6 | 7 | |
7 | 8 | class Command(BaseCommand): |
... | ... | @@ -11,6 +12,17 @@ class Command(BaseCommand): |
11 | 12 | def handle(self, **options): |
12 | 13 | create_forms() |
13 | 14 | |
15 | +VARIANT = '1' | |
16 | + | |
17 | +def value_combinations(attributes): | |
18 | + if len(attributes) == 0: | |
19 | + yield () | |
20 | + return | |
21 | + combinations1 = value_combinations(attributes[1:]) | |
22 | + attr_values = attributes[0].values.all() | |
23 | + for c in combinations1: | |
24 | + for val in attr_values: | |
25 | + yield (val.id,) + c | |
14 | 26 | |
15 | 27 | def create_forms(): |
16 | 28 | cursor = connection.cursor() |
... | ... | @@ -20,52 +32,57 @@ def create_forms(): |
20 | 32 | transaction.managed() |
21 | 33 | |
22 | 34 | cursor.execute('''truncate dictionary_lexemeform''') |
23 | - # ew. można odfiltrować te z wygenerowanymi? | |
24 | - select_query = ''' | |
35 | + tts = TableTemplate.objects.filter(variant_id=VARIANT).prefetch_related( | |
36 | + 'attributes__values', 'cell_attributes__values') | |
37 | + for tt in tts: | |
38 | + print 'creating forms for: %s' % tt.name | |
39 | + tt_attrs = list(tt.attributes.all()) | |
40 | + cell_attrs = list(tt.cell_attributes.all()) | |
41 | + tt_attr_combinations = value_combinations(tt_attrs) | |
42 | + cell_attr_combinations = value_combinations(cell_attrs) | |
43 | + attr_clauses = [''' | |
44 | + %s in (select lav.attribute_value_id from dictionary_lexemeav lav | |
45 | + where lav.lexeme_id = l.id)''' | |
46 | + for _ in xrange(len(tt_attrs) + len(cell_attrs))] | |
47 | + attr_clauses += [''' | |
48 | + %s in (select lexemeattributevalue_id from | |
49 | + dictionary_tabletemplate_attribute_values tt_attr_val | |
50 | + where tt_attr_val.tabletemplate_id = tt.id)''' | |
51 | + for _ in tt_attrs] | |
52 | + attr_clauses += [''' | |
53 | + %s in (select lexemeattributevalue_id from | |
54 | + dictionary_tablecell_attribute_values tc_attr_val | |
55 | + where tc_attr_val.tablecell_id = tc.id)''' | |
56 | + for _ in cell_attrs] | |
57 | + if not attr_clauses: | |
58 | + attr_clauses = [' true'] | |
59 | + for tt_c in tt_attr_combinations: | |
60 | + for cell_c in cell_attr_combinations: | |
61 | + select_query = ''' | |
25 | 62 | select distinct l.id as lexeme_id, tc.prefix||o.rdzen||z.zak||tc.suffix as form |
26 | 63 | from leksemy l |
27 | 64 | join odmieniasie o on l.id = o.l_id |
28 | 65 | join wzory w on (o.w_id = w.id) |
29 | - join dictionary_tabletemplate_pattern_types tt_pt on | |
30 | - w.typ = tt_pt.patterntype_id | |
31 | - join dictionary_tabletemplate tt on | |
32 | - (tt_pt.tabletemplate_id = tt.id) | |
33 | - join dictionary_tabletemplate_parts_of_speech tt_pos on | |
34 | - (tt.id = tt_pos.tabletemplate_id and | |
66 | + join dictionary_tabletemplate tt | |
67 | + on tt.id = %s | |
68 | + join dictionary_tabletemplate_pattern_types tt_pt | |
69 | + on (w.typ = tt_pt.patterntype_id and tt.id = tt_pt.tabletemplate_id) | |
70 | + join dictionary_tabletemplate_parts_of_speech tt_pos | |
71 | + on (tt.id = tt_pos.tabletemplate_id and | |
35 | 72 | l.pos = tt_pos.partofspeech_id) |
36 | - join dictionary_tablecell tc on tt.id = tc.table_template_id | |
37 | - join dictionary_tablecell_pattern_types tc_pt on | |
38 | - (tc.id = tc_pt.tablecell_id and w.typ = tc_pt.patterntype_id) | |
39 | - left join dictionary_tablecell_genders tc_g on | |
40 | - tc.id = tc_g.tablecell_id | |
41 | - join zakonczenia z on | |
42 | - (o.w_id = z.w_id and tc.base_form_label_id = z.efobaz) | |
43 | -where true = all ( | |
44 | - select attr_val.id in (select lexemeattributevalue_id from | |
45 | - dictionary_tabletemplate_attribute_values tt_attr_val | |
46 | - where tt_attr_val.tabletemplate_id = tt.id) | |
47 | - from dictionary_lexemeav lav | |
48 | - join dictionary_lexemeattributevalue attr_val | |
49 | - on lav.attribute_value_id = attr_val.id | |
50 | - join dictionary_tabletemplate_attributes tt_attr | |
51 | - on (attr_val.attribute_id = tt_attr.lexemeattribute_id and | |
52 | - tt.id = tt_attr.tabletemplate_id) | |
53 | - where lav.lexeme_id = l.id) and | |
54 | - true = all ( | |
55 | - select attr_val.id in (select lexemeattributevalue_id from | |
56 | - dictionary_tablecell_attribute_values tc_attr_val | |
57 | - where tc_attr_val.tablecell_id = tc.id) | |
58 | - from dictionary_lexemeav lav | |
59 | - join dictionary_lexemeattributevalue attr_val | |
60 | - on lav.attribute_value_id = attr_val.id | |
61 | - join dictionary_tabletemplate_cell_attributes tc_attr | |
62 | - on (attr_val.attribute_id = tc_attr.lexemeattribute_id and | |
63 | - tt.id = tc_attr.tabletemplate_id) | |
64 | - where lav.lexeme_id = l.id) and | |
65 | - tt.variant_id = '1' | |
66 | - ''' # jaki wariant? | |
67 | - cursor.execute('''insert into dictionary_lexemeform (lexeme_id, form) | |
68 | - (%s)''' % select_query) | |
73 | + join dictionary_tablecell tc | |
74 | + on tt.id = tc.table_template_id | |
75 | + join dictionary_tablecell_pattern_types tc_pt | |
76 | + on (tc.id = tc_pt.tablecell_id and w.typ = tc_pt.patterntype_id) | |
77 | + left join dictionary_tablecell_genders tc_g | |
78 | + on tc.id = tc_g.tablecell_id | |
79 | + join zakonczenia z | |
80 | + on (o.w_id = z.w_id and tc.base_form_label_id = z.efobaz) | |
81 | +where''' + ' and '.join(attr_clauses) | |
82 | + cursor.execute(''' | |
83 | + insert into dictionary_lexemeform (lexeme_id, form) | |
84 | + (%s)''' % select_query, | |
85 | + (tt.id,) + tt_c + cell_c + tt_c + cell_c) | |
69 | 86 | |
70 | 87 | transaction.commit() |
71 | 88 | transaction.leave_transaction_management() |
... | ... |