Skip to content

Commit

Permalink
Feature: manual CDR import on Admin (csv, json, yaml)
Browse files Browse the repository at this point in the history
  • Loading branch information
areski committed Oct 26, 2015
1 parent a796a5a commit 89f4b9e
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 4 deletions.
6 changes: 6 additions & 0 deletions addons/samples/cdrimport/CDRImport-2015-10-26.csv
@@ -0,0 +1,6 @@
switch,cdr_source_type,callid,caller_id_number,caller_id_name,destination_number,dialcode,state,channel,starting_date,duration,billsec,progresssec,answersec,waitsec,hangup_cause_id,hangup_cause,direction,country_code,accountcode,buy_rate,buy_cost,sell_rate,sell_cost,extradata
127.0.0.1,1,96aa82fe-7bd1-11e5-a230-5c514f6a0f72,904151440,CallerIDName,+34798400122,34,,,2015-10-21 12:13:10,55,50,,,,16,,1,,1000,,,,,{}
127.0.0.1,1,c9135e4a-7bd1-11e5-a230-5c514f6a0f72,904234320,CallerIDName,+34798401111,34,,,2015-10-21 12:33:15,15,10,,,,16,,1,,1000,,,,,{}
127.0.0.1,1,cfaf8b56-7bd1-11e5-a230-5c514f6a0f72,901110380,CallerIDName,+34650104877,34,,,2015-10-21 12:53:16,41,34,,,,16,,1,,1000,,,,,{}
127.0.0.1,1,3c64a168-7bd2-11e5-a230-5c514f6a0f72,904234320,CallerIDName,+34798401111,34,,,2015-10-21 12:53:16,16,11,,,,16,,1,,1000,,,,,{}
127.0.0.1,1,41b20dd9-7bd2-11e5-a230-5c514f6a0f72,904231111,CallerIDName,+34650104877,34,,,2015-10-21 12:53:16,8,5,,,,16,,1,,1000,,,,,{}
5 changes: 5 additions & 0 deletions cdr_stats/cdr_stats/settings.py
Expand Up @@ -53,6 +53,8 @@
}
}

DATABASE_ROUTERS = ['import_cdr.router.CDRImportRouter']

# CACHES = {
# 'default': {
# 'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
Expand Down Expand Up @@ -184,6 +186,8 @@
# DAJAXICE_MEDIA_PREFIX = "dajax" # http://domain.com/dajax/
# DAJAXICE_CACHE_CONTROL = 10 * 24 * 60 * 60

ALLOWED_HOSTS = ['127.0.0.1']

INSTALLED_APPS = (
# admin tool apps
# 'admintools_bootstrap', # https://bitbucket.org/salvator/django-admintools-bootstrap
Expand Down Expand Up @@ -233,6 +237,7 @@
'bootstrap3_datetime',
'crispy_forms',
'call_analytic',
'import_export',
)

CRISPY_TEMPLATE_PACK = 'bootstrap3'
Expand Down
1 change: 1 addition & 0 deletions cdr_stats/cdr_stats/settings_local.py
Expand Up @@ -11,6 +11,7 @@
# The Initial Developer of the Original Code is
# Arezqui Belaid <info@star2billing.com>
#

import os

DEBUG = True
Expand Down
18 changes: 16 additions & 2 deletions cdr_stats/import_cdr/admin.py
Expand Up @@ -14,6 +14,18 @@

from django.contrib import admin
from import_cdr.models import CDRImport
from import_export import resources
from import_export.admin import ImportExportModelAdmin


class CDRImportResource(resources.ModelResource):

class Meta:
model = CDRImport
using = 'import_cdr'
# fields = ('switch', 'cdr_source_type', 'callid', 'caller_id_number', 'caller_id_name', 'destination_number', 'dialcode', 'state', 'channel', 'starting_date', 'duration', 'billsec', 'progresssec', 'answersec', 'waitsec', 'hangup_cause_id', 'hangup_cause', 'direction', 'country_code', 'accountcode', 'buy_rate', 'buy_cost', 'sell_rate', 'sell_cost',)
import_id_fields = ('callid',)
exclude = ('id', 'imported', )


class MultiDBModelAdmin(admin.ModelAdmin):
Expand Down Expand Up @@ -44,10 +56,12 @@ def formfield_for_manytomany(self, db_field, request=None, **kwargs):


# CDRImport
class CDRImportAdmin(MultiDBModelAdmin):
# class CDRImportAdmin(MultiDBModelAdmin, ImportExportModelAdmin):
class CDRImportAdmin(MultiDBModelAdmin, ImportExportModelAdmin):
list_display = ('id', 'cdr_source_type', 'callid', 'switch', 'destination_number', 'dialcode', 'caller_id_number',
'duration', 'hangup_cause', 'direction', 'starting_date', 'hangup_cause_id', 'hangup_cause',
'accountcode', 'imported', )
search_fields = ('destination_number', 'caller_id_number',)
resource_class = CDRImportResource

admin.site.register(CDRImport, MultiDBModelAdmin)
admin.site.register(CDRImport, CDRImportAdmin)
6 changes: 4 additions & 2 deletions cdr_stats/import_cdr/models.py
Expand Up @@ -24,6 +24,8 @@ class CDRImport(models.Model):
Manually selecting a database for a QuerySet:
CDRImport.objects.using('import_cdr').all()
TODO: add documentation to explain all the fields
"""
id = models.AutoField(primary_key=True)
switch = models.CharField(max_length=80)
Expand Down Expand Up @@ -56,8 +58,8 @@ class CDRImport(models.Model):
extradata = json_field.JSONField(blank=True)

def __unicode__(self):
return '[%s] %s - dur:%d - hangup:%d' % \
(self.id, self.destination_number, self.duration, self.hangup_cause_id)
return '[%s] %s - dur:%d - hangup:%s' % \
(self.id, self.destination_number, self.duration, str(self.hangup_cause_id))

class Meta:
# Remove `managed = False` lines if you wish to allow Django to create, modify,
Expand Down
59 changes: 59 additions & 0 deletions cdr_stats/import_cdr/router.py
@@ -0,0 +1,59 @@
#
# CDR-Stats License
# http://www.cdr-stats.org
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (C) 2011-2015 Star2Billing S.L.
#
# The Initial Developer of the Original Code is
# Arezqui Belaid <info@star2billing.com>
#


"""
We implemented a router cause django-import-export doesn't have a mechanism
to specify the database with `using`
https://github.com/django-import-export/django-import-export
"""


class CDRImportRouter(object):
"""
A router to control all database operations on models.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read auth models go to import_cdr.
"""
if model._meta.app_label == 'import_cdr':
return 'import_cdr'
return None

def db_for_write(self, model, **hints):
"""
Attempts to write auth models go to import_cdr.
"""
if model._meta.app_label == 'import_cdr':
return 'import_cdr'
return None

def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations if a model in the import_cdr app is involved.
"""
if obj1._meta.app_label == 'import_cdr' or \
obj2._meta.app_label == 'import_cdr':
return True
return None

def allow_migrate(self, db, app_label, model=None, **hints):
"""
Make sure the auth app only appears in the 'import_cdr'
database.
"""
if app_label == 'import_cdr':
return db == 'import_cdr'
return None
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/source/_static/images/admin/import-cdr.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions docs/source/user-guide-doc/admin_panel.rst
Expand Up @@ -13,6 +13,8 @@ The Admin UI is located at http://localhost:8000/admin/
:depth: 1


.. _admin-panel-dashboard:

Dashboard
---------

Expand All @@ -21,6 +23,47 @@ Dashboard page for the admin interface after successful login with superuser cre
.. image:: ../_static/images/admin/admin_dashboard.png


.. _admin-panel-cdr-import:

CDR Manual Import / Export
--------------------------

CDR-Stats have a tool to import manually CDRs from CSV, Json, Excel & Yaml.
The first lines of the import file should contain the following header names "switch,cdr_source_type,callid,caller_id_number,caller_id_name,destination_number,dialcode,state,channel,starting_date,duration,billsec,progresssec,answersec,waitsec,hangup_cause_id,hangup_cause,direction,country_code,accountcode,buy_rate,buy_cost,sell_rate,sell_cost,extradata".

Example of a CDR csv file to import:
::

switch,cdr_source_type,callid,caller_id_number,caller_id_name,destination_number,dialcode,state,channel,starting_date,duration,billsec,progresssec,answersec,waitsec,hangup_cause_id,hangup_cause,direction,country_code,accountcode,buy_rate,buy_cost,sell_rate,sell_cost,extradata
127.0.0.1,1,96aa82fe-7bd1-11e5-a230-5c514f6a0f72,904151440,CallerIDName,+34798400122,34,,,2015-10-21 12:13:10,55,50,,,,16,,1,,1000,,,,,{}
127.0.0.1,1,c9135e4a-7bd1-11e5-a230-5c514f6a0f72,904234320,CallerIDName,+34798401111,34,,,2015-10-21 12:33:15,15,10,,,,16,,1,,1000,,,,,{}
127.0.0.1,1,cfaf8b56-7bd1-11e5-a230-5c514f6a0f72,901110380,CallerIDName,+34650104877,34,,,2015-10-21 12:53:16,41,34,,,,16,,1,,1000,,,,,{}
127.0.0.1,1,3c64a168-7bd2-11e5-a230-5c514f6a0f72,904234320,CallerIDName,+34798401111,34,,,2015-10-21 12:53:16,16,11,,,,16,,1,,1000,,,,,{}
127.0.0.1,1,41b20dd9-7bd2-11e5-a230-5c514f6a0f72,904231111,CallerIDName,+34650104877,34,,,2015-10-21 12:53:16,8,5,,,,16,,1,,1000,,,,,{}


Note:
::

- cdr_source_type is an integer to define from where the CDR comes from (UNKNOWN = 0, CSV = 1, API = 2, FREESWITCH = 3, ASTERISK = 4, YATE = 5, KAMAILIO = 6, OPENSIPS = 7, SIPWISE = 8, VERAZ = 9)

- extradata is a Json field, if empty you have to set it as `{}`


From CDR Import admin page, you will also be able to export your CDRs to CSV, Json, HTML, Ods, Excel & Yaml.


**URL**:

* http://localhost:8000/admin/import_cdr/cdrimport/import/
* http://localhost:8000/admin/import_cdr/cdrimport/export/

.. image:: ../_static/images/admin/import-cdr.png

.. image:: ../_static/images/admin/import-cdr-confirmation.png

.. _admin-panel-alarm:

Alarm
-----

Expand All @@ -46,6 +89,8 @@ To Add/Update alarm
.. image:: ../_static/images/admin/add_alarm.png


.. _admin-panel-alarm-report:

Alarm-report
------------

Expand All @@ -67,6 +112,8 @@ To Add/Update alarmreport
.. image:: ../_static/images/admin/alarm_report.png


.. _admin-panel-blacklist:

Blacklist
---------

Expand All @@ -85,6 +132,8 @@ to update.
.. image:: ../_static/images/admin/add_prefix_into_blacklist.png


.. _admin-panel-whitelist:

Whitelist
---------

Expand All @@ -104,6 +153,8 @@ to update.
.. image:: ../_static/images/admin/add_prefix_into_whitelist.png


.. _admin-panel-alert-remove-prefix:

Alert-remove-prefix
-------------------

Expand All @@ -130,6 +181,8 @@ To Add/Update alert-remove prefix
.. image:: ../_static/images/admin/add_alert_remove_prefix.png


.. _admin-panel-switch:

Switch
------

Expand All @@ -140,6 +193,8 @@ Switch
.. image:: ../_static/images/admin/switch_list.png


.. _admin-panel-hangupcause:

HangupCause
-----------

Expand All @@ -151,6 +206,8 @@ HangupCause



.. _admin-panel-cdr-view:

CDR View
--------

Expand Down
1 change: 1 addition & 0 deletions requirements/django.txt
Expand Up @@ -14,6 +14,7 @@ django-dajax-ng==0.9.4
django-dajaxice-ng==0.7.0.1
django-country-dialcode==0.6.3
django-admin-tools-stats==0.6.1
django-import-export==0.2.8
# django-filter==0.9.2

#0.8 is not compatible with django 1.7
Expand Down

0 comments on commit 89f4b9e

Please sign in to comment.