ajax_lemma_status.py
38.4 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
# -*- coding: utf-8 -*-
#Copyright (c) 2012, Bartłomiej Nitoń
#All rights reserved.
#Redistribution and use in source and binary forms, with or without modification, are permitted provided
#that the following conditions are met:
# Redistributions of source code must retain the above copyright notice, this list of conditions and
# the following disclaimer.
# Redistributions in binary form must reproduce the above copyright notice, this list of conditions
# and the following disclaimer in the documentation and/or other materials provided with the distribution.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import operator
from django.db.models import Q
from django.db.models import Count, Max
from accounts.models import RealizedLemma, RealizedPhraseology, RealizedSemantics
from common.decorators import render, ajax, AjaxError
from dictionary.common_func import frame_structure_exists
from dictionary.models import Configuration, Frame, Lemma, Lemma_Status, StatusChange
from semantics.change_log import backup_lemma_and_get_frames
from semantics.models import SemanticFrame
from semantics.utils import get_frames_differences
@render('lemma_status.html')
@ajax(method='get', encode_result=False)
def get_lemma_status(request, id):
if request.session.has_key('lemma_from_note_id') and request.session['lemma_from_note_id']:
id = request.session['lemma_from_note_id']
selected_lemma = Lemma.objects.get(id=id)
abort_status = None
next_statuses = []
pos = selected_lemma.entry_obj.pos
if ((selected_lemma.owner == request.user
and selected_lemma.status.type.sym_name != 'checked')
or request.user.has_perm('dictionary.confirm_lemma')
or (selected_lemma.vocabulary.editors.filter(id=request.user.id).exists()
and request.user.has_perm('dictionary.change_lemmas')
and selected_lemma.status.type.sym_name == 'initial')
or phraseologic_status_changes(request.user, selected_lemma)
or semantic_status_changes(request.user, selected_lemma)):
if request.user.groups.filter(group_settings__abort_statuses=selected_lemma.status.abort_status).exists():
abort_status = selected_lemma.status.abort_status
next_statuses = selected_lemma.status.next_statuses
if not lemma_can_be_temporary(selected_lemma):
next_statuses = next_statuses.exclude(status=u'zalążkowe')
next_statuses = next_statuses.filter(pk__in=request.user.groups.all()[0].group_settings.next_statuses.all())
next_statuses = next_statuses.all()
return {'lemma': selected_lemma,
'abort_status': abort_status,
'next_statuses': next_statuses,
'pos': pos}
def phraseologic_status_changes(user, selected_lemma):
phraseologic_change = False
if (user.has_perm('dictionary.add_phraseologic_frames') and
selected_lemma.status.type.sym_name != 'checked_f' and
selected_lemma.vocabulary.editors.filter(id=user.id).exists() and
(selected_lemma.phraseologist == user or selected_lemma.status.type.sym_name == 'checked')):
phraseologic_change = True
return phraseologic_change
def semantic_status_changes(user, selected_lemma):
semantic_change = False
if (user.has_perm('dictionary.add_semantic_frames') and
selected_lemma.status.type.sym_name != 'checked_s' and
selected_lemma.vocabulary.editors.filter(id=user.id).exists() and
(selected_lemma.semanticist == user or selected_lemma.status.type.sym_name == 'checked_f')):
semantic_change = True
return semantic_change
def lemma_can_be_temporary(lemma):
can_be_temporary = False
system_conf = Configuration.objects.get(selected_conf=True)
if (lemma.frequency_1M < system_conf.min_1M_freq and
lemma.frequency_300M < system_conf.min_300M_freq and
lemma.skladnica_frames.exists()):
can_be_temporary = True
return can_be_temporary
@ajax(method='post')
def status_need_validation(request, status_id, lemma_id):
next_status = False
lemma_status = Lemma.objects.get(id=lemma_id).status
new_status = Lemma_Status.objects.get(id=status_id)
if(new_status.priority > lemma_status.priority):
next_status = True
need_validation = (new_status.validate or
new_status.check_examples or
new_status.check_semantics) and next_status
return {'need_validation': need_validation}
@ajax(method='post')
def lemma_status_change(request, status_id, lemma_id):
try:
lemma_obj = Lemma.objects.get(id=lemma_id, old=False)
except Lemma.DoesNotExist:
raise AjaxError('old version')
entry_obj = lemma_obj.entry_obj
# sprawdza czy uzytkownik moze modyfikowac hasla w danym slowniku
try:
lemma_obj.vocabulary.editors.get(username=request.user.username)
edit_vocabulary = True
except:
edit_vocabulary = False
changed = False
message = ''
new_status = None
try: # jesli zarezerowano haslo przyciskiem
new_status = Lemma_Status.objects.get(id=status_id)
except:
pass
actual_semantic_frames = SemanticFrame.objects.none()
next_status = False
if(new_status):
actual_semantic_frames = backup_lemma_and_get_frames(lemma_obj)
if(new_status and new_status.priority > lemma_obj.status.priority):
next_status = True
# reserve lemma
if(lemma_obj.status.type.sym_name == 'initial' and edit_vocabulary and
request.user.has_perm('dictionary.change_lemmas')):
lemma_obj.owner = request.user
changed = True
if not new_status:
new_status = lemma_obj.status.next_statuses.order_by('priority')[0]
# resign from lemma
elif(not next_status and
new_status and new_status.type.sym_name == 'initial'):
lemma_obj.owner = None
changed = True
# lemma type changed to ready or temporary
elif(new_status and new_status.type.sym_name == 'ready' and next_status):
if entry_obj.pos.tag == 'verb':
flat_frames_value = 4.5
else:
flat_frames_value = 3.5
update_lemma_stats_ready(lemma_obj, lemma_obj.owner, new_status, flat_frames_value)
changed = True
# lemma type changed to checked
elif(new_status and new_status.type.sym_name == 'checked' and next_status):
if lemma_obj.owner == request.user:
if lemma_obj.status.abort_status != new_status:
message = u'Nie można zatwierdzać hasła, którego jest się właścicielem.'
else:
if lemma_obj.status.abort_status != new_status:
if entry_obj.pos.tag == 'verb':
checked_frame_value = 1.0
corrected_frame_value = 7.0
bonus_factor = 1.2
else:
checked_frame_value = 1.0
corrected_frame_value = 6.0
bonus_factor = 1.0
update_lemma_stats_conf(lemma_obj, lemma_obj.owner, request.user, new_status,
checked_frame_value, corrected_frame_value, bonus_factor)
changed = True
# pobieranie hasla do obrobki frazeologicznej
elif(lemma_obj.status.type.sym_name == 'checked' and
edit_vocabulary and new_status.type.sym_name == 'edit_f'
and next_status):
lemma_obj.phraseologist = request.user
add_new_frames_to_phraseologic_propositions(lemma_obj)
changed = True
# porzucanie obrobki frazeologicznej hasla
elif(new_status and
lemma_obj.status.type.sym_name == 'edit_f' and
lemma_obj.status.abort_status == new_status):
lemma_obj.phraseologist = None
changed = True
# zmiana statusu hasla na gotowe frazeologicznie
elif(new_status and new_status.type.sym_name == 'ready_f'
and next_status):
new_phraseologic_frame_value = 7.0
proposed_phraseologic_frame_value = 2.0
update_lemma_stats_ready_f(lemma_obj, lemma_obj.phraseologist,
new_status, new_phraseologic_frame_value,
proposed_phraseologic_frame_value)
add_new_frames_to_phraseologic_propositions(lemma_obj)
changed = True
# zmiana statusu hasla na sprawdzone frazeologicznie
elif(new_status and
new_status and new_status.type.sym_name == 'checked_f'
and next_status):
checked_frame_value = 4.0
corrected_frame_value = 7.0
bonus = 0.5
update_lemma_stats_conf_f(lemma_obj, lemma_obj.phraseologist, request.user, new_status,
checked_frame_value, corrected_frame_value, bonus)
add_new_frames_to_phraseologic_propositions(lemma_obj)
changed = True
# pobieranie hasla do obrobki semantycznej
elif(lemma_obj.status.type.sym_name == 'checked_f' and
edit_vocabulary and new_status.type.sym_name == 'edit_s'
and next_status):
lemma_obj.semanticist = request.user
add_new_frames_to_phraseologic_propositions(lemma_obj)
changed = True
# porzucanie obrobki semantycznej hasla
elif(new_status and
lemma_obj.status.type.sym_name == 'edit_s' and
lemma_obj.status.abort_status == new_status):
lemma_obj.semanticist = None
changed = True
# zmiana statusu hasla na gotowe semantycznie
elif(new_status and new_status.type.sym_name == 'ready_s'
and next_status):
### naliczanie oplat za gotowosc semantyczna
frame_value = 8
update_sem_stats_ready_s(lemma_obj.entry_obj, actual_semantic_frames,
lemma_obj.semanticist, new_status, frame_value)
add_new_frames_to_phraseologic_propositions(lemma_obj)
changed = True
# zmiana statusu hasla na sprawdzone semantycznie
elif(new_status and new_status.type.sym_name == 'checked_s'
and next_status):
checked_frame_value = 0.0
corrected_frame_value = 0.0
bonus = 2.0
### naliczanie oplat za sprawdzenie i bonusow
update_sem_stats_conf_s(lemma_obj.entry_obj, actual_semantic_frames,
lemma_obj.semanticist, request.user, new_status,
checked_frame_value, corrected_frame_value, bonus)
add_new_frames_to_phraseologic_propositions(lemma_obj)
changed = True
# jak oznaczamy status jako "do usuniecia", to czyscimy oplaty za haslo
elif(new_status and new_status.type.sym_name == 'erase'
and next_status):
changed = True
erase_payments(lemma_obj)
elif(new_status):
changed = True
if changed and new_status != lemma_obj.status:
lemma_obj.status = new_status
lemma_obj.save()
status_change = StatusChange(act_owner=lemma_obj.owner,
changer=request.user,
lemma=lemma_obj,
status=new_status)
status_change.save()
status_change.semantic_frames.add(*actual_semantic_frames.all())
lemma_obj.status_history.add(status_change)
if new_status:
new_status_type = new_status.type.sym_name
else:
new_status_type = ''
return {'entry' : lemma_obj.entry,
'lemma_id' : lemma_obj.id,
'changed' : changed,
'message' : message,
'new_status_type': new_status_type,
'next_status' : next_status}
def erase_payments(lemma):
RealizedLemma.objects.filter(lemma__entry_obj=lemma.entry_obj).delete()
RealizedPhraseology.objects.filter(lemma__entry_obj=lemma.entry_obj).delete()
RealizedSemantics.objects.filter(entry=lemma.entry_obj).delete()
############## lexicography #######################
def update_lemma_stats_ready(lemma, lex, status, flat_frames_value):
"""Update user stats about properly created frames."""
ready_statuses = Lemma_Status.objects.filter(type__sym_name='ready')
q_ready_statuses = []
for ready_status in ready_statuses.all():
q_ready_statuses.append(Q(status=ready_status))
ready_lemmas = RealizedLemma.objects.filter(reduce(operator.or_, q_ready_statuses)).filter(lemma__entry=lemma.entry).order_by('-date')
ncounted_ready_lemmas = ready_lemmas.filter(counted=False)
# sprawdz czy dane haslo nie zostalo juz podliczone
if ncounted_ready_lemmas.exists() or not ready_lemmas.exists():
lex_dict = {'made_frames': 0,
'cash': 0.0}
for frame in lemma.frames.all():
flat_frames = float(frame.positions.annotate(num_args=Count('arguments')).aggregate(Max('num_args'))['num_args__max'])
lex_dict['made_frames'] += flat_frames
lex_dict['cash'] += flat_frames_value*flat_frames
# leksykograf nie został jeszcze oplacony za dane haslo, wiec zmien wartosc
if not ready_lemmas.exists():
lex_real_lemma = RealizedLemma(lemma=lemma, cash=lex_dict['cash'],
made_frames=lex_dict['made_frames'],
paid=False, status=status, bonus=False,
counted=False)
lex_real_lemma.save()
lex.user_stats.lemma_real_history.add(lex_real_lemma)
else:
to_update = RealizedLemma.objects.get(pk=ready_lemmas[0].pk)
to_update.lemma = lemma
to_update.cash = lex_dict['cash']
to_update.made_frames = lex_dict['made_frames']
to_update.status = status
to_update.save()
# leksykograf moze sie tez zmienic
old_lex = to_update.user_stats.all()[0].user # UWAGA: leksykograf tez moze sie zmienic
if old_lex != lex:
old_lex.user_stats.lemma_real_history.remove(to_update)
lex.user_stats.lemma_real_history.add(to_update)
def update_lemma_stats_conf(lemma, lex, superlex, status,
checked_frame_value, corrected_frame_value, bonus_factor):
"""Update user stats about properly created frames."""
ready_statuses = Lemma_Status.objects.filter(type__sym_name='ready')
q_ready_statuses = []
for ready_status in ready_statuses.all():
q_ready_statuses.append(Q(status=ready_status))
checked_statuses = Lemma_Status.objects.filter(type__sym_name='checked')
q_checked_statuses = []
for checked_status in checked_statuses.all():
q_checked_statuses.append(Q(status=checked_status))
checked_lemmas = RealizedLemma.objects.filter(reduce(operator.or_, q_checked_statuses)).filter(lemma__entry=lemma.entry,
bonus=False).order_by('-date')
ncounted_checked_lemmas = checked_lemmas.filter(counted=False)
# sprawdz czy dane haslo nie zsotalo juz podliczone
if ncounted_checked_lemmas.exists() or not checked_lemmas.exists():
ready_lemmas = RealizedLemma.objects.filter(reduce(operator.or_, q_ready_statuses)).filter(lemma__entry=lemma.entry).order_by('-date').all()
ready_lemma = ready_lemmas[len(ready_lemmas)-1].lemma
lex_dict = {'same_frames': 0,
'wrong_frames': 0,
'cash': 0.0}
superlex_dict = {'same_frames': [],
'redo_frames': [],
'cash': 0.0}
q_same_frames = []
for frame in ready_lemma.frames.all():
flat_frames = float(frame.positions.annotate(num_args=Count('arguments')).aggregate(Max('num_args'))['num_args__max'])
try:
same_frame = lemma.frames.get(text_rep=frame.text_rep)
superlex_dict['same_frames'].append(frame)
superlex_dict['cash'] += checked_frame_value
lex_dict['same_frames'] += flat_frames
bonus = flat_frames * bonus_factor*flat_frames**(1.0/3.0)
lex_dict['cash'] += bonus
q_same_frames.append(Q(text_rep=same_frame.text_rep))
except Frame.DoesNotExist:
lex_dict['wrong_frames'] += flat_frames
continue
new_frames = lemma.frames
if len(q_same_frames) > 0:
new_frames = new_frames.exclude(reduce(operator.or_, q_same_frames))
superlex_dict['redo_frames'] = new_frames.all()
for frame in superlex_dict['redo_frames']:
superlex_dict['cash'] += corrected_frame_value
# superleksykograf nie zostal jeszcze oplacony za dane haslo, zmien wartosc
if not checked_lemmas.exists():
superlex_real_lemma = RealizedLemma(lemma=lemma, cash=superlex_dict['cash'],
corr_frames=len(superlex_dict['redo_frames']),
ncorr_frames=len(superlex_dict['same_frames']),
paid=False, status=status, bonus=False,
counted=False)
superlex_real_lemma.save()
superlex.user_stats.lemma_real_history.add(superlex_real_lemma)
else:
to_update = RealizedLemma.objects.get(pk=checked_lemmas[0].pk)
to_update.lemma = lemma
to_update.cash = superlex_dict['cash']
to_update.corr_frames = len(superlex_dict['redo_frames'])
to_update.ncorr_frames = len(superlex_dict['same_frames'])
to_update.status = status
to_update.save()
# superleksykograf tylko klika wiec moze sie zmienic
old_superlex = to_update.user_stats.all()[0].user # UWAGA: leksykograf tez moze sie zmienic
if old_superlex != superlex:
old_superlex.user_stats.lemma_real_history.remove(to_update)
superlex.user_stats.lemma_real_history.add(to_update)
# hasla pochodzace z clarinu maja cash = 0.0 i nie nalezy ich bonusować
try:
RealizedLemma.objects.get(lemma__entry=lemma.entry,
status__type__sym_name='ready',
bonus=False, cash=0.0, paid=True,
counted=True)
except RealizedLemma.DoesNotExist:
try: # zmienianie bonusu
lex_real_lemma = RealizedLemma.objects.get(lemma__entry=lemma.entry,
status__type__sym_name=status.type.sym_name,
bonus=True)
if not lex_real_lemma.counted:
lex_real_lemma.lemma = lemma
lex_real_lemma.cash = lex_dict['cash']
lex_real_lemma.prop_frames = lex_dict['same_frames']
lex_real_lemma.wrong_frames = lex_dict['wrong_frames']
lex_real_lemma.status = status
lex_real_lemma.save()
old_lex = lex_real_lemma.user_stats.all()[0].user # UWAGA: leksykograf tez moze sie zmienic
if old_lex != lex:
old_lex.user_stats.lemma_real_history.remove(lex_real_lemma)
lex.user_stats.lemma_real_history.add(lex_real_lemma)
except RealizedLemma.DoesNotExist:
lex_real_lemma = RealizedLemma(lemma=lemma,
cash=lex_dict['cash'],
prop_frames=lex_dict['same_frames'],
wrong_frames=lex_dict['wrong_frames'],
paid=False, status=status,
bonus=True, counted=False)
lex_real_lemma.save()
lex.user_stats.lemma_real_history.add(lex_real_lemma)
######################## phraseology #############################
def add_new_frames_to_phraseologic_propositions(lemma):
entry_obj = lemma.entry_obj
phraseologic_frames = lemma.frames.filter(phraseologic=True).all()
phraseologic_propositions = entry_obj.phraseologic_propositions.all()
propositions_to_add = []
for frame in phraseologic_frames:
if not frame_structure_exists(frames=phraseologic_propositions,
searched_frame=frame):
propositions_to_add.append(frame)
entry_obj.phraseologic_propositions.add(*propositions_to_add)
def update_lemma_stats_ready_f(lemma, phraseologist, status,
new_phraseologic_frame_value, proposed_phraseologic_frame_value):
"""Update user stats about added phraseologic frames."""
ready_statuses = Lemma_Status.objects.filter(type__sym_name='ready_f')
q_ready_statuses = []
for ready_status in ready_statuses.all():
q_ready_statuses.append(Q(status=ready_status))
ready_lemmas = RealizedPhraseology.objects.filter(reduce(operator.or_, q_ready_statuses)).filter(lemma__entry=lemma.entry,
lemma__entry_obj__pos=lemma.entry_obj.pos).order_by('-date')
ncounted_ready_lemmas = ready_lemmas.filter(counted=False)
# sprawdz czy dane haslo nie zostalo juz podliczone
if ncounted_ready_lemmas.exists() or not ready_lemmas.exists():
phraseologist_dict = {'new_frames': 0,
'reused_frames': 0,
'cash': 0.0}
for frame in lemma.frames.filter(phraseologic=True).all():
if frame_structure_exists(frames=lemma.entry_obj.phraseologic_propositions.all(),
searched_frame=frame):
phraseologist_dict['reused_frames'] += 1
phraseologist_dict['cash'] += proposed_phraseologic_frame_value
else:
phraseologist_dict['new_frames'] += 1
phraseologist_dict['cash'] += new_phraseologic_frame_value
# leksykograf nie został jeszcze oplacony za dane haslo, wiec zmien wartosc
if not ready_lemmas.exists():
phrase_real_lemma = RealizedPhraseology(lemma=lemma,
cash=phraseologist_dict['cash'],
new_frames=phraseologist_dict['new_frames'],
reused_frames=phraseologist_dict['reused_frames'],
paid=False,
status=status,
bonus=False,
counted=False)
phrase_real_lemma.save()
phraseologist.user_stats.phraseology_real_history.add(phrase_real_lemma)
else:
to_update = RealizedPhraseology.objects.get(pk=ready_lemmas[0].pk)
to_update.lemma = lemma
to_update.cash = phraseologist_dict['cash']
to_update.new_frames = phraseologist_dict['new_frames']
to_update.reused_frames = phraseologist_dict['reused_frames']
to_update.status = status
to_update.save()
# frazeolog moze sie tez zmienic
old_phraseologist = to_update.user_stats.all()[0].user # UWAGA: frazeolog tez moze sie zmienic
if old_phraseologist != phraseologist:
old_phraseologist.user_stats.phraseology_real_history.remove(to_update)
phraseologist.user_stats.phraseology_real_history.add(to_update)
def update_lemma_stats_conf_f(lemma, phraseologist, superphraseologist, status,
checked_frame_value, corrected_frame_value, bonus):
"""Update user stats about properly created phraseologic frames."""
ready_statuses = Lemma_Status.objects.filter(type__sym_name='ready_f')
q_ready_statuses = []
for ready_status in ready_statuses.all():
q_ready_statuses.append(Q(status=ready_status))
checked_statuses = Lemma_Status.objects.filter(type__sym_name='checked_f')
q_checked_statuses = []
for checked_status in checked_statuses.all():
q_checked_statuses.append(Q(status=checked_status))
checked_lemmas = RealizedPhraseology.objects.filter(reduce(operator.or_, q_checked_statuses)).filter(lemma__entry_obj=lemma.entry_obj,
bonus=False).order_by('-date')
ncounted_checked_lemmas = checked_lemmas.filter(counted=False)
# sprawdz czy dane haslo nie zsotalo juz podliczone
if ncounted_checked_lemmas.exists() or not checked_lemmas.exists():
ready_lemmas = RealizedPhraseology.objects.filter(reduce(operator.or_, q_ready_statuses)).filter(lemma__entry_obj=lemma.entry_obj).order_by('-date').all()
ready_lemma = ready_lemmas[len(ready_lemmas)-1].lemma
phraseologist_dict = {'same_frames': 0,
'wrong_frames': 0,
'cash': 0.0}
superphraseologist_dict = {'same_frames': [],
'redo_frames': [],
'cash': 0.0}
q_same_frames = []
for frame in ready_lemma.frames.filter(phraseologic=True).all():
#flat_frames = float(frame.positions.annotate(num_args=Count('arguments')).aggregate(Max('num_args'))['num_args__max'])
try:
same_frame = lemma.frames.filter(phraseologic=True).get(text_rep=frame.text_rep)
superphraseologist_dict['same_frames'].append(frame)
superphraseologist_dict['cash'] += checked_frame_value
phraseologist_dict['same_frames'] += 1
phraseologist_dict['cash'] += bonus
q_same_frames.append(Q(text_rep=same_frame.text_rep))
except Frame.DoesNotExist:
phraseologist_dict['wrong_frames'] += 1
continue
new_frames = lemma.frames.filter(phraseologic=True)
if len(q_same_frames) > 0:
new_frames = new_frames.exclude(reduce(operator.or_, q_same_frames))
superphraseologist_dict['redo_frames'] = new_frames.all()
for frame in superphraseologist_dict['redo_frames']:
superphraseologist_dict['cash'] += corrected_frame_value
# superleksykograf nie zostal jeszcze oplacony za dane haslo, zmien wartosc
if not checked_lemmas.exists():
superphraseologist_real_lemma = RealizedPhraseology(lemma=lemma,
cash=superphraseologist_dict['cash'],
corr_frames=len(superphraseologist_dict['redo_frames']),
ncorr_frames=len(superphraseologist_dict['same_frames']),
paid=False,
status=status,
bonus=False,
counted=False)
superphraseologist_real_lemma.save()
superphraseologist.user_stats.phraseology_real_history.add(superphraseologist_real_lemma)
else:
to_update = RealizedPhraseology.objects.get(pk=checked_lemmas[0].pk)
to_update.lemma = lemma
to_update.cash = superphraseologist_dict['cash']
to_update.corr_frames = len(superphraseologist_dict['redo_frames'])
to_update.ncorr_frames = len(superphraseologist_dict['same_frames'])
to_update.status = status
to_update.save()
# superfrazeolog tylko klika wiec moze sie zmienic
old_superphraseologist = to_update.user_stats.all()[0].user # UWAGA: frazeolog tez moze sie zmienic
if old_superphraseologist != superphraseologist:
old_superphraseologist.user_stats.phraseology_real_history.remove(to_update)
superphraseologist.user_stats.phraseology_real_history.add(to_update)
# hasla pochodzace z clarinu maja cash = 0.0 i nie nalezy ich bonusować
try:
RealizedPhraseology.objects.get(lemma__entry_obj=lemma.entry_obj,
status__type__sym_name='ready_f',
bonus=False, cash=0.0, paid=True,
counted=True)
except RealizedPhraseology.DoesNotExist:
try: # zmienianie bonusu
phraseologist_real_lemma = RealizedPhraseology.objects.get(lemma__entry_obj=lemma.entry_obj,
status__type__sym_name=status.type.sym_name,
bonus=True)
if not phraseologist_real_lemma.counted:
phraseologist_real_lemma.lemma = lemma
phraseologist_real_lemma.cash = phraseologist_dict['cash']
phraseologist_real_lemma.prop_frames = phraseologist_dict['same_frames']
phraseologist_real_lemma.wrong_frames = phraseologist_dict['wrong_frames']
phraseologist_real_lemma.status = status
phraseologist_real_lemma.save()
old_phraseologist = phraseologist_real_lemma.user_stats.all()[0].user # UWAGA: frazeolog tez moze sie zmienic
if old_phraseologist != phraseologist:
old_phraseologist.user_stats.phraseology_real_history.remove(phraseologist_real_lemma)
phraseologist.user_stats.phraseology_real_history.add(phraseologist_real_lemma)
except RealizedPhraseology.DoesNotExist:
phraseologist_real_lemma = RealizedPhraseology(lemma=lemma,
cash=phraseologist_dict['cash'],
prop_frames=phraseologist_dict['same_frames'],
wrong_frames=phraseologist_dict['wrong_frames'],
paid=False, status=status,
bonus=True, counted=False)
phraseologist_real_lemma.save()
phraseologist.user_stats.phraseology_real_history.add(phraseologist_real_lemma)
####################### semantics #############################
def update_sem_stats_ready_s(entry, semantic_frames, semanticist, status, frame_value):
"""Update user stats about created semantic frames."""
ready_statuses = Lemma_Status.objects.filter(type__sym_name='ready_s')
q_ready_statuses = []
for ready_status in ready_statuses.all():
q_ready_statuses.append(Q(status=ready_status))
ready_semantics = RealizedSemantics.objects.filter(reduce(operator.or_, q_ready_statuses)).filter(entry=entry).order_by('-date')
actual_frames_count = semantic_frames.count()
sem_dict = {'made_frames': actual_frames_count,
'cash': frame_value*float(actual_frames_count)}
# sprawdz czy dane haslo nie zostalo juz podliczone
if not ready_semantics.exists():
# semantyk nie został jeszcze oplacony za dane haslo, wiec utworz
realized_semantics = RealizedSemantics(entry=entry, cash=sem_dict['cash'],
made_frames=sem_dict['made_frames'],
status=status, bonus=False)
realized_semantics.save()
realized_semantics.frames.add(*semantic_frames.all())
semanticist.user_stats.semantics_real_history.add(realized_semantics)
else:
to_update = RealizedSemantics.objects.get(pk=ready_semantics[0].pk)
to_update.cash = sem_dict['cash']
to_update.made_frames = sem_dict['made_frames']
to_update.frames.clear()
to_update.frames.add(*semantic_frames.all())
to_update.status = status
to_update.save()
# semantyk moze sie zmienic
old_semanticist = to_update.user_stats.all()[0].user # UWAGA: semantyk tez moze sie zmienic
if old_semanticist != semanticist:
old_semanticist.user_stats.semantics_real_history.remove(to_update)
semanticist.user_stats.semantics_real_history.add(to_update)
def update_sem_stats_conf_s(entry, semantic_frames, semanticist, supersemanticist, status,
checked_frame_value, corrected_frame_value, bonus_factor):
"""Update user stats about checked semantic frames."""
ready_statuses = Lemma_Status.objects.filter(type__sym_name='ready_s')
q_ready_statuses = []
for ready_status in ready_statuses.all():
q_ready_statuses.append(Q(status=ready_status))
checked_statuses = Lemma_Status.objects.filter(type__sym_name='checked_s')
q_checked_statuses = []
for checked_status in checked_statuses.all():
q_checked_statuses.append(Q(status=checked_status))
checked_semantics = RealizedSemantics.objects.filter(reduce(operator.or_, q_checked_statuses)).filter(entry=entry,
bonus=False).order_by('-date')
ready_semantics = RealizedSemantics.objects.filter(reduce(operator.or_, q_ready_statuses)).filter(entry=entry).order_by('-date')
ready_sem_frames = ready_semantics[ready_semantics.count()-1].frames
checked_sem_frames = semantic_frames
ready_to_checked_diffs = get_frames_differences(ready_sem_frames.all(), checked_sem_frames.all())
checked_to_ready_diffs = get_frames_differences(checked_sem_frames.all(), ready_sem_frames.all())
sem_dict = {'same_frames': len(ready_to_checked_diffs['matching_frames']),
'wrong_frames': len(ready_to_checked_diffs['missing_frames']),
'cash': bonus_factor*float(len(ready_to_checked_diffs['matching_frames']))}
supersem_dict = {'same_frames': len(checked_to_ready_diffs['matching_frames']),
'redo_frames': len(checked_to_ready_diffs['missing_frames']),
'cash': (float(len(checked_to_ready_diffs['missing_frames']))*corrected_frame_value+
float(len(ready_to_checked_diffs['matching_frames']))*checked_frame_value)}
# sprawdz czy dane haslo nie zostalo juz podliczone
if not checked_semantics.exists():
supersem_real_semantics = RealizedSemantics(entry=entry,
cash=supersem_dict['cash'],
corr_frames=supersem_dict['redo_frames'],
ncorr_frames=supersem_dict['same_frames'],
status=status,
bonus=False)
supersem_real_semantics.save()
supersem_real_semantics.frames.add(*semantic_frames.all())
supersemanticist.user_stats.semantics_real_history.add(supersem_real_semantics)
else:
to_update = RealizedSemantics.objects.get(pk=checked_semantics[0].pk)
to_update.cash = supersem_dict['cash']
to_update.corr_frames = len(supersem_dict['redo_frames'])
to_update.ncorr_frames = len(supersem_dict['same_frames'])
to_update.status = status
to_update.frames.clear()
to_update.frames.add(*semantic_frames.all())
to_update.save()
# supersemantyk tylko klika wiec moze sie zmienic
old_supersem = to_update.user_stats.all()[0].user
if old_supersem != supersemanticist:
old_supersem.user_stats.semantics_real_history.remove(to_update)
supersemanticist.user_stats.semantics_real_history.add(to_update)
try: # zmienianie bonusu
sem_real_semantics = RealizedSemantics.objects.get(entry=entry,
status__type__sym_name=status.type.sym_name,
bonus=True)
sem_real_semantics.cash = sem_dict['cash']
sem_real_semantics.prop_frames = sem_dict['same_frames']
sem_real_semantics.wrong_frames = sem_dict['wrong_frames']
sem_real_semantics.status = status
sem_real_semantics.frames.clear()
sem_real_semantics.frames.add(*semantic_frames.all())
sem_real_semantics.save()
old_sem = sem_real_semantics.user_stats.all()[0].user
if old_sem != semanticist:
old_sem.user_stats.semantics_real_history.remove(sem_real_semantics)
semanticist.user_stats.semantics_real_history.add(sem_real_semantics)
except RealizedSemantics.DoesNotExist:
sem_real_semantics = RealizedSemantics(entry=entry,
cash=sem_dict['cash'],
prop_frames=sem_dict['same_frames'],
wrong_frames=sem_dict['wrong_frames'],
status=status,
bonus=True)
sem_real_semantics.save()
sem_real_semantics.frames.add(*semantic_frames.all())
semanticist.user_stats.semantics_real_history.add(sem_real_semantics)