-
Notifications
You must be signed in to change notification settings - Fork 2
/
asterisk_log.py
125 lines (113 loc) · 5.35 KB
/
asterisk_log.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
################################################################################
#
# Module [asterisk_log] MLMConseil
# ----------------------------------
#
# ----------------------------------
# permet de recharger les appels d'asterisk
# par le fichier "Master.csv"
# ----------------------------------------
# langage : Python 2.7
# date creation : 30/10/2015
# date modification : /2015
# version : 0.1
# auteur : Bouslahi Yacine
#
################################################################################
from openerp.osv import fields, osv, orm
import logging
import datetime
import time
import sys, os
_logger = logging.getLogger(__name__)
# chemin absolu vers le fichier Master.csv
PATH_MASTER = 'C:\\Program Files (x86)\\Odoo 8.0-20150719\\server\\openerp\\addons\\asterisk_log\\Master.csv'
class asterisk_log(osv.Model):
_name = 'asterisk.log'
_description = "Informations sur les appels"
_order = 'date' #'date desc' DESC
_log_access = False
_columns = {
#'name': fields.many2one('res.partner', 'Nom'),
'appelant': fields.char('Appelant', size=50), #, required=True
'appele': fields.char('Appelé', size=50),
'date': fields.char('Date', size=50),
'heure' : fields.char('Heure', size=50),
'duree': fields.char('Durée', size=50),
'etat': fields.char('Etat', size=50),
#'total_appel': fields.function(_appel_total, string='total d\'appel', type='integer'),
}
def update_tree(self, cr, uid, ids, context=None):
i = 0
ligne_file = 0
max_id = 0
req = None
try:
date_mnt = time.strftime("%d-%m-%Y")
req1 = "SELECT count(*) FROM asterisk_log" #" SELECT COUNT(*) FROM asterisk_log"
cr.execute(req1)
id_appel = cr.fetchone()[0]
if id_appel == None:
id_appel = 0
max_id = id_appel
try:
file = open(PATH_MASTER,'r')
except IOError:
raise orm.except_orm(("Erreur :"),("fichier Master.csv introuvable %s" % (os.path.abspath(PATH_MASTER))))
# sys.exit(0)
for ligne in file.readlines():
if ligne_file > max_id-2: #changer a max_id-1
i +=1
appelant = str(ligne.split(",")[4].replace('"','')) # recupere au format Merouane <208>
appele = str(ligne.split(",")[2].replace('"',''))
#dans le cas ou c un appel entrant
if appele == '0033185092063':
appelant = str(ligne.split(",")[1].replace('"','')) # recuperer juste le num
date_heure = len(ligne.split(",")[13].replace('"','').split(" "))
if date_heure == 2:
date = str(ligne.split(",")[13].replace('"','').split(" ")[0])
heure = str(ligne.split(",")[13].replace('"','').split(" ")[1])
else:
date = str(ligne.split(",")[13].replace('"',''))
heure = '00:00:00'
duree = str(ligne.split(",")[15].replace('"',''))
etat = str(ligne.split(",")[16].replace('"',''))
id_appel +=1
req = "INSERT INTO asterisk_log(appelant, appele, date, heure, duree, etat) \
VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}' \
)".format(appelant, appele, date, heure, duree, etat)
##dans le cas ou c un appel sortant
else:
date_heure = len(ligne.split(",")[9].replace('"','').split(" "))
if date_heure == 2:
date = str(ligne.split(",")[9].replace('"','').split(" ")[0])
heure = str(ligne.split(",")[9].replace('"','').split(" ")[1])
else:
date = str(ligne.split(",")[9].replace('"',''))
heure = '00:00:00'
duree = str(ligne.split(",")[13].replace('"',''))
etat = str(ligne.split(",")[14].replace('"',''))
id_appel +=1
req = "INSERT INTO asterisk_log(appelant, appele, date, heure, duree, etat) \
VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}' \
)".format(appelant, appele, date, heure, duree, etat)
cr.execute(req)
cr.commit()
ligne_file +=1
file.close
except Exception as err:
raise orm.except_orm(
("Erreur :"),
("requete incorrecte ! \n details : %s \n req = %s" % (err, req)))
else:
if i==0:
raise orm.except_orm(("Mise à jour"),
("Pas de nouvels appels"))
else:
raise orm.except_orm(
("Mise à jour reussie"),
("%s Nouvels appels trouvés" % i))
finally:
_logger.info("Mise a jour des appels reussie: %s appels trouvé" % i)