1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# Copyright 2013 Mathias WOLFF |
3
|
|
|
# This file is part of pyfreebilling. |
4
|
|
|
# |
5
|
|
|
# pyfreebilling is free software: you can redistribute it and/or modify |
6
|
|
|
# it under the terms of the GNU General Public License as published by |
7
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
# (at your option) any later version. |
9
|
|
|
# |
10
|
|
|
# pyfreebilling is distributed in the hope that it will be useful, |
11
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
# GNU General Public License for more details. |
14
|
|
|
# |
15
|
|
|
# You should have received a copy of the GNU General Public License |
16
|
|
|
# along with pyfreebilling. If not, see <http://www.gnu.org/licenses/> |
17
|
|
|
|
18
|
|
|
from django.db.models import Sum, Avg |
19
|
|
|
from django.contrib import admin |
20
|
|
|
from django.contrib.admin.views.main import ChangeList |
21
|
|
|
from django.conf import settings |
22
|
|
|
from django.utils.translation import ugettext_lazy as _ |
23
|
|
|
|
24
|
|
|
from datetime import date |
25
|
|
|
import datetime |
26
|
|
|
from import_export.admin import ExportMixin |
27
|
|
|
from import_export.formats import base_formats |
28
|
|
|
|
29
|
|
|
from pyfreebilling.pyfreebill.models import Person |
30
|
|
|
|
31
|
|
|
from .models import CDR |
32
|
|
|
from .resources import CDRResourceExtra |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
DEFAULT_FORMATS = (base_formats.CSV, ) |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
class TotalAveragesChangeList(ChangeList): |
39
|
|
|
|
40
|
|
|
def __init__(self, *args): |
41
|
|
|
super(TotalAveragesChangeList, self).__init__(*args) |
42
|
|
|
|
43
|
|
|
def get_min_duration(self, sec_duration): |
44
|
|
|
if sec_duration: |
45
|
|
|
min = int(sec_duration / 60) |
46
|
|
|
sec = int(sec_duration % 60) |
47
|
|
|
else: |
48
|
|
|
min = 0 |
49
|
|
|
sec = 0 |
50
|
|
|
return "%02d:%02d" % (min, sec) |
51
|
|
|
|
52
|
|
|
def get_results(self, request): |
53
|
|
|
super(TotalAveragesChangeList, self).get_results(request) |
54
|
|
|
self.total_sell_total = 0 |
55
|
|
|
self.total_cost_total = 0 |
56
|
|
|
try: |
57
|
|
|
q = self.result_list.aggregate( |
58
|
|
|
total_sell_sum=Sum('total_sell'), |
59
|
|
|
total_cost_sum=Sum('total_cost'), |
60
|
|
|
effective_duration_sum=Sum('effective_duration'), |
61
|
|
|
effective_duration_avg=Avg('effective_duration') |
62
|
|
|
) |
63
|
|
|
except: |
64
|
|
|
self.total_effective_duration = 0 |
65
|
|
|
self.total_cost_total = 0 |
66
|
|
|
self.total_sell_total = 0 |
67
|
|
|
self.avg_effective_duration = 0 |
68
|
|
|
self.margin = 0 |
69
|
|
|
return |
70
|
|
|
self.total_effective_duration = q['effective_duration_sum'] |
71
|
|
|
self.total_cost_total = q['total_cost_sum'] |
72
|
|
|
self.total_sell_total = q['total_sell_sum'] |
73
|
|
|
self.avg_effective_duration = q['effective_duration_avg'] |
74
|
|
|
try: |
75
|
|
|
self.margin = self.total_sell_total - self.total_cost_total |
76
|
|
|
except: |
77
|
|
|
self.margin = 0 |
78
|
|
|
self.min_avg_effective_duration = self.get_min_duration(q['effective_duration_avg']) |
79
|
|
|
self.min_total_effective_duration = self.get_min_duration(q['effective_duration_sum']) |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
class CDRAdmin(ExportMixin, admin.ModelAdmin): |
83
|
|
|
search_fields = ['^prefix', |
84
|
|
|
'^destination_number', |
85
|
|
|
'^customer__name', |
86
|
|
|
'^cost_destination', |
87
|
|
|
'^sell_destination'] |
88
|
|
|
list_filter = ('start_stamp',) |
89
|
|
|
list_select_related = ( |
90
|
|
|
'customer', |
91
|
|
|
'ratecard_id', |
92
|
|
|
'lcr_carrier_id', |
93
|
|
|
) |
94
|
|
|
# date_hierarchy = 'start_stamp' |
95
|
|
|
change_list_template = 'admin/cdr/change_list.html' |
96
|
|
|
resource_class = CDRResourceExtra |
97
|
|
|
fieldsets = ( |
98
|
|
|
(_(u'General'), { |
99
|
|
|
'fields': ('customer', 'customerdirectory_id', |
100
|
|
|
'start_stamp', |
101
|
|
|
'destination_number', |
102
|
|
|
('min_effective_duration', 'billsec'), |
103
|
|
|
('sell_destination', 'cost_destination'), |
104
|
|
|
'switchname') |
105
|
|
|
}), |
106
|
|
|
(_(u'Advanced date / duration infos'), { |
107
|
|
|
'fields': (('answered_stamp', |
108
|
|
|
'end_stamp', |
109
|
|
|
'duration', |
110
|
|
|
'effectiv_duration')) |
111
|
|
|
}), |
112
|
|
|
(_(u'Financial infos'), { |
113
|
|
|
'fields': (('total_cost', 'cost_rate'), |
114
|
|
|
('total_sell', 'rate'), |
115
|
|
|
('init_block', 'block_min_duration')) |
116
|
|
|
}), |
117
|
|
|
(_(u'LCR infos'), { |
118
|
|
|
'fields': ('prefix', |
119
|
|
|
('ratecard_id', 'lcr_group_id'), |
120
|
|
|
('lcr_carrier_id', 'gateway')) |
121
|
|
|
}), |
122
|
|
|
(_(u'Call detailed infos'), { |
123
|
|
|
'fields': ('caller_id_number', |
124
|
|
|
('hangup_cause', |
125
|
|
|
'hangup_cause_q850', |
126
|
|
|
'hangup_disposition', |
127
|
|
|
'sip_hangup_cause'), |
128
|
|
|
('read_codec', 'write_codec'), |
129
|
|
|
'sip_user_agent', |
130
|
|
|
'customer_ip', |
131
|
|
|
('uuid', 'bleg_uuid', 'chan_name', 'country')) |
132
|
|
|
}), |
133
|
|
|
) |
134
|
|
|
|
135
|
|
|
def has_add_permission(self, request, obj=None): |
136
|
|
|
return False |
137
|
|
|
|
138
|
|
|
def has_delete_permission(self, request, obj=None): |
139
|
|
|
return False |
140
|
|
|
|
141
|
|
|
def get_actions(self, request): |
142
|
|
|
if request.user.is_superuser: |
143
|
|
|
return |
144
|
|
|
else: |
145
|
|
|
return |
146
|
|
|
|
147
|
|
|
def get_changelist(self, request, **kwargs): |
148
|
|
|
return TotalAveragesChangeList |
149
|
|
|
|
150
|
|
|
def changelist_view(self, request, extra_context=None): |
151
|
|
|
if request.user.is_superuser: |
152
|
|
|
self.list_display_links = ['start_stamp', ] |
153
|
|
|
return super(CDRAdmin, self).changelist_view(request, |
154
|
|
|
extra_context=None) |
155
|
|
|
else: |
156
|
|
|
self.list_display_links = ['None', ] |
157
|
|
|
return super(CDRAdmin, self).changelist_view(request, |
158
|
|
|
extra_context=None) |
159
|
|
|
|
160
|
|
|
def get_ordering(self, request): |
161
|
|
|
if request.user.is_superuser: |
162
|
|
|
return ['-start_stamp', ] |
163
|
|
|
else: |
164
|
|
|
return ['-start_stamp', ] |
165
|
|
|
|
166
|
|
|
def get_list_display(self, request): |
167
|
|
|
if request.user.is_superuser: |
168
|
|
|
return ['start_stamp', |
169
|
|
|
'customer', |
170
|
|
|
'sell_destination', |
171
|
|
|
'destination_number', |
172
|
|
|
'min_effective_duration', |
173
|
|
|
'hangup_cause_colored', |
174
|
|
|
'lcr_carrier_id', |
175
|
|
|
'cost_rate', |
176
|
|
|
'rate', |
177
|
|
|
'prefix', |
178
|
|
|
'ratecard_id', |
179
|
|
|
'switchname'] |
180
|
|
|
else: |
181
|
|
|
return ['start_stamp', |
182
|
|
|
'customer', |
183
|
|
|
'customer_ip', |
184
|
|
|
'sell_destination', |
185
|
|
|
'destination_number', |
186
|
|
|
'min_effective_duration', |
187
|
|
|
'hangup_cause', |
188
|
|
|
'rate', |
189
|
|
|
'total_sell'] |
190
|
|
|
|
191
|
|
|
def get_list_filter(self, request): |
192
|
|
|
if request.user.is_superuser: |
193
|
|
|
return ['start_stamp', |
194
|
|
|
'customer', |
195
|
|
|
'lcr_carrier_id', |
196
|
|
|
'ratecard_id', |
197
|
|
|
'rctype', |
198
|
|
|
'switchname'] |
199
|
|
|
else: |
200
|
|
|
return ['start_stamp', 'sell_destination'] |
201
|
|
|
|
202
|
|
|
def get_readonly_fields(self, request, obj=None): |
203
|
|
|
if request.user.is_superuser: |
204
|
|
|
return ['customer_ip', |
205
|
|
|
'customer', |
206
|
|
|
'customerdirectory_id', |
207
|
|
|
'caller_id_number', |
208
|
|
|
'destination_number', |
209
|
|
|
'start_stamp', |
210
|
|
|
'answered_stamp', |
211
|
|
|
'end_stamp', |
212
|
|
|
'duration', |
213
|
|
|
'min_effective_duration', |
214
|
|
|
'billsec', |
215
|
|
|
'hangup_cause', |
216
|
|
|
'hangup_cause_q850', |
217
|
|
|
'gateway', |
218
|
|
|
'lcr_carrier_id', |
219
|
|
|
'prefix', |
220
|
|
|
'country', |
221
|
|
|
'cost_rate', |
222
|
|
|
'total_cost', |
223
|
|
|
'total_sell', |
224
|
|
|
'rate', |
225
|
|
|
'init_block', |
226
|
|
|
'block_min_duration', |
227
|
|
|
'ratecard_id', |
228
|
|
|
'lcr_group_id', |
229
|
|
|
'uuid', |
230
|
|
|
'bleg_uuid', |
231
|
|
|
'chan_name', |
232
|
|
|
'read_codec', |
233
|
|
|
'write_codec', |
234
|
|
|
'sip_user_agent', |
235
|
|
|
'hangup_disposition', |
236
|
|
|
'effectiv_duration', |
237
|
|
|
'sip_hangup_cause', |
238
|
|
|
'sell_destination', |
239
|
|
|
'cost_destination', |
240
|
|
|
'switchname', |
241
|
|
|
'sipserver_name'] |
242
|
|
|
else: |
243
|
|
|
return ['start_stamp', |
244
|
|
|
'customer', |
245
|
|
|
'customer_ip', |
246
|
|
|
'sell_destination', |
247
|
|
|
'destination_number', |
248
|
|
|
'min_effective_duration', |
249
|
|
|
'hangup_cause', |
250
|
|
|
'rate', |
251
|
|
|
'total_sell'] |
252
|
|
|
|
253
|
|
|
def get_form(self, request, obj=None, **kwargs): |
254
|
|
|
self.exclude = ['effectiv_duration', |
255
|
|
|
'effective_duration', |
256
|
|
|
'sip_rtp_rxstat', |
257
|
|
|
'sip_rtp_txstat', |
258
|
|
|
'switch_ipv4'] |
259
|
|
|
if not request.user.is_superuser: |
260
|
|
|
self.exclude.append('cost_rate') |
261
|
|
|
self.exclude.append('total_cost') |
262
|
|
|
self.exclude.append('gateway') |
263
|
|
|
self.exclude.append('switchname') |
264
|
|
|
self.exclude.append('lcr_carrier_id') |
265
|
|
|
self.exclude.append('lcr_group_id') |
266
|
|
|
self.exclude.append('cost_destination') |
267
|
|
|
return super(CDRAdmin, self).get_form(request, obj, **kwargs) |
268
|
|
|
|
269
|
|
|
def get_queryset(self, request): |
270
|
|
|
today_c = date.today() - datetime.timedelta(days=settings.PFB_NB_CUST_CDR) |
271
|
|
|
today_a = date.today() - datetime.timedelta(days=settings.PFB_NB_ADMIN_CDR) |
272
|
|
|
user = getattr(request, 'user', None) |
273
|
|
|
qs = super(CDRAdmin, self).get_queryset(request) |
274
|
|
|
# add .prefetch_related('content_type') for reduce queries |
275
|
|
|
if user.is_superuser: |
276
|
|
|
return qs.filter(start_stamp__gte=today_a) |
277
|
|
|
else: |
278
|
|
|
usercompany = Person.objects.get(user=user) |
279
|
|
|
return unicode(qs.filter(customer=usercompany.company).filter(start_stamp__gte=today_c).filter(effective_duration__gt="0")) |
280
|
|
|
|
281
|
|
|
def get_export_formats(self): |
282
|
|
|
format_csv = DEFAULT_FORMATS |
283
|
|
|
return [f for f in format_csv if f().can_export()] |
284
|
|
|
|
285
|
|
|
|
286
|
|
|
admin.site.register(CDR, CDRAdmin) |
287
|
|
|
|