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.contrib import admin |
19
|
|
|
from django.contrib import messages |
20
|
|
|
from django.contrib.contenttypes.admin import GenericTabularInline, GenericStackedInline |
21
|
|
|
from django.db.models.functions import Trunc, Coalesce |
22
|
|
|
from django.db.models import DateTimeField, Sum, Avg, Count, Max, Min, Value as V |
23
|
|
|
from django.utils.html import escape |
24
|
|
|
from django.core.urlresolvers import reverse |
25
|
|
|
from django.contrib.admin.views.main import ChangeList |
26
|
|
|
from django.forms.models import BaseInlineFormSet |
27
|
|
|
from django.template import Context, loader |
28
|
|
|
from django.utils.safestring import mark_safe |
29
|
|
|
from django.conf import settings |
30
|
|
|
from django.utils.translation import ugettext_lazy as _ |
31
|
|
|
|
32
|
|
|
from import_export.admin import ImportExportMixin, ImportMixin |
33
|
|
|
from import_export.formats import base_formats |
34
|
|
|
|
35
|
|
|
import datetime |
36
|
|
|
|
37
|
|
|
from pyfreebilling.antifraud.models import Fraud |
38
|
|
|
|
39
|
|
|
from pyfreebilling.switch import esl |
40
|
|
|
from pyfreebilling.switch.models import VoipSwitchProfile |
41
|
|
|
|
42
|
|
|
from pyfreebilling.customerdirectory.models import CustomerDirectory |
43
|
|
|
|
44
|
|
|
from .models import * |
45
|
|
|
from .forms import CustomerRateCardsAdminForm, CompanyAdminForm, CustomerRatesAdminForm, ProviderRatesAdminForm, ProviderTariffAdminForm, RateCardAdminForm, CustomerDirectoryAdminForm |
46
|
|
|
from .resources import CalleridPrefixResource |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
APP_LABEL = _(u'CDR report') |
50
|
|
|
|
51
|
|
|
DEFAULT_FORMATS = (base_formats.CSV, ) |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
# def sofiaupdate(modeladmin, request, queryset): |
55
|
|
|
# """ generate new sofia xml config file """ |
56
|
|
|
# try: |
57
|
|
|
# t = loader.get_template('xml/sofia.conf.xml') |
58
|
|
|
# except IOError: |
59
|
|
|
# messages.error(request, |
60
|
|
|
# _(u"""sofia config xml file update failed. Can not load |
61
|
|
|
# template file !""")) |
62
|
|
|
# sipprofiles = SipProfile.objects.all() |
63
|
|
|
# accounts = Company.objects.filter(supplier_enabled=True) |
64
|
|
|
# c = Context({"sipprofiles": sipprofiles, "accounts": accounts}) |
65
|
|
|
# try: |
66
|
|
|
# f = open('/usr/local/freeswitch/conf/autoload_configs/sofia.conf.xml', |
67
|
|
|
# 'w') |
68
|
|
|
# try: |
69
|
|
|
# f.write(t.render(c)) |
70
|
|
|
# f.close() |
71
|
|
|
# try: |
72
|
|
|
# fs = esl.getReloadGateway() |
73
|
|
|
# messages.success(request, _(u"FS successfully reload")) |
74
|
|
|
# except IOError: |
75
|
|
|
# messages.error(request, _(u"""customer sip config xml file update |
76
|
|
|
# failed. FS ACL update failed ! Try manually -- %s""" % fs)) |
77
|
|
|
# finally: |
78
|
|
|
# #f.close() |
79
|
|
|
# messages.success(request, _(u"sofia config xml file update success")) |
80
|
|
|
# except IOError: |
81
|
|
|
# messages.error(request, _(u"""sofia config xml file update failed. Can |
82
|
|
|
# not create file !""")) |
83
|
|
|
# sofiaupdate.short_description = _(u"update sofia config xml file") |
84
|
|
|
# |
85
|
|
|
# |
86
|
|
|
# def aclupdate(modeladmin, request, queryset): |
87
|
|
|
# """ generate new ACL xml config file """ |
88
|
|
|
# try: |
89
|
|
|
# t = loader.get_template('xml/acl.conf.xml') |
90
|
|
|
# except IOError: |
91
|
|
|
# messages.error(request, _(u"""ACL config xml file update failed. Can |
92
|
|
|
# not load template file !""")) |
93
|
|
|
# acllists = AclLists.objects.all() |
94
|
|
|
# aclnodes = AclNodes.objects.all() |
95
|
|
|
# c = Context({"acllists": acllists, "aclnodes": aclnodes}) |
96
|
|
|
# try: |
97
|
|
|
# f = open('/usr/local/freeswitch/conf/autoload_configs/acl.conf.xml', |
98
|
|
|
# 'w') |
99
|
|
|
# try: |
100
|
|
|
# f.write(t.render(c)) |
101
|
|
|
# f.close() |
102
|
|
|
# try: |
103
|
|
|
# fs = esl.getReloadACL() |
104
|
|
|
# messages.success(request, _(u"FS successfully reload")) |
105
|
|
|
# except IOError: |
106
|
|
|
# messages.error(request, _(u"""ACL config xml file update failed. |
107
|
|
|
# FS ACL update failed ! Try manually--- %s""" % fs)) |
108
|
|
|
# finally: |
109
|
|
|
# messages.success(request, _(u"ACL config xml file update success")) |
110
|
|
|
# except IOError: |
111
|
|
|
# messages.error(request, _(u"""ACL xml file update failed. Can not |
112
|
|
|
# create file !""")) |
113
|
|
|
# aclupdate.short_description = _(u"update ACL config xml file") |
114
|
|
|
# |
115
|
|
|
# |
116
|
|
|
# admin.site.add_action(sofiaupdate, _(u"generate sofia configuration file")) |
117
|
|
|
# admin.site.add_action(aclupdate, _(u"generate acl configuration file")) |
118
|
|
|
|
119
|
|
|
# Company - Contatcs |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
class AntiFraudInline(admin.TabularInline): |
123
|
|
|
model = Fraud |
124
|
|
|
collapse = True |
125
|
|
|
max_num = 1 |
126
|
|
|
description = _(u'AntiFraud system parameters') |
127
|
|
|
readonly_fields = ('high_amount_alert_sent', |
128
|
|
|
'high_minutes_alert_sent', |
129
|
|
|
'account_blocked_alert_sent') |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
class CustomerDirectoryInline(GenericStackedInline): |
133
|
|
|
model = CustomerDirectory |
134
|
|
|
extra = 0 |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
class EmailAddressInline(GenericTabularInline): |
138
|
|
|
model = EmailAddress |
139
|
|
|
extra = 0 |
140
|
|
|
collapse = True |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
class PhoneNumberInline(GenericTabularInline): |
144
|
|
|
model = PhoneNumber |
145
|
|
|
extra = 0 |
146
|
|
|
collapse = True |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
class WebSiteInline(GenericTabularInline): |
150
|
|
|
model = WebSite |
151
|
|
|
extra = 0 |
152
|
|
|
collapse = True |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
class StreetAddressInline(GenericStackedInline): |
156
|
|
|
model = StreetAddress |
157
|
|
|
extra = 0 |
158
|
|
|
collapse = True |
159
|
|
|
modal = True |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
class CustomerRateCardsInline(admin.TabularInline): |
163
|
|
|
model = CustomerRateCards |
164
|
|
|
description = _(u'select the Ratecards affected to customer account. Order is important !') |
165
|
|
|
form = CustomerRateCardsAdminForm |
166
|
|
|
max_num = 7 |
167
|
|
|
extra = 0 |
168
|
|
|
modal = True |
169
|
|
|
# sortable = True |
170
|
|
|
# sortable_order_field = 'priority' |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
class CompanyAdmin(admin.ModelAdmin): |
174
|
|
|
inlines = [ |
175
|
|
|
CustomerRateCardsInline, |
176
|
|
|
AntiFraudInline, |
177
|
|
|
#CustomerDirectoryInline, |
178
|
|
|
PhoneNumberInline, |
179
|
|
|
EmailAddressInline, |
180
|
|
|
WebSiteInline, |
181
|
|
|
StreetAddressInline, |
182
|
|
|
] |
183
|
|
|
# form = CompanyAdminForm |
184
|
|
|
save_on_top = True |
185
|
|
|
title_icon = 'fa-group' |
186
|
|
|
|
187
|
|
|
search_fields = ['^name', ] |
188
|
|
|
prepopulated_fields = {'slug': ('name',)} |
189
|
|
|
readonly_fields = ( |
190
|
|
|
'customer_balance', |
191
|
|
|
'supplier_balance', |
192
|
|
|
'vat_number_validated' |
193
|
|
|
) |
194
|
|
|
fieldsets = ( |
195
|
|
|
(_(u'General'), { |
196
|
|
|
'fields': (('name', 'nickname'), |
197
|
|
|
('slug', 'about'), |
198
|
|
|
'account_number', |
199
|
|
|
('vat', 'vat_number'), |
200
|
|
|
'vat_number_validated'), |
201
|
|
|
'description': _(u'General company informations') |
202
|
|
|
}), |
203
|
|
|
(_(u'Customer settings'), { |
204
|
|
|
'fields': ('customer_enabled', |
205
|
|
|
('max_calls', 'calls_per_second'), |
206
|
|
|
'billing_cycle', |
207
|
|
|
('prepaid', 'credit_limit'), |
208
|
|
|
('customer_balance',)), |
209
|
|
|
}), |
210
|
|
|
(_(u'Customer alerts'), { |
211
|
|
|
'fields': ('low_credit_alert', |
212
|
|
|
'email_alert', |
213
|
|
|
'low_credit_alert_sent', |
214
|
|
|
'account_blocked_alert_sent'), |
215
|
|
|
'classes': ('collapse',), |
216
|
|
|
'description': _(u'All the customer alert parameters') |
217
|
|
|
}), |
218
|
|
|
(_(u'Provider settings'), { |
219
|
|
|
'fields': ('supplier_enabled', |
220
|
|
|
'supplier_balance'), |
221
|
|
|
'classes': ('collapse',), |
222
|
|
|
'description': _(u'If this company is your provider, this is right place to manage its parameters') |
223
|
|
|
}), |
224
|
|
|
) |
225
|
|
|
|
226
|
|
|
def get_customer_enabled_display(self, obj): |
227
|
|
|
if obj.customer_enabled: |
228
|
|
|
return mark_safe('<span class="label label-success"><i class="icon-thumbs-up"></i> YES</span>') |
229
|
|
|
return mark_safe('<span class="label label-danger"><i class="icon-thumbs-down"></i> NO</span>') |
230
|
|
|
get_customer_enabled_display.short_description = _(u'Customer') |
231
|
|
|
get_customer_enabled_display.admin_order_field = _(u'customer_enabled') |
232
|
|
|
|
233
|
|
|
def get_supplier_enabled_display(self, obj): |
234
|
|
|
if obj.supplier_enabled: |
235
|
|
|
return mark_safe('<span class="label label-success"><i class="icon-thumbs-up"></i> YES</span>') |
236
|
|
|
return mark_safe('<span class="label label-danger"><i class="icon-thumbs-down"></i> NO</span>') |
237
|
|
|
get_supplier_enabled_display.short_description = _(u'Provider') |
238
|
|
|
get_supplier_enabled_display.admin_order_field = _(u'provider_enabled') |
239
|
|
|
|
240
|
|
|
def get_prepaid_display(self, obj): |
241
|
|
|
if obj.prepaid: |
242
|
|
|
return mark_safe('<span class="label label-success"><i class="icon-thumbs-up"></i> YES</span>') |
243
|
|
|
return mark_safe('<span class="label label-danger"><i class="icon-thumbs-down"></i> NO</span>') |
244
|
|
|
get_prepaid_display.short_description = _(u'Prepaid') |
245
|
|
|
get_prepaid_display.admin_order_field = _(u'prepaid') |
246
|
|
|
|
247
|
|
|
def get_vat_display(self, obj): |
248
|
|
|
if obj.vat: |
249
|
|
|
return mark_safe('<span class="label label-info"><i class="icon-thumbs-up"></i> YES</span>') |
250
|
|
|
return mark_safe('<span class="label label-danger"><i class="icon-thumbs-down"></i> NO</span>') |
251
|
|
|
get_vat_display.short_description = _(u'VAT') |
252
|
|
|
get_vat_display.admin_order_field = _(u'vat') |
253
|
|
|
|
254
|
|
|
def get_vat_number_validated_display(self, obj): |
255
|
|
|
if obj.vat_number_validated: |
256
|
|
|
return mark_safe('<span class="label label-info"><i class="icon-thumbs-up"></i> YES</span>') |
257
|
|
|
return mark_safe('<span class="label label-danger"><i class="icon-thumbs-down"></i> NO</span>') |
258
|
|
|
get_vat_number_validated_display.short_description = _(u'VIES') |
259
|
|
|
get_vat_number_validated_display.admin_order_field = _(u'vies') |
260
|
|
|
|
261
|
|
|
def has_add_permission(self, request, obj=None): |
262
|
|
|
if request.user.is_superuser: |
263
|
|
|
return True |
264
|
|
|
else: |
265
|
|
|
return False |
266
|
|
|
|
267
|
|
|
def has_delete_permission(self, request, obj=None): |
268
|
|
|
if request.user.is_superuser: |
269
|
|
|
return True |
270
|
|
|
else: |
271
|
|
|
return False |
272
|
|
|
|
273
|
|
|
def get_actions(self, request): |
274
|
|
|
if request.user.is_superuser: |
275
|
|
|
actions = super(CompanyAdmin, self).get_actions(request) |
276
|
|
|
if 'delete_selected' in actions: |
277
|
|
|
del actions['delete_selected'] |
278
|
|
|
return actions |
279
|
|
|
return |
280
|
|
|
|
281
|
|
|
def changelist_view(self, request, extra_context=None): |
282
|
|
|
if request.user.is_superuser: |
283
|
|
|
self.list_display_links = ['name', ] |
284
|
|
|
return super(CompanyAdmin, self).changelist_view(request, extra_context=None) |
285
|
|
|
else: |
286
|
|
|
self.list_display_links = ['None', ] |
287
|
|
|
return super(CompanyAdmin, self).changelist_view(request, extra_context=None) |
288
|
|
|
|
289
|
|
|
def get_list_display(self, request): |
290
|
|
|
if request.user.is_superuser: |
291
|
|
|
return ('id', |
292
|
|
|
'name', |
293
|
|
|
'get_prepaid_display', |
294
|
|
|
'get_vat_display', |
295
|
|
|
'get_vat_number_validated_display', |
296
|
|
|
'get_customer_enabled_display', |
297
|
|
|
'customer_balance', |
298
|
|
|
'get_supplier_enabled_display', |
299
|
|
|
'supplier_balance', |
300
|
|
|
'balance_history') |
301
|
|
|
else: |
302
|
|
|
return ('name', |
303
|
|
|
'get_prepaid_display', |
304
|
|
|
'get_customer_enabled_display', |
305
|
|
|
'customer_balance') |
306
|
|
|
|
307
|
|
|
# def get_list_filter(self, request): |
308
|
|
|
# if request.user.is_superuser: |
309
|
|
|
# #return ('name',) |
310
|
|
|
# return ['prepaid', 'customer_enabled', 'supplier_enabled'] |
311
|
|
|
# else: |
312
|
|
|
# return [] |
313
|
|
|
|
314
|
|
|
def get_readonly_fields(self, request, obj=None): |
315
|
|
|
if request.user.is_superuser: |
316
|
|
|
return ['customer_balance', |
317
|
|
|
'supplier_balance', |
318
|
|
|
'low_credit_alert_sent', |
319
|
|
|
'account_blocked_alert_sent'] |
320
|
|
|
else: |
321
|
|
|
return ['name', |
322
|
|
|
'prepaid', |
323
|
|
|
'customer_enabled', |
324
|
|
|
'customer_balance', |
325
|
|
|
'vat_number', |
326
|
|
|
'max_calls', |
327
|
|
|
'billing_cycle'] |
328
|
|
|
|
329
|
|
|
def get_form(self, request, obj=None, **kwargs): |
330
|
|
|
self.exclude = [] |
331
|
|
|
if not request.user.is_superuser: |
332
|
|
|
self.exclude.append('supplier_enabled') |
333
|
|
|
self.exclude.append('supplier_balance') |
334
|
|
|
self.exclude.append('vat') |
335
|
|
|
self.exclude.append('nickname') |
336
|
|
|
self.exclude.append('slug') |
337
|
|
|
self.exclude.append('about') |
338
|
|
|
|
339
|
|
|
return super(CompanyAdmin, self).get_form(request, obj, **kwargs) |
340
|
|
|
|
341
|
|
|
def get_queryset(self, request): |
342
|
|
|
user = getattr(request, 'user', None) |
343
|
|
|
qs = super(CompanyAdmin, self).get_queryset(request) |
344
|
|
|
if user.is_superuser: |
345
|
|
|
return qs |
346
|
|
|
else: |
347
|
|
|
usercompany = Person.objects.get(user=user) |
348
|
|
|
return qs.filter(name=usercompany.company) |
349
|
|
|
|
350
|
|
|
|
351
|
|
|
class PersonAdmin(admin.ModelAdmin): |
352
|
|
|
inlines = [ |
353
|
|
|
PhoneNumberInline, |
354
|
|
|
] |
355
|
|
|
|
356
|
|
|
list_display_links = ('first_name', |
357
|
|
|
'last_name') |
358
|
|
|
list_display = ('first_name', |
359
|
|
|
'last_name', |
360
|
|
|
'company') |
361
|
|
|
list_filter = ('company', ) |
362
|
|
|
ordering = ('last_name', |
363
|
|
|
'first_name') |
364
|
|
|
search_fields = ['^first_name', |
365
|
|
|
'^last_name', |
366
|
|
|
'^company__name'] |
367
|
|
|
prepopulated_fields = {'slug': ('first_name', 'last_name')} |
368
|
|
|
|
369
|
|
|
def has_change_permission(self, request, obj=None): |
370
|
|
|
if request.user.is_superuser: |
371
|
|
|
return True |
372
|
|
|
else: |
373
|
|
|
return False |
374
|
|
|
|
375
|
|
|
|
376
|
|
|
class GroupAdmin(admin.ModelAdmin): |
377
|
|
|
list_display_links = ('name', ) |
378
|
|
|
list_display = ('name', |
379
|
|
|
'date_modified') |
380
|
|
|
ordering = ('-date_modified', |
381
|
|
|
'name') |
382
|
|
|
search_fields = ['^name', |
383
|
|
|
'^about'] |
384
|
|
|
prepopulated_fields = {'slug': ('name',)} |
385
|
|
|
|
386
|
|
|
def has_change_permission(self, request, obj=None): |
387
|
|
|
if request.user.is_superuser: |
388
|
|
|
return True |
389
|
|
|
else: |
390
|
|
|
return False |
391
|
|
|
|
392
|
|
|
|
393
|
|
|
class CompanyBalanceHistoryAdmin(admin.ModelAdmin): |
394
|
|
|
list_display_links = ('company', ) |
395
|
|
|
list_display = ('company', |
396
|
|
|
'amount_debited', |
397
|
|
|
'amount_refund', |
398
|
|
|
'customer_balance', |
399
|
|
|
'supplier_balance', |
400
|
|
|
'operation_type', |
401
|
|
|
'reference', |
402
|
|
|
'date_modified') |
403
|
|
|
ordering = ('-date_modified', |
404
|
|
|
'company') |
405
|
|
|
search_fields = ['^company__name', |
406
|
|
|
'^reference'] |
407
|
|
|
|
408
|
|
|
def save_model(self, request, obj, form, change): |
409
|
|
|
if change: |
410
|
|
|
messages.info(request, _(u"No need to update balance")) |
411
|
|
|
else: |
412
|
|
|
company = form.cleaned_data['company'] |
413
|
|
|
amount_debited = form.cleaned_data['amount_debited'] |
414
|
|
|
amount_refund = form.cleaned_data['amount_refund'] |
415
|
|
|
if form.cleaned_data['operation_type'] == "customer": |
416
|
|
|
balance = Company.objects.get(pk=company.id) |
417
|
|
|
balance.customer_balance = balance.customer_balance - amount_debited + amount_refund |
418
|
|
|
balance.save() |
419
|
|
|
obj.customer_balance = balance.customer_balance |
420
|
|
|
elif form.cleaned_data['operation_type'] == "supplier": |
421
|
|
|
balance = Company.objects.get(pk=company.id) |
422
|
|
|
balance.supplier_balance = balance.supplier_balance - amount_debited + amount_refund |
423
|
|
|
balance.save() |
424
|
|
|
obj.supplier_balance = balance.supplier_balance |
425
|
|
|
else: |
426
|
|
|
pass |
427
|
|
|
messages.success(request, _(u"balance updated")) |
428
|
|
|
obj.save() |
429
|
|
|
|
430
|
|
|
fieldsets = ( |
431
|
|
|
(_(u'General'), { |
432
|
|
|
'fields': ('company', |
433
|
|
|
'operation_type', |
434
|
|
|
'reference', |
435
|
|
|
'description')}), |
436
|
|
|
(_(u'Amount'), { |
437
|
|
|
'fields': ('amount_debited', |
438
|
|
|
'amount_refund')}), |
439
|
|
|
(_(u'Balances'), { |
440
|
|
|
'fields': ('customer_balance', |
441
|
|
|
'supplier_balance')}), |
442
|
|
|
) |
443
|
|
|
|
444
|
|
|
def get_list_filter(self, request): |
445
|
|
|
if request.user.is_superuser: |
446
|
|
|
return ['company', 'operation_type'] |
447
|
|
|
else: |
448
|
|
|
return [] |
449
|
|
|
|
450
|
|
|
def has_change_permission(self, request, obj=None): |
451
|
|
|
if request.user.is_superuser: |
452
|
|
|
return True |
453
|
|
|
else: |
454
|
|
|
return False |
455
|
|
|
|
456
|
|
|
def has_delete_permission(self, request, obj=None): |
457
|
|
|
return False |
458
|
|
|
|
459
|
|
|
def get_actions(self, request): |
460
|
|
|
if request.user.is_superuser: |
461
|
|
|
return |
462
|
|
|
else: |
463
|
|
|
return |
464
|
|
|
|
465
|
|
|
def get_readonly_fields(self, request, obj=None): |
466
|
|
|
if obj: # This is the case when obj is already created. it's an edit |
467
|
|
|
return ['company', |
468
|
|
|
'amount_debited', |
469
|
|
|
'amount_refund', |
470
|
|
|
'customer_balance', |
471
|
|
|
'supplier_balance', |
472
|
|
|
'operation_type'] |
473
|
|
|
else: |
474
|
|
|
return ['customer_balance', 'supplier_balance'] |
475
|
|
|
|
476
|
|
|
# CallerID prefix list |
477
|
|
|
|
478
|
|
|
|
479
|
|
|
class CalleridPrefixAdmin(ImportMixin, admin.ModelAdmin): |
480
|
|
|
list_display = ['calleridprefixlist', |
481
|
|
|
'prefix', |
482
|
|
|
'date_added', |
483
|
|
|
'date_modified'] |
484
|
|
|
ordering = ('calleridprefixlist', |
485
|
|
|
'prefix') |
486
|
|
|
list_filter = ('calleridprefixlist', ) |
487
|
|
|
search_fields = ('^prefix', ) |
488
|
|
|
resource_class = CalleridPrefixResource |
489
|
|
|
|
490
|
|
|
def has_change_permission(self, request, obj=None): |
491
|
|
|
if request.user.is_superuser: |
492
|
|
|
return True |
493
|
|
|
else: |
494
|
|
|
return False |
495
|
|
|
|
496
|
|
|
def get_import_formats(self): |
497
|
|
|
format_csv = DEFAULT_FORMATS |
498
|
|
|
return [f for f in format_csv if f().can_import()] |
499
|
|
|
|
500
|
|
|
|
501
|
|
|
class CalleridPrefixListAdmin(admin.ModelAdmin): |
502
|
|
|
list_display = ['name', |
503
|
|
|
'description', |
504
|
|
|
'prefix', |
505
|
|
|
'date_added', |
506
|
|
|
'date_modified'] |
507
|
|
|
ordering = ('name', ) |
508
|
|
|
list_filter = ['name', ] |
509
|
|
|
search_fields = ('^name', |
510
|
|
|
'description') |
511
|
|
|
|
512
|
|
|
def has_change_permission(self, request, obj=None): |
513
|
|
|
if request.user.is_superuser: |
514
|
|
|
return True |
515
|
|
|
else: |
516
|
|
|
return False |
517
|
|
|
|
518
|
|
|
# Provider Rates |
519
|
|
|
|
520
|
|
|
|
521
|
|
|
class ProviderRatesFormSet(BaseInlineFormSet): |
522
|
|
|
|
523
|
|
|
def get_queryset(self): |
524
|
|
|
qs = super(ProviderRatesFormSet, self).get_queryset() |
525
|
|
|
return qs[:40] |
526
|
|
|
|
527
|
|
|
|
528
|
|
|
class ProviderRatesInline(admin.TabularInline): |
529
|
|
|
model = ProviderRates |
530
|
|
|
formset = ProviderRatesFormSet |
531
|
|
|
max_count = 40 |
532
|
|
|
extra = 1 |
533
|
|
|
|
534
|
|
|
|
535
|
|
View Code Duplication |
class ProviderTariffAdmin(admin.ModelAdmin): |
|
|
|
|
536
|
|
|
list_display = ['id', |
537
|
|
|
'name', |
538
|
|
|
'carrier', |
539
|
|
|
'prefix', |
540
|
|
|
'quality', |
541
|
|
|
'reliability', |
542
|
|
|
'date_start', |
543
|
|
|
'date_end', |
544
|
|
|
'get_boolean_display', |
545
|
|
|
'rates'] |
546
|
|
|
ordering = ['name', ] |
547
|
|
|
readonly_fields = ['id', ] |
548
|
|
|
form = ProviderTariffAdminForm |
549
|
|
|
|
550
|
|
|
def get_boolean_display(self, obj): |
551
|
|
|
if obj.enabled: |
552
|
|
|
return mark_safe('<span class="label label-success"><i class="icon-thumbs-up"></i> YES</span>') |
553
|
|
|
return mark_safe('<span class="label label-warning"><i class="icon-thumbs-down"></i> NO</span>') |
554
|
|
|
get_boolean_display.short_description = _(u'Enabled') |
555
|
|
|
get_boolean_display.admin_order_field = _(u'enabled') |
556
|
|
|
|
557
|
|
|
def has_change_permission(self, request, obj=None): |
558
|
|
|
if request.user.is_superuser: |
559
|
|
|
return True |
560
|
|
|
else: |
561
|
|
|
return False |
562
|
|
|
|
563
|
|
|
|
564
|
|
View Code Duplication |
class ProviderRatesAdmin(ImportExportMixin, admin.ModelAdmin): |
|
|
|
|
565
|
|
|
list_display = ['id', |
566
|
|
|
'provider_tariff', |
567
|
|
|
'destination', |
568
|
|
|
'digits', |
569
|
|
|
'cost_rate', |
570
|
|
|
'block_min_duration', |
571
|
|
|
'init_block', |
572
|
|
|
'get_boolean_display', |
573
|
|
|
'date_added', |
574
|
|
|
'date_modified'] |
575
|
|
|
ordering = ['provider_tariff', |
576
|
|
|
'digits'] |
577
|
|
|
list_filter = ['provider_tariff', |
578
|
|
|
'enabled',] |
579
|
|
|
search_fields = ['^digits', |
580
|
|
|
'^destination'] |
581
|
|
|
actions = ['make_enabled', |
582
|
|
|
'make_disabled'] |
583
|
|
|
form = ProviderRatesAdminForm |
584
|
|
|
|
585
|
|
|
def get_boolean_display(self, obj): |
586
|
|
|
if obj.enabled: |
587
|
|
|
return mark_safe('<span class="label label-success"><i class="icon-thumbs-up"></i> YES</span>') |
588
|
|
|
return mark_safe('<span class="label label-warning"><i class="icon-thumbs-down"></i> NO</span>') |
589
|
|
|
get_boolean_display.short_description = _(u'Enabled') |
590
|
|
|
get_boolean_display.admin_order_field = _(u'enabled') |
591
|
|
|
|
592
|
|
|
def has_change_permission(self, request, obj=None): |
593
|
|
|
if request.user.is_superuser: |
594
|
|
|
return True |
595
|
|
|
else: |
596
|
|
|
return False |
597
|
|
|
|
598
|
|
|
def make_enabled(self, request, queryset): |
599
|
|
|
rows_updated = queryset.update(enabled=True) |
600
|
|
|
if rows_updated == 1: |
601
|
|
|
message_bit = _(u"1 item was") |
602
|
|
|
else: |
603
|
|
|
message_bit = _(u"%s items were") % rows_updated |
604
|
|
|
self.message_user(request, _(u"%s successfully marked as enabled.") % message_bit) |
605
|
|
|
make_enabled.short_description = _(u"mark selected items as enabled") |
606
|
|
|
|
607
|
|
|
def make_disabled(self, request, queryset): |
608
|
|
|
rows_updated = queryset.update(enabled=False) |
609
|
|
|
if rows_updated == 1: |
610
|
|
|
message_bit = _(u"1 item was") |
611
|
|
|
else: |
612
|
|
|
message_bit = _(u"%s items were") % rows_updated |
613
|
|
|
self.message_user(request, _(u"%s successfully marked as disabled.") % message_bit) |
614
|
|
|
make_disabled.short_description = _(u"mark selected items as disabled") |
615
|
|
|
|
616
|
|
|
def get_import_formats(self): |
617
|
|
|
format_csv = DEFAULT_FORMATS |
618
|
|
|
return [f for f in format_csv if f().can_import()] |
619
|
|
|
|
620
|
|
|
def get_export_formats(self): |
621
|
|
|
format_csv = DEFAULT_FORMATS |
622
|
|
|
return [f for f in format_csv if f().can_export()] |
623
|
|
|
|
624
|
|
|
# LCR |
625
|
|
|
|
626
|
|
|
|
627
|
|
|
class LCRProvidersInline(admin.TabularInline): |
628
|
|
|
model = LCRProviders |
629
|
|
|
# formset = LCRProvidersFormSet |
630
|
|
|
# fields = ('rates',) |
631
|
|
|
extra = 0 |
632
|
|
|
|
633
|
|
|
|
634
|
|
|
class LCRGroupAdmin(admin.ModelAdmin): |
635
|
|
|
list_display = ['name', 'description', 'lcrtype'] |
636
|
|
|
ordering = ('name', 'lcrtype') |
637
|
|
|
list_filter = ('lcrtype',) |
638
|
|
|
inlines = [ |
639
|
|
|
LCRProvidersInline, |
640
|
|
|
] |
641
|
|
|
|
642
|
|
|
def has_change_permission(self, request, obj=None): |
643
|
|
|
if request.user.is_superuser: |
644
|
|
|
return True |
645
|
|
|
else: |
646
|
|
|
return False |
647
|
|
|
|
648
|
|
|
|
649
|
|
|
class LCRProvidersAdmin(admin.ModelAdmin): |
650
|
|
|
list_display = ['lcr', 'provider_tariff', 'rates'] |
651
|
|
|
list_filter = ('lcr',) |
652
|
|
|
|
653
|
|
|
def has_change_permission(self, request, obj=None): |
654
|
|
|
if request.user.is_superuser: |
655
|
|
|
return True |
656
|
|
|
else: |
657
|
|
|
return False |
658
|
|
|
|
659
|
|
|
# Customer Rates |
660
|
|
|
|
661
|
|
|
|
662
|
|
|
class CustomerRatesFormSet(BaseInlineFormSet): |
663
|
|
|
def get_queryset(self): |
664
|
|
|
qs = super(CustomerRatesFormSet, self).get_queryset() |
665
|
|
|
return qs[:40] |
666
|
|
|
|
667
|
|
|
|
668
|
|
|
class CustomerRatesInline(admin.TabularInline): |
669
|
|
|
model = CustomerRates |
670
|
|
|
#formset = CustomerRatesFormSet |
671
|
|
|
max_num = 40 |
672
|
|
|
extra = 1 |
673
|
|
|
|
674
|
|
|
|
675
|
|
View Code Duplication |
class CustomerRatesAdmin(ImportExportMixin, admin.ModelAdmin): |
|
|
|
|
676
|
|
|
list_display = ['id', |
677
|
|
|
'ratecard', |
678
|
|
|
'destination', |
679
|
|
|
'prefix', |
680
|
|
|
'destnum_length', |
681
|
|
|
'rate', |
682
|
|
|
'init_block', |
683
|
|
|
'block_min_duration', |
684
|
|
|
'minimal_time', |
685
|
|
|
'get_boolean_display', |
686
|
|
|
] |
687
|
|
|
ordering = ['ratecard', |
688
|
|
|
'prefix'] |
689
|
|
|
list_filter = ['ratecard', |
690
|
|
|
'enabled'] |
691
|
|
|
search_fields = ['^prefix', |
692
|
|
|
'^destination'] |
693
|
|
|
actions = ['make_enabled', 'make_disabled'] |
694
|
|
|
readonly_fields = ['id', ] |
695
|
|
|
form = CustomerRatesAdminForm |
696
|
|
|
|
697
|
|
|
def get_boolean_display(self, obj): |
698
|
|
|
if obj.enabled: |
699
|
|
|
return mark_safe('<span class="label label-success"><i class="icon-thumbs-up"></i> YES</span>') |
700
|
|
|
return mark_safe('<span class="label label-warning"><i class="icon-thumbs-down"></i> NO</span>') |
701
|
|
|
get_boolean_display.short_description = _(u'Enabled') |
702
|
|
|
get_boolean_display.admin_order_field = _(u'enabled') |
703
|
|
|
|
704
|
|
|
def has_change_permission(self, request, obj=None): |
705
|
|
|
if request.user.is_superuser: |
706
|
|
|
return True |
707
|
|
|
else: |
708
|
|
|
return False |
709
|
|
|
|
710
|
|
|
def make_enabled(self, request, queryset): |
711
|
|
|
rows_updated = queryset.update(enabled=True) |
712
|
|
|
if rows_updated == 1: |
713
|
|
|
message_bit = _(u"1 item was") |
714
|
|
|
else: |
715
|
|
|
message_bit = _(u"%s items were") % rows_updated |
716
|
|
|
self.message_user(request, |
717
|
|
|
_(u"%s successfully marked as enabled.") % message_bit) |
718
|
|
|
make_enabled.short_description = _(u"mark selected items as enabled") |
719
|
|
|
|
720
|
|
|
def make_disabled(self, request, queryset): |
721
|
|
|
rows_updated = queryset.update(enabled=False) |
722
|
|
|
if rows_updated == 1: |
723
|
|
|
message_bit = _(u"1 item was") |
724
|
|
|
else: |
725
|
|
|
message_bit = _(u"%s items were") % rows_updated |
726
|
|
|
self.message_user(request, |
727
|
|
|
_(u"%s successfully marked as disabled.") % message_bit) |
728
|
|
|
make_disabled.short_description = _(u"mark selected items as disabled") |
729
|
|
|
|
730
|
|
|
def get_import_formats(self): |
731
|
|
|
format_csv = DEFAULT_FORMATS |
732
|
|
|
return [f for f in format_csv if f().can_import()] |
733
|
|
|
|
734
|
|
|
def get_export_formats(self): |
735
|
|
|
format_csv = DEFAULT_FORMATS |
736
|
|
|
return [f for f in format_csv if f().can_export()] |
737
|
|
|
|
738
|
|
|
|
739
|
|
View Code Duplication |
class RateCardAdmin(admin.ModelAdmin): |
|
|
|
|
740
|
|
|
list_display = ['id', |
741
|
|
|
'name', |
742
|
|
|
'rctype', |
743
|
|
|
'lcrgroup', |
744
|
|
|
'lcr', |
745
|
|
|
'get_boolean_display', |
746
|
|
|
'rates', |
747
|
|
|
'date_start', |
748
|
|
|
'date_end', |
749
|
|
|
'callerid_filter', |
750
|
|
|
'callerid_list'] |
751
|
|
|
ordering = ['name', 'enabled', 'lcrgroup'] |
752
|
|
|
list_filter = ['enabled', 'lcrgroup'] |
753
|
|
|
search_fields = ['description', '^name'] |
754
|
|
|
form = RateCardAdminForm |
755
|
|
|
# inlines = [ |
756
|
|
|
# CustomerRatesInline, |
757
|
|
|
# ] |
758
|
|
|
|
759
|
|
|
def has_change_permission(self, request, obj=None): |
760
|
|
|
if request.user.is_superuser: |
761
|
|
|
return True |
762
|
|
|
else: |
763
|
|
|
return False |
764
|
|
|
|
765
|
|
|
def get_boolean_display(self, obj): |
766
|
|
|
if obj.enabled: |
767
|
|
|
return mark_safe('<span class="label label-success"><i class="icon-thumbs-up"></i> YES</span>') |
768
|
|
|
return mark_safe('<span class="label label-warning"><i class="icon-thumbs-down"></i> NO</span>') |
769
|
|
|
get_boolean_display.short_description = _(u'Enabled') |
770
|
|
|
get_boolean_display.admin_order_field = _(u'enabled') |
771
|
|
|
|
772
|
|
|
|
773
|
|
|
class CustomerRateCardsAdmin(admin.ModelAdmin): # (SortableModelAdmin): |
774
|
|
|
list_display = ['company', |
775
|
|
|
'ratecard', |
776
|
|
|
'tech_prefix', |
777
|
|
|
'priority', |
778
|
|
|
'description'] |
779
|
|
|
ordering = ['company', ] |
780
|
|
|
raw_id_fields = ('ratecard',) |
781
|
|
|
list_filter = ['ratecard', 'company'] |
782
|
|
|
search_fields = ['^company__company', ] |
783
|
|
|
form = form = CustomerRateCardsAdminForm |
784
|
|
|
|
785
|
|
|
def has_change_permission(self, request, obj=None): |
786
|
|
|
if request.user.is_superuser: |
787
|
|
|
return True |
788
|
|
|
else: |
789
|
|
|
return False |
790
|
|
|
|
791
|
|
|
|
792
|
|
|
# VoipSwitch |
793
|
|
|
|
794
|
|
|
class VoipSwitchProfileInline(admin.TabularInline): |
795
|
|
|
# form = RoutesDidForm |
796
|
|
|
description = 'FS profiles' |
797
|
|
|
model = VoipSwitchProfile |
798
|
|
|
modal = True |
799
|
|
|
extra = 0 |
800
|
|
|
collapse = False |
801
|
|
|
|
802
|
|
|
|
803
|
|
|
class VoipSwitchAdmin(admin.ModelAdmin): |
804
|
|
|
list_display = ['name', |
805
|
|
|
'ip', |
806
|
|
|
'date_added', |
807
|
|
|
'date_modified'] |
808
|
|
|
ordering = ['name', ] |
809
|
|
|
|
810
|
|
|
def has_change_permission(self, request, obj=None): |
811
|
|
|
if request.user.is_superuser: |
812
|
|
|
return True |
813
|
|
|
else: |
814
|
|
|
return False |
815
|
|
|
|
816
|
|
|
# SofiaGateway |
817
|
|
|
|
818
|
|
|
|
819
|
|
|
class SofiaGatewayAdmin(admin.ModelAdmin): |
820
|
|
|
list_display = ['name', |
821
|
|
|
'company', |
822
|
|
|
'channels', |
823
|
|
|
'proxy', |
824
|
|
|
'transport', |
825
|
|
|
'sip_port', |
826
|
|
|
'get_enabled_display', |
827
|
|
|
'get_register_display', |
828
|
|
|
'date_modified'] |
829
|
|
|
ordering = ['company', |
830
|
|
|
'name', |
831
|
|
|
'proxy'] |
832
|
|
|
list_filter = ['company', |
833
|
|
|
'enabled'] |
834
|
|
|
search_fields = ['^company__name', |
835
|
|
|
'proxy'] |
836
|
|
|
# actions = [sofiaupdate] |
837
|
|
|
|
838
|
|
|
def get_enabled_display(self, obj): |
839
|
|
|
if obj.enabled: |
840
|
|
|
return mark_safe('<span class="label label-success"><i class="icon-thumbs-up"></i> YES</span>') |
841
|
|
|
return mark_safe('<span class="label label-danger"><i class="icon-thumbs-down"></i> NO</span>') |
842
|
|
|
get_enabled_display.short_description = _(u'Enabled') |
843
|
|
|
get_enabled_display.admin_order_field = _(u'enabled') |
844
|
|
|
|
845
|
|
|
def get_register_display(self, obj): |
846
|
|
|
if obj.register: |
847
|
|
|
return mark_safe('<span class="label label-info"><i class="icon-thumbs-up"></i> ON</span>') |
848
|
|
|
return mark_safe('<span class="label label-danger"><i class="icon-thumbs-down"></i> OFF</span>') |
849
|
|
|
get_register_display.short_description = _(u'Register') |
850
|
|
|
get_register_display.admin_order_field = _(u'register') |
851
|
|
|
|
852
|
|
|
def has_change_permission(self, request, obj=None): |
853
|
|
|
if request.user.is_superuser: |
854
|
|
|
return True |
855
|
|
|
else: |
856
|
|
|
return False |
857
|
|
|
|
858
|
|
|
|
859
|
|
|
class SipProfileAdmin(admin.ModelAdmin): |
860
|
|
|
list_display = ['name', |
861
|
|
|
'ext_sip_ip', |
862
|
|
|
'sip_ip', |
863
|
|
|
'sip_port', |
864
|
|
|
'auth_calls', |
865
|
|
|
'apply_inbound_acl', |
866
|
|
|
'disable_register', |
867
|
|
|
'log_auth_failures', |
868
|
|
|
'disable_transcoding', |
869
|
|
|
'enabled', |
870
|
|
|
'date_modified'] |
871
|
|
|
ordering = ['name', ] |
872
|
|
|
list_filter = ['sip_port', ] |
873
|
|
|
search_fields = ['^name', ] |
874
|
|
|
inlines = [VoipSwitchProfileInline, ] |
875
|
|
|
fieldsets = ( |
876
|
|
|
(_(u'Basic settings'), { |
877
|
|
|
'fields': (('name', 'sip_port'), |
878
|
|
|
('sip_ip', 'rtp_ip'), |
879
|
|
|
('ext_sip_ip', 'ext_rtp_ip'), |
880
|
|
|
'enabled'), |
881
|
|
|
'description': _(u'General sip profile informations') |
882
|
|
|
}), |
883
|
|
|
# ('Media Related Options', { |
884
|
|
|
# 'fields': (''), |
885
|
|
|
# 'classes': ('collapse',), |
886
|
|
|
# 'description': 'Manage Media Related Options' |
887
|
|
|
# }), |
888
|
|
|
(_(u'Codecs Related Options'), { |
889
|
|
|
'fields': ('disable_transcoding', |
890
|
|
|
('inbound_codec_prefs', 'outbound_codec_prefs')), |
891
|
|
|
'classes': ('collapse',), |
892
|
|
|
'description': _(u'Manage Codecs Related Options') |
893
|
|
|
}), |
894
|
|
|
(_(u'NAT'), { |
895
|
|
|
'fields': ('aggressive_nat_detection', |
896
|
|
|
'NDLB_rec_in_nat_reg_c', |
897
|
|
|
'NDLB_force_rport', |
898
|
|
|
'NDLB_broken_auth_hash'), |
899
|
|
|
'classes': ('collapse',), |
900
|
|
|
'description': _(u'NAT management') |
901
|
|
|
}), |
902
|
|
|
(_(u'DTMF Related options'), { |
903
|
|
|
'fields': ('pass_rfc2833',), |
904
|
|
|
'classes': ('collapse',), |
905
|
|
|
'description': _(u'DTMF management') |
906
|
|
|
}), |
907
|
|
|
(_(u'SIP Related Options'), { |
908
|
|
|
'fields': (('enable_timer', 'session_timeout')), |
909
|
|
|
'classes': ('collapse',), |
910
|
|
|
'description': _(u'Manage SIP Related Options') |
911
|
|
|
}), |
912
|
|
|
(_(u'RTP Related Options'), { |
913
|
|
|
'fields': ('rtp_rewrite_timestamps',), |
914
|
|
|
'classes': ('collapse',), |
915
|
|
|
'description': _(u'Manage RTP Related Options') |
916
|
|
|
}), |
917
|
|
|
(_(u'Authentification Authorization'), { |
918
|
|
|
'fields': ('apply_inbound_acl', |
919
|
|
|
'auth_calls', |
920
|
|
|
'log_auth_failures'), |
921
|
|
|
'classes': ('collapse',), |
922
|
|
|
'description': _(u'Authentification Authorization management') |
923
|
|
|
}), |
924
|
|
|
(_(u'Registration'), { |
925
|
|
|
'fields': ('disable_register', |
926
|
|
|
'accept_blind_reg'), |
927
|
|
|
'classes': ('collapse',), |
928
|
|
|
'description': _(u'Registration settings management') |
929
|
|
|
}), |
930
|
|
|
(_(u'Others'), { |
931
|
|
|
'fields': ('user_agent',), |
932
|
|
|
'classes': ('collapse',), |
933
|
|
|
'description': _(u'Others parameters') |
934
|
|
|
}), |
935
|
|
|
) |
936
|
|
|
|
937
|
|
|
def has_change_permission(self, request, obj=None): |
938
|
|
|
if request.user.is_superuser: |
939
|
|
|
return True |
940
|
|
|
else: |
941
|
|
|
return False |
942
|
|
|
|
943
|
|
|
# AclLists |
944
|
|
|
|
945
|
|
|
|
946
|
|
|
class AclNodesAdminInline(admin.ModelAdmin): |
947
|
|
|
list_display = ('company', 'cidr', 'policy', 'list') |
948
|
|
|
|
949
|
|
|
|
950
|
|
|
class AclListsAdmin(admin.ModelAdmin): |
951
|
|
|
list_display = ('acl_name', 'default_policy') |
952
|
|
|
ordering = ['acl_name', 'default_policy'] |
953
|
|
|
list_filter = ['default_policy', ] |
954
|
|
|
inlines = [AclNodesAdminInline, ] |
955
|
|
|
|
956
|
|
|
def has_change_permission(self, request, obj=None): |
957
|
|
|
if request.user.is_superuser: |
958
|
|
|
return True |
959
|
|
|
else: |
960
|
|
|
return False |
961
|
|
|
|
962
|
|
|
# AclNodes |
963
|
|
|
|
964
|
|
|
|
965
|
|
|
class AclNodesAdmin(admin.ModelAdmin): |
966
|
|
|
list_display = ('company', 'cidr', 'policy', 'list') |
967
|
|
|
ordering = ['company', 'policy', 'cidr'] |
968
|
|
|
list_filter = ['company', 'list'] |
969
|
|
|
search_fields = ['cidr', ] |
970
|
|
|
|
971
|
|
|
def has_change_permission(self, request, obj=None): |
972
|
|
|
if request.user.is_superuser: |
973
|
|
|
return True |
974
|
|
|
else: |
975
|
|
|
return False |
976
|
|
|
|
977
|
|
|
# Hangup Cause |
978
|
|
|
|
979
|
|
|
|
980
|
|
|
class HangupCauseAdmin(ImportExportMixin, admin.ModelAdmin): |
981
|
|
|
list_display = ('id', 'code', 'enumeration', 'cause', 'description') |
982
|
|
|
search_fields = ('code', 'enumeration') |
983
|
|
|
|
984
|
|
|
def has_change_permission(self, request, obj=None): |
985
|
|
|
if request.user.is_superuser: |
986
|
|
|
return True |
987
|
|
|
else: |
988
|
|
|
return False |
989
|
|
|
|
990
|
|
|
|
991
|
|
|
class CarrierNormalizationRulesAdmin(admin.ModelAdmin): |
992
|
|
|
list_display = ('company', 'prefix', 'remove_prefix', 'add_prefix') |
993
|
|
|
ordering = ('company', 'prefix') |
994
|
|
|
search_fields = ('^prefix',) |
995
|
|
|
|
996
|
|
|
def has_change_permission(self, request, obj=None): |
997
|
|
|
if request.user.is_superuser: |
998
|
|
|
return True |
999
|
|
|
else: |
1000
|
|
|
return False |
1001
|
|
|
|
1002
|
|
|
|
1003
|
|
|
class CustomerNormalizationRulesAdmin(admin.ModelAdmin): |
1004
|
|
|
list_display = ('company', 'prefix', 'remove_prefix', 'add_prefix') |
1005
|
|
|
ordering = ('company', 'prefix') |
1006
|
|
|
search_fields = ('^prefix',) |
1007
|
|
|
|
1008
|
|
|
def has_change_permission(self, request, obj=None): |
1009
|
|
|
if request.user.is_superuser: |
1010
|
|
|
return True |
1011
|
|
|
else: |
1012
|
|
|
return False |
1013
|
|
|
|
1014
|
|
|
|
1015
|
|
|
class CarrierCIDNormalizationRulesAdmin(admin.ModelAdmin): |
1016
|
|
|
list_display = ('company', 'prefix', 'remove_prefix', 'add_prefix') |
1017
|
|
|
ordering = ('company',) |
1018
|
|
|
|
1019
|
|
|
def has_change_permission(self, request, obj=None): |
1020
|
|
|
if request.user.is_superuser: |
1021
|
|
|
return True |
1022
|
|
|
else: |
1023
|
|
|
return False |
1024
|
|
|
|
1025
|
|
|
|
1026
|
|
|
class CustomerCIDNormalizationRulesAdmin(admin.ModelAdmin): |
1027
|
|
|
list_display = ('company', 'prefix', 'remove_prefix', 'add_prefix') |
1028
|
|
|
ordering = ('company',) |
1029
|
|
|
|
1030
|
|
|
def has_change_permission(self, request, obj=None): |
1031
|
|
|
if request.user.is_superuser: |
1032
|
|
|
return True |
1033
|
|
|
else: |
1034
|
|
|
return False |
1035
|
|
|
|
1036
|
|
|
|
1037
|
|
|
class DestinationNumberRulesAdmin(admin.ModelAdmin): |
1038
|
|
|
list_display = ('prefix', 'format_num', 'description') |
1039
|
|
|
ordering = ('prefix',) |
1040
|
|
|
|
1041
|
|
|
def has_change_permission(self, request, obj=None): |
1042
|
|
|
if request.user.is_superuser: |
1043
|
|
|
return True |
1044
|
|
|
else: |
1045
|
|
|
return False |
1046
|
|
|
|
1047
|
|
|
|
1048
|
|
|
class DimCustomerDestinationAdmin(admin.ModelAdmin): |
1049
|
|
|
list_display = ('date', 'customer', 'destination', 'total_calls', 'success_calls', 'total_duration', 'avg_duration', 'max_duration', 'min_duration', 'total_sell', 'total_cost') |
1050
|
|
|
readonly_fields = ('date', 'customer', 'destination', 'total_calls', 'success_calls', 'total_duration', 'avg_duration', 'max_duration', 'min_duration', 'total_sell', 'total_cost') |
1051
|
|
|
list_filter = ('date', 'customer', 'destination') |
1052
|
|
|
ordering = ('-date', 'customer', 'destination') |
1053
|
|
|
|
1054
|
|
|
def has_add_permission(self, request, obj=None): |
1055
|
|
|
return False |
1056
|
|
|
|
1057
|
|
|
def has_delete_permission(self, request, obj=None): |
1058
|
|
|
return False |
1059
|
|
|
|
1060
|
|
|
|
1061
|
|
|
class DimProviderDestinationAdmin(admin.ModelAdmin): |
1062
|
|
|
list_display = ('date', 'provider', 'destination', 'total_calls', 'success_calls', 'total_duration', 'avg_duration', 'max_duration', 'min_duration', 'total_sell', 'total_cost') |
1063
|
|
|
readonly_fields = ('date', 'provider', 'destination', 'total_calls', 'success_calls', 'total_duration', 'avg_duration', 'max_duration', 'min_duration', 'total_sell', 'total_cost') |
1064
|
|
|
list_filter = ('date', 'provider', 'destination') |
1065
|
|
|
ordering = ('-date', 'provider', 'destination') |
1066
|
|
|
# list_display = ('get_day_stats',) |
1067
|
|
|
|
1068
|
|
|
def has_add_permission(self, request, obj=None): |
1069
|
|
|
return False |
1070
|
|
|
|
1071
|
|
|
def has_delete_permission(self, request, obj=None): |
1072
|
|
|
return False |
1073
|
|
|
|
1074
|
|
|
|
1075
|
|
View Code Duplication |
class SaleSummaryAdmin(admin.ModelAdmin): |
|
|
|
|
1076
|
|
|
change_list_template = 'admin/sale_summary_change_list.html' |
1077
|
|
|
date_hierarchy = 'date__date' |
1078
|
|
|
search_fields = ('^customer__name',) |
1079
|
|
|
|
1080
|
|
|
# orderable |
1081
|
|
|
|
1082
|
|
|
list_filter = ( |
1083
|
|
|
'destination', |
1084
|
|
|
'customer__name', |
1085
|
|
|
) |
1086
|
|
|
|
1087
|
|
|
def has_add_permission(self, request, obj=None): |
1088
|
|
|
return False |
1089
|
|
|
|
1090
|
|
|
def has_delete_permission(self, request, obj=None): |
1091
|
|
|
return False |
1092
|
|
|
|
1093
|
|
|
def changelist_view(self, request, extra_context=None): |
1094
|
|
|
response = super(SaleSummaryAdmin, self).changelist_view( |
1095
|
|
|
request, |
1096
|
|
|
extra_context=extra_context, |
1097
|
|
|
) |
1098
|
|
|
|
1099
|
|
|
def get_next_in_date_hierarchy(request, date_hierarchy): |
1100
|
|
|
if date_hierarchy + '__day' in request.GET: |
1101
|
|
|
return 'day' |
1102
|
|
|
|
1103
|
|
|
if date_hierarchy + '__month' in request.GET: |
1104
|
|
|
return 'day' |
1105
|
|
|
|
1106
|
|
|
if date_hierarchy + '__year' in request.GET: |
1107
|
|
|
return 'week' |
1108
|
|
|
|
1109
|
|
|
return 'month' |
1110
|
|
|
|
1111
|
|
|
try: |
1112
|
|
|
qs = response.context_data['cl'].queryset |
1113
|
|
|
except (AttributeError, KeyError): |
1114
|
|
|
return response |
1115
|
|
|
|
1116
|
|
|
metrics = { |
1117
|
|
|
'total': Coalesce(Count('id'), V(0)), # not OK |
1118
|
|
|
'total_sales': Coalesce(Sum('total_sell'), V(0)), |
1119
|
|
|
'margin': Coalesce(Sum('total_sell'), V(0)) - Coalesce(Sum('total_cost'), V(0)), |
1120
|
|
|
'total_calls': Coalesce(Sum('total_calls'), V(0)), |
1121
|
|
|
'success_calls': Coalesce(Sum('success_calls'), V(0)), |
1122
|
|
|
'total_duration': Coalesce(Sum('total_duration'), V(0)), |
1123
|
|
|
'max_duration': Coalesce(Max('max_duration'), V(0)), |
1124
|
|
|
'min_duration': Coalesce(Min('min_duration'), V(0)), |
1125
|
|
|
} |
1126
|
|
|
|
1127
|
|
|
summary_stats = qs\ |
1128
|
|
|
.values('customer__name')\ |
1129
|
|
|
.annotate(**metrics)\ |
1130
|
|
|
.order_by('-total_sales') |
1131
|
|
|
|
1132
|
|
|
response.context_data['summary'] = list(summary_stats) |
1133
|
|
|
|
1134
|
|
|
summary_totals = dict( |
1135
|
|
|
qs.aggregate(**metrics) |
1136
|
|
|
) |
1137
|
|
|
response.context_data['summary_total'] = summary_totals |
1138
|
|
|
|
1139
|
|
|
period = get_next_in_date_hierarchy( |
1140
|
|
|
request, |
1141
|
|
|
self.date_hierarchy, |
1142
|
|
|
) |
1143
|
|
|
response.context_data['period'] = period |
1144
|
|
|
|
1145
|
|
|
metrics2 = { |
1146
|
|
|
'total_sales': Coalesce(Sum('total_sell'), V(0)), |
1147
|
|
|
'margin': Coalesce(Sum('total_sell'), V(0)) - Coalesce(Sum('total_cost'), V(0)), |
1148
|
|
|
'total_calls': Coalesce(Sum('total_calls'), V(0)), |
1149
|
|
|
'success_calls': Coalesce(Sum('success_calls'), V(0)), |
1150
|
|
|
'total_duration': Coalesce(Sum('total_duration'), V(0)), |
1151
|
|
|
} |
1152
|
|
|
|
1153
|
|
|
summary_over_time = qs.annotate( |
1154
|
|
|
period=Trunc( |
1155
|
|
|
'date__date', |
1156
|
|
|
period, |
1157
|
|
|
output_field=DateTimeField() |
1158
|
|
|
) |
1159
|
|
|
).values('period').annotate(**metrics2).order_by('period') |
1160
|
|
|
|
1161
|
|
|
# generate data for graph |
1162
|
|
|
|
1163
|
|
|
# revenue repartition / 5 customers |
1164
|
|
|
chart_label = [] |
1165
|
|
|
chart_data = [] |
1166
|
|
|
top5sales = 0 |
1167
|
|
|
if len(summary_stats) < 5: |
1168
|
|
|
max_cust = len(summary_stats) |
1169
|
|
|
else: |
1170
|
|
|
max_cust = 5 |
1171
|
|
|
for i in range(max_cust): |
1172
|
|
|
chart_label.append(summary_stats[i]['customer__name']) |
1173
|
|
|
chart_data.append(int(summary_stats[i]['total_sales'])) |
1174
|
|
|
top5sales = top5sales + int(summary_stats[i]['total_sales']) |
1175
|
|
|
|
1176
|
|
|
# Add others to graphs |
1177
|
|
|
chart_label.append(_(u'others')) |
1178
|
|
|
chart_data.append(int(summary_totals['total_sales']) - top5sales) |
1179
|
|
|
|
1180
|
|
|
# transform unicode text to strings |
1181
|
|
|
response.context_data['chart_label'] = map(str, chart_label) |
1182
|
|
|
# Example of datas ["Africa", "Asia", "Europe", "Latin America", "North America"] |
1183
|
|
|
response.context_data['chart_data'] = chart_data |
1184
|
|
|
# Example of datas [2478,5267,734,784,433] |
1185
|
|
|
# response.context_data['chart_color'] = ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850"] |
1186
|
|
|
|
1187
|
|
|
# timeseries stats |
1188
|
|
|
callvolume_data = [] |
1189
|
|
|
successcallvolume_data = [] |
1190
|
|
|
timeserie_data = [] |
1191
|
|
|
revenue_data = [] |
1192
|
|
|
margin_data = [] |
1193
|
|
|
for j in range(len(summary_over_time)): |
1194
|
|
|
callvolume_data.append(int(summary_over_time[j]['total_calls'])) |
1195
|
|
|
successcallvolume_data.append(int(summary_over_time[j]['success_calls'])) |
1196
|
|
|
revenue_data.append(int(summary_over_time[j]['total_sales'])) |
1197
|
|
|
margin_data.append(int(summary_over_time[j]['margin'])) |
1198
|
|
|
if period == 'day': |
1199
|
|
|
timeserie_data.append(summary_over_time[j]['period'].strftime('%Y-%m-%d')) |
1200
|
|
|
elif period == 'week': |
1201
|
|
|
timeserie_data.append(summary_over_time[j]['period'].strftime('%W')) |
1202
|
|
|
else: |
1203
|
|
|
timeserie_data.append(summary_over_time[j]['period'].strftime('%Y-%b')) |
1204
|
|
|
|
1205
|
|
|
response.context_data['callvolume_data'] = callvolume_data |
1206
|
|
|
response.context_data['successcallvolume_data'] = successcallvolume_data |
1207
|
|
|
response.context_data['revenue_data'] = revenue_data |
1208
|
|
|
response.context_data['margin_data'] = margin_data |
1209
|
|
|
response.context_data['timeserie_data'] = timeserie_data |
1210
|
|
|
response.context_data['summary_over_time'] = period |
1211
|
|
|
|
1212
|
|
|
return response |
1213
|
|
|
|
1214
|
|
|
|
1215
|
|
View Code Duplication |
class CostSummaryAdmin(admin.ModelAdmin): |
|
|
|
|
1216
|
|
|
change_list_template = 'admin/cost_summary_change_list.html' |
1217
|
|
|
date_hierarchy = 'date__date' |
1218
|
|
|
search_fields = ('^provider__name',) |
1219
|
|
|
|
1220
|
|
|
# orderable |
1221
|
|
|
|
1222
|
|
|
list_filter = ( |
1223
|
|
|
'destination', |
1224
|
|
|
'provider__name', |
1225
|
|
|
) |
1226
|
|
|
|
1227
|
|
|
def has_add_permission(self, request, obj=None): |
1228
|
|
|
return False |
1229
|
|
|
|
1230
|
|
|
def has_delete_permission(self, request, obj=None): |
1231
|
|
|
return False |
1232
|
|
|
|
1233
|
|
|
def changelist_view(self, request, extra_context=None): |
1234
|
|
|
response = super(CostSummaryAdmin, self).changelist_view( |
1235
|
|
|
request, |
1236
|
|
|
extra_context=extra_context, |
1237
|
|
|
) |
1238
|
|
|
|
1239
|
|
|
def get_next_in_date_hierarchy(request, date_hierarchy): |
1240
|
|
|
if date_hierarchy + '__day' in request.GET: |
1241
|
|
|
return 'day' |
1242
|
|
|
|
1243
|
|
|
if date_hierarchy + '__month' in request.GET: |
1244
|
|
|
return 'day' |
1245
|
|
|
|
1246
|
|
|
if date_hierarchy + '__year' in request.GET: |
1247
|
|
|
return 'week' |
1248
|
|
|
|
1249
|
|
|
return 'month' |
1250
|
|
|
|
1251
|
|
|
try: |
1252
|
|
|
qs = response.context_data['cl'].queryset |
1253
|
|
|
except (AttributeError, KeyError): |
1254
|
|
|
return response |
1255
|
|
|
|
1256
|
|
|
metrics = { |
1257
|
|
|
'total': Coalesce(Count('id'), V(0)), # not OK |
1258
|
|
|
'total_buy': Coalesce(Sum('total_cost'), V(0)), |
1259
|
|
|
'margin': Coalesce(Sum('total_sell'), V(0)) - Coalesce(Sum('total_cost'), V(0)), |
1260
|
|
|
'total_calls': Coalesce(Sum('total_calls'), V(0)), |
1261
|
|
|
'success_calls': Coalesce(Sum('success_calls'), V(0)), |
1262
|
|
|
'total_duration': Coalesce(Sum('total_duration'), V(0)), |
1263
|
|
|
'max_duration': Coalesce(Max('max_duration'), V(0)), |
1264
|
|
|
'min_duration': Coalesce(Min('min_duration'), V(0)), |
1265
|
|
|
} |
1266
|
|
|
|
1267
|
|
|
summary_stats = qs\ |
1268
|
|
|
.values('provider__name')\ |
1269
|
|
|
.annotate(**metrics)\ |
1270
|
|
|
.order_by('-total_buy') |
1271
|
|
|
|
1272
|
|
|
response.context_data['summary'] = list(summary_stats) |
1273
|
|
|
|
1274
|
|
|
# Calculate total line |
1275
|
|
|
summary_totals = dict( |
1276
|
|
|
qs.aggregate(**metrics) |
1277
|
|
|
) |
1278
|
|
|
response.context_data['summary_total'] = summary_totals |
1279
|
|
|
|
1280
|
|
|
period = get_next_in_date_hierarchy( |
1281
|
|
|
request, |
1282
|
|
|
self.date_hierarchy, |
1283
|
|
|
) |
1284
|
|
|
response.context_data['period'] = period |
1285
|
|
|
|
1286
|
|
|
# generate period data for graph |
1287
|
|
|
metrics2 = { |
1288
|
|
|
'total_buy': Coalesce(Sum('total_cost'), V(0)), |
1289
|
|
|
'margin': Coalesce(Sum('total_sell'), V(0)) - Coalesce(Sum('total_cost'), V(0)), |
1290
|
|
|
'total_calls': Coalesce(Sum('total_calls'), V(0)), |
1291
|
|
|
'success_calls': Coalesce(Sum('success_calls'), V(0)), |
1292
|
|
|
'total_duration': Coalesce(Sum('total_duration'), V(0)), |
1293
|
|
|
} |
1294
|
|
|
|
1295
|
|
|
summary_over_time = qs.annotate( |
1296
|
|
|
period=Trunc( |
1297
|
|
|
'date__date', |
1298
|
|
|
period, |
1299
|
|
|
output_field=DateTimeField() |
1300
|
|
|
) |
1301
|
|
|
).values('period').annotate(**metrics2).order_by('period') |
1302
|
|
|
|
1303
|
|
|
# generate data for graph |
1304
|
|
|
|
1305
|
|
|
# revenue repartition / 5 customers |
1306
|
|
|
chart_label = [] |
1307
|
|
|
chart_data = [] |
1308
|
|
|
top5sales = 0 |
1309
|
|
|
if len(summary_stats) < 5: |
1310
|
|
|
max_cust = len(summary_stats) |
1311
|
|
|
else: |
1312
|
|
|
max_cust = 5 |
1313
|
|
|
for i in range(max_cust): |
1314
|
|
|
chart_label.append(summary_stats[i]['provider__name']) |
1315
|
|
|
chart_data.append(int(summary_stats[i]['total_buy'])) |
1316
|
|
|
top5sales = top5sales + int(summary_stats[i]['total_buy']) |
1317
|
|
|
|
1318
|
|
|
# Add others to graphs |
1319
|
|
|
chart_label.append(_(u'others')) |
1320
|
|
|
chart_data.append(int(summary_totals['total_buy']) - top5sales) |
1321
|
|
|
|
1322
|
|
|
# transform unicode text to strings |
1323
|
|
|
response.context_data['chart_label'] = map(str, chart_label) |
1324
|
|
|
# Example of datas ["Africa", "Asia", "Europe", "Latin America", "North America"] |
1325
|
|
|
response.context_data['chart_data'] = chart_data |
1326
|
|
|
# Example of datas [2478,5267,734,784,433] |
1327
|
|
|
# response.context_data['chart_color'] = ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850"] |
1328
|
|
|
|
1329
|
|
|
# timeseries stats |
1330
|
|
|
callvolume_data = [] |
1331
|
|
|
successcallvolume_data = [] |
1332
|
|
|
timeserie_data = [] |
1333
|
|
|
revenue_data = [] |
1334
|
|
|
margin_data = [] |
1335
|
|
|
for j in range(len(summary_over_time)): |
1336
|
|
|
callvolume_data.append(int(summary_over_time[j]['total_calls'])) |
1337
|
|
|
successcallvolume_data.append(int(summary_over_time[j]['success_calls'])) |
1338
|
|
|
revenue_data.append(int(summary_over_time[j]['total_buy'])) |
1339
|
|
|
margin_data.append(int(summary_over_time[j]['margin'])) |
1340
|
|
|
if period == 'day': |
1341
|
|
|
timeserie_data.append(summary_over_time[j]['period'].strftime('%Y-%m-%d')) |
1342
|
|
|
elif period == 'week': |
1343
|
|
|
timeserie_data.append(summary_over_time[j]['period'].strftime('%W')) |
1344
|
|
|
else: |
1345
|
|
|
timeserie_data.append(summary_over_time[j]['period'].strftime('%Y-%b')) |
1346
|
|
|
|
1347
|
|
|
response.context_data['callvolume_data'] = callvolume_data |
1348
|
|
|
response.context_data['successcallvolume_data'] = successcallvolume_data |
1349
|
|
|
response.context_data['revenue_data'] = revenue_data |
1350
|
|
|
response.context_data['margin_data'] = margin_data |
1351
|
|
|
response.context_data['timeserie_data'] = timeserie_data |
1352
|
|
|
response.context_data['summary_over_time'] = period |
1353
|
|
|
|
1354
|
|
|
return response |
1355
|
|
|
|
1356
|
|
|
|
1357
|
|
|
#---------------------------------------- |
1358
|
|
|
# register |
1359
|
|
|
#---------------------------------------- |
1360
|
|
|
admin.site.register(Company, CompanyAdmin) |
1361
|
|
|
admin.site.register(Person, PersonAdmin) |
1362
|
|
|
admin.site.register(Group, GroupAdmin) |
1363
|
|
|
admin.site.register(CompanyBalanceHistory, CompanyBalanceHistoryAdmin) |
1364
|
|
|
admin.site.register(CalleridPrefix, CalleridPrefixAdmin) |
1365
|
|
|
admin.site.register(CalleridPrefixList, CalleridPrefixListAdmin) |
1366
|
|
|
admin.site.register(ProviderTariff, ProviderTariffAdmin) |
1367
|
|
|
admin.site.register(ProviderRates, ProviderRatesAdmin) |
1368
|
|
|
admin.site.register(LCRGroup, LCRGroupAdmin) |
1369
|
|
|
#admin.site.register(LCRProviders, LCRProvidersAdmin) |
1370
|
|
|
admin.site.register(RateCard, RateCardAdmin) |
1371
|
|
|
admin.site.register(CustomerRates, CustomerRatesAdmin) |
1372
|
|
|
admin.site.register(CustomerRateCards, CustomerRateCardsAdmin) |
1373
|
|
|
#admin.site.register(AclLists, AclListsAdmin) |
1374
|
|
|
#admin.site.register(AclNodes, AclNodesAdmin) |
1375
|
|
|
#admin.site.register(VoipSwitch, VoipSwitchAdmin) |
1376
|
|
|
admin.site.register(SipProfile, SipProfileAdmin) |
1377
|
|
|
admin.site.register(SofiaGateway, SofiaGatewayAdmin) |
1378
|
|
|
#admin.site.register(HangupCause, HangupCauseAdmin) |
1379
|
|
|
admin.site.register(CarrierNormalizationRules, CarrierNormalizationRulesAdmin) |
1380
|
|
|
admin.site.register(CustomerNormalizationRules, |
1381
|
|
|
CustomerNormalizationRulesAdmin) |
1382
|
|
|
admin.site.register(CarrierCIDNormalizationRules, |
1383
|
|
|
CarrierCIDNormalizationRulesAdmin) |
1384
|
|
|
admin.site.register(CustomerCIDNormalizationRules, |
1385
|
|
|
CustomerCIDNormalizationRulesAdmin) |
1386
|
|
|
admin.site.register(DestinationNumberRules, DestinationNumberRulesAdmin) |
1387
|
|
|
#admin.site.register(DimCustomerHangupcause, DimCustomerHangupcauseAdmin) |
1388
|
|
|
#admin.site.register(DimCustomerSipHangupcause, DimCustomerSipHangupcauseAdmin) |
1389
|
|
|
#admin.site.register(DimProviderHangupcause, DimProviderHangupcauseAdmin) |
1390
|
|
|
#admin.site.register(DimProviderSipHangupcause, DimProviderSipHangupcauseAdmin) |
1391
|
|
|
admin.site.register(DimCustomerDestination, DimCustomerDestinationAdmin) |
1392
|
|
|
admin.site.register(DimProviderDestination, DimProviderDestinationAdmin) |
1393
|
|
|
admin.site.register(SaleSummary, SaleSummaryAdmin) |
1394
|
|
|
admin.site.register(CostSummary, CostSummaryAdmin) |
1395
|
|
|
|