lexeme_history.py
13.2 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# -*- coding: utf-8 -*-
from django.utils.encoding import force_unicode
from django.core.exceptions import ObjectDoesNotExist
from django.utils.translation import ugettext_lazy as _
from common.util import GroupDict
from dictionary.models import Lexeme, Inflection, InflectionCharacteristic, \
ClassificationValue, Qualifier, CrossReferenceType, LexemeAttributeValue, \
Gender, BorrowingSource
from history.models import History
from patterns.models import Pattern
attribute_translation_list = [
# Leksem
('leksemy', 'haslo', _(u'entry')),
('leksemy', 'haslosuf', _(u'entry suffix')),
('leksemy', 'glosa', _(u'gloss')),
('leksemy', 'nota', _(u'note')),
('leksemy', 'extended_note', _(u'extended note')),
('leksemy', 'wymowa', _(u'pronunciation')),
('leksemy', 'valence', _(u'valence')),
('leksemy', 'pos', _(u'part of speech')),
('leksemy', 'slownik', _(u'owner dictionary')),
('leksemy', 'status', _(u'status')),
('leksemy', 'komentarz', _(u'comment')),
('leksemy', 'specialist', _(u'specialist')),
('leksemy', 'borrowing_source_id', _(u'borrowing source')),
('leksemy', 'freq', _(u'occurrences')),
# Odmiana
('odmieniasie', 'oind', _(u'index')),
('odmieniasie', 'charfl', _(u'inflection characteristic')),
('odmieniasie', 'gender_id', _(u'gender')),
('odmieniasie', 'w_id', _(u'pattern')),
('odmieniasie', 'pronunciation', _(u'pronunciation')),
]
attribute_translation = dict(
((table, column), label)
for table, column, label in attribute_translation_list)
lexeme_attribute_order = [
_(u'entry'),
_(u'entry suffix'),
_(u'gloss'),
_(u'note'),
_(u'extended note'),
_(u'pronunciation'),
_(u'valence'),
_(u'part of speech'),
_(u'owner dictionary'),
_(u'status'),
_(u'comment'),
_(u'specialist'),
_(u'borrowing source'),
_(u'occurrences'),
# _(u'kwalifikator'),
# _(u'klasyfikacja'),
# _(u'slownik'),
# _(u'odsyłacz'),
]
inflection_attribute_order = [
_(u'index'),
_(u'inflection characteristic'),
_(u'gender'),
_(u'pattern'),
_(u'pronunciation'),
# _(u'kwalifikator'),
]
def get_lexeme_attr(attr, lexeme):
if attr == 'haslo':
return lexeme.entry
elif attr == 'haslosuf':
return lexeme.entry_suffix
elif attr == 'glosa':
return lexeme.gloss
elif attr == 'wymowa':
return lexeme.pronunciation
elif attr == 'nota':
return lexeme.note
elif attr == 'extended_note':
return lexeme.extended_note
elif attr == 'valence':
return lexeme.valence
elif attr == 'pos':
return lexeme.part_of_speech.symbol
elif attr == 'slownik':
return lexeme.owner_vocabulary.id
elif attr == 'status':
return lexeme.status
elif attr == 'komentarz':
return lexeme.comment
elif attr == 'borrowing_source_id':
if lexeme.borrowing_source:
return lexeme.borrowing_source.label
else:
return ''
elif attr == 'freq':
return lexeme.freq
def get_inflection_attr(attr, inflection):
if attr == 'oind':
return inflection.index
elif attr == 'gender_id':
return inflection.gender.symbol if inflection.gender else ''
elif attr == 'w_id':
return inflection.pattern.name
def prepare_value(column, value):
try:
if column == 'qualifier_id':
prepared = Qualifier.all_objects.get(id=int(value)).label # text
elif column == 'status':
prepared = dict(Lexeme.STATUS_CHOICES).get(value) # text
elif column == 'charfl':
prepared = InflectionCharacteristic.objects.get(
id=int(value)).symbol # text
elif column == 'gender_id':
prepared = Gender.objects.get(id=int(value)).symbol # text
elif column == 'w_id':
prepared = Pattern.all_objects.get(id=int(value)).name # text
elif column in ('classification_value_id', 'classificationvalue_id'):
cv = ClassificationValue.all_objects.get(id=int(value))
prepared = (cv.label, cv.classification.name) #(text_wartość, text_klasyfikacja)
elif column == 'attribute_value_id':
av = LexemeAttributeValue.objects.get(id=int(value))
prepared = (av.value or av.display_value, av.attribute.name) #(text_wartość, text_atrybut)
elif column == 'borrowing_source_id':
prepared = BorrowingSource.objects.get(id=int(value)).label #text
elif value == 'true':
prepared = _(u'yes')
elif value == 'false':
prepared = _(u'no')
else:
prepared = value
except ObjectDoesNotExist:
# prepared = None
if column in ('classification_value_id', 'classificationvalue_id', 'attribute_value_id'):
prepared = (force_unicode(_(u'<unavailable>')), force_unicode(_(u'<unavailable>')))
else:
prepared = None
except ValueError:
prepared = None
return prepared
def inflection_header(gender, pattern):
if gender:
return u'%s/%s ' % (gender.symbol, pattern.name)
else:
return pattern.name
def transaction_table(frame):
transaction_dict = {}
inflections = GroupDict()
extra_attributes = {}
classifications = {}
qualifiers = []
vocabs = []
crs = {}
inflection_qualifiers = {}
deleted = False
for item in frame.history_set.all():
table = item.table_name
column = item.column_name
if (table == 'leksemy' and column == 'usuniety' and
item.new_value == 'true' and item.old_value == 'false'):
deleted = True
attr = None
if (table, column) in attribute_translation:
attr = attribute_translation[(table, column)]
before, after = tuple(
prepare_value(column, v)
for v in (item.old_value, item.new_value))
before_after = (
before if item.operation != 'INSERT' else None,
after if item.operation != 'DELETE' else None)
if attr:
if table not in ('odmieniasie', 'kwalifikatory_odmieniasiow'):
transaction_dict[attr] = before_after
elif table == 'odmieniasie':
if item.row_id not in inflections:
inflections[item.row_id] = {}
inflections[item.row_id][attr] = before_after
if column == 'attribute_value_id':
if before:
value, attr = before
which = 0
else: # after
value, attr = after
which = 1
if attr not in extra_attributes:
extra_attributes[attr] = ([], [])
extra_attributes[attr][which].append(value)
if column in ('classification_value_id', 'classificationvalue_id'):
if before:
value, classification_name = before
which = 0
elif after: # after
value, classification_name = after
which = 1
if classification_name not in classifications:
classifications[classification_name] = ([], [])
classifications[classification_name][which].append(value)
if table == 'kwalifikatory_leksemow' and column == 'qualifier_id':
qualifiers.append(before_after)
if table == 'leksemy_w_slownikach' and column == 'slownik':
vocabs.append(before_after)
if table == 'odsylacze':
if item.row_id not in crs:
crs[item.row_id] = {}
crs[item.row_id][column] = before_after
if table == 'kwalifikatory_odmieniasiow':
if item.row_id not in inflection_qualifiers:
inflection_qualifiers[item.row_id] = {}
if column: # stare DELETE nie będą widoczne
inflection_qualifiers[item.row_id][column] = before_after
if deleted:
return deleted_lexeme_table(frame.lexeme)
rows = []
for attr in lexeme_attribute_order:
if attr in transaction_dict:
rows.append((attr, transaction_dict[attr]))
for attr, (before, after) in extra_attributes.iteritems():
rows.append(
(attr, (', '.join(before) or None, ', '.join(after) or None)))
for before_after in qualifiers:
rows.append((_(u'qualifier'), before_after))
for name, (before, after) in classifications.iteritems():
attr = _(u'classification: %s') % name
rows.append(
(attr, (', '.join(before) or None, ', '.join(after) or None)))
for before_after in vocabs:
rows.append((_(u'dictionary'), before_after))
for cr_data in crs.itervalues():
attr = _(u'cross-reference')
before_after = []
for i in (0, 1):
try:
if cr_data['l_id_do'][i] is not None:
l = Lexeme.all_objects.get(id=int(cr_data['l_id_do'][i]))
genders = l.inflection_data()['genders']
cr_type = CrossReferenceType.objects.get(
pk=cr_data['typods_id'][i])
prepared = ' '.join((cr_type.symbol, unicode(l), genders))
else:
prepared = None
except Lexeme.DoesNotExist:
prepared = None
before_after.append(prepared)
rows.append((attr, tuple(before_after)))
inflection_dict = GroupDict()
for inflection_id, inflection_data in inflections.iteritems():
for attr in inflection_attribute_order:
if attr in inflection_data:
inflection_dict.add(
inflection_id, (attr, inflection_data[attr]))
for q_data in inflection_qualifiers.itervalues():
if q_data: # stare DELETE...
attr = _(u'qualifier')
inflection_data = q_data.get(
'inflection_id', q_data.get('lexemeinflectionpattern_id'))
inflection_id = int(inflection_data[0] or inflection_data[1])
inflection_dict.add(inflection_id, (attr, q_data['qualifier_id']))
inflection_tables = []
for inflection_id, inflection_data in inflection_dict.iteritems():
inflection = Inflection.all_objects.filter(pk=inflection_id)
if inflection:
inflection = inflection[0]
header = inflection_header(inflection.gender, inflection.pattern)
else:
records = History.objects.filter(
operation='DELETE', table_name='odmieniasie',
row_id=inflection_id)
try:
gender_id = records.get(column_name='gender_id').old_value
pattern_id = records.get(column_name='w_id').old_value
gender = Gender.objects.get(id=gender_id)
pattern = Pattern.all_objects.get(id=pattern_id)
header = inflection_header(gender, pattern)
except ObjectDoesNotExist: # stare DELETE...
header = ''
header += force_unicode(_(u'(deleted)'))
inflection_tables.append((header, inflection_data))
return rows, inflection_tables
def deleted_lexeme_table(lexeme):
rows = []
for table, column, attr in attribute_translation_list:
if table == 'leksemy':
rows.append((
attr,
(prepare_value(
column, get_lexeme_attr(column, lexeme)), None)
))
for q in lexeme.qualifiers.all():
rows.append((_(u'qualifier'), (q.label, None)))
for classification in lexeme.owner_vocabulary.classifications.all():
attr = _(u'classification: %s') % classification.name
cvs = lexeme.classification_values(classification)
for cv in cvs:
rows.append((attr, (cv.label, None)))
for vocab in lexeme.vocabularies.all():
rows.append((_(u'dictionary'), (vocab.id, None)))
for cr in lexeme.refs_to.all():
attr = _(u'cross-reference')
rows.append((attr, (
' '.join([
cr.type.symbol, cr.to_lexeme.entry,
cr.to_lexeme.inflection_data()['genders']]),
None)))
for av in lexeme.lexemeattributevalue_set.all():
attr = av.attribute.name
rows.append((attr, (av.value or av.display_value, None)))
inflection_tables = []
for inflection in lexeme.inflection_set.all():
inflection_data = []
for table, column, attr in attribute_translation_list:
if table == 'odmieniasie' and column != 'charfl':
inflection_data.append(
(attr, (get_inflection_attr(column, inflection), None)))
for q in inflection.qualifiers.all():
attr = _(u'qualifier')
inflection_data.append((attr, (q.label, None)))
header = inflection_header(inflection.gender, inflection.pattern)
inflection_tables.append((header, inflection_data))
return rows, inflection_tables
def lexeme_table(frame):
rows, inflection_tables = transaction_table(frame)
lexeme = frame.lexeme
if lexeme is None:
lexeme = _(u'(deleted)') # aktualnie niemożliwe
return {
'rows': rows,
'inflection_tables': inflection_tables,
'user': frame.user,
'date': frame.transaction_began,
'lexeme': lexeme,
}
def lexeme_tables(history_frames):
return (lexeme_table(frame) for frame in history_frames)