get_payments.py
8.89 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
#-*- 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 codecs
from django.core.management.base import BaseCommand
from django.db.models import Q
from django.contrib.auth.models import User
from dictionary.models import Lemma, Vocabulary
from accounts.models import RealizedLemma
from django.db.models import Sum, Count, Max
class Command(BaseCommand):
args = 'none'
help = """
Complete RealizedLemmas objects about information
how many frames where done (ready also are counted), and gives users money
for ready flat frames.
"""
def handle(self, **options):
get_payments()
def get_payments():
payments_path = 'data/payments.csv'
payments_file = codecs.open(payments_path, 'wt', 'utf-8')
users = User.objects.all()
all_made_lemmas_count = 0
all_made_lemmas_ready = 0
all_made_lemmas_checked = 0
all_frames_count_bef = 0
all_frames_count_after = 0
all_checked_lemmas_count = 0
for user in users:
#realized_lemmas = RealizedLemma.objects.filter(user_stats__user=user,
# paid=False,
# date__range=["2010-01-01",
# "2013-05-01"])
realized_lemmas = RealizedLemma.objects.filter(user_stats__user=user,
paid=False,
date__range=["2012-05-01",
"2014-06-30"])
cash_to_pay = realized_lemmas.aggregate(Sum('cash'))['cash__sum']
lemmas_after_date = RealizedLemma.objects.filter(user_stats__user=user,
paid=False,
date__range=["2013-07-01",
"2014-04-30"])
cash_after_daterange = lemmas_after_date.aggregate(Sum('cash'))['cash__sum']
if cash_to_pay and cash_after_daterange:
payments_file.write(u'\nUżytkownik' + ';' + user.username + ';' +
u'Wynagrodzenie' + ';' + str(round(cash_to_pay, 2)) + ';' +
str(round(cash_after_daterange, 2)) + '\n')
payments_file.write(';' + u'Słownik' + ';' +
u'Zrealizowane hasła' + ';' +
u'Zrealizowane hasła (gotowe)' + ';' +
u'Zrealizowane hasła (sprawdzone)' + ';' +
u'Ramki (przed sprawdzeniem)' + ';' +
u'Ramki (po sprawdzeniu)' + ';' +
u'Sprawdzone hasła' + '\n')
elif cash_to_pay:
payments_file.write(u'\nUżytkownik' + ';' + user.username + ';' +
u'Wynagrodzenie' + ';' + str(round(cash_to_pay, 2)) + '\n')
payments_file.write(';' + u'Słownik' + ';' +
u'Zrealizowane hasła' + ';' +
u'Zrealizowane hasła (gotowe)' + ';' +
u'Zrealizowane hasła (sprawdzone)' + ';' +
u'Ramki (przed sprawdzeniem)' + ';' +
u'Ramki (po sprawdzeniu)' + ';' +
u'Sprawdzone hasła' + '\n')
user_made_lemmas_count = 0
user_made_lemmas_ready = 0
user_made_lemmas_checked = 0
user_frames_count_bef = 0
user_frames_count_after = 0
user_checked_lemmas_count = 0
for vocabulary in Vocabulary.objects.all():
made_lemmas = realized_lemmas.filter(made_frames__gt=0,
lemma__vocabulary=vocabulary)
voc_frames_count_bef = 0
voc_frames_count_after = 0
voc_made_lemmas_ready = 0
voc_made_lemmas_checked = 0
made_lemmas_count = made_lemmas.count()
user_made_lemmas_count += made_lemmas_count
all_made_lemmas_count += made_lemmas_count
for real_lemma in made_lemmas:
lemma = Lemma.objects.get(entry=real_lemma.lemma.entry, old=False,
vocabulary=vocabulary)
if lemma.status.status == 'gotowe':
user_made_lemmas_ready += 1
all_made_lemmas_ready += 1
voc_made_lemmas_ready += 1
elif lemma.status.status == 'sprawdzone':
user_made_lemmas_checked += 1
all_made_lemmas_checked += 1
voc_made_lemmas_checked += 1
for frame in lemma.frames.all():
voc_frames_count_after += frame.positions.annotate(num_args=Count('arguments')).aggregate(Max('num_args'))['num_args__max']
user_frames_count_after += frame.positions.annotate(num_args=Count('arguments')).aggregate(Max('num_args'))['num_args__max']
all_frames_count_after += frame.positions.annotate(num_args=Count('arguments')).aggregate(Max('num_args'))['num_args__max']
lemma = real_lemma.lemma
for frame in lemma.frames.all():
voc_frames_count_bef += frame.positions.annotate(num_args=Count('arguments')).aggregate(Max('num_args'))['num_args__max']
user_frames_count_bef += frame.positions.annotate(num_args=Count('arguments')).aggregate(Max('num_args'))['num_args__max']
all_frames_count_bef += frame.positions.annotate(num_args=Count('arguments')).aggregate(Max('num_args'))['num_args__max']
checked_lemmas = realized_lemmas.filter(lemma__vocabulary=
vocabulary).filter(Q(corr_frames__gt=0) |
Q(ncorr_frames__gt=0)).count()
user_checked_lemmas_count += checked_lemmas
all_checked_lemmas_count += checked_lemmas
if checked_lemmas == 0 and made_lemmas_count == 0:
continue
payments_file.write(';' + vocabulary.name + ';' + str(made_lemmas_count) + ';'
+ str(voc_made_lemmas_ready) + ';'
+ str(voc_made_lemmas_checked) + ';'
+ str(voc_frames_count_bef) + ';'
+ str(voc_frames_count_after) + ';'
+ str(checked_lemmas) + '\n')
if user_checked_lemmas_count != 0 or user_made_lemmas_count != 0:
payments_file.write(';Suma;' + str(user_made_lemmas_count) + ';'
+ str(user_made_lemmas_ready) + ';'
+ str(user_made_lemmas_checked) + ';'
+ str(user_frames_count_bef) + ';'
+ str(user_frames_count_after) + ';'
+ str(user_checked_lemmas_count) + '\n')
payments_file.write('\n\n;Suma;' + str(all_made_lemmas_count) + ';'
+ str(all_made_lemmas_ready) + ';'
+ str(all_made_lemmas_checked) + ';'
+ str(all_frames_count_bef) + ';'
+ str(all_frames_count_after) + ';'
+ str(all_checked_lemmas_count) + '\n')
payments_file.close()