Commit 0f034e07 authored by Valentin Samir's avatar Valentin Samir

[voip/models.py] On génère la configuration d'asterisk et on la recharge à la volée

parent 1bce5d17
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
sys.path.append('/usr/scripts/sip/')
from django.db import models
from django.contrib.auth.models import User
import hashlib
import os
import asterisk_reload_conf
from django.utils.importlib import import_module
conn_pool = import_module('conn_pool', 'intranet')
# Description de la configuration générée pour asterisk
asterisk_conf = {
'modules':{
'sip':{
'reload_fields':["caller_id","password"],
'gen_conf':lambda _:Profile.asterisk_config, # pour pouvoir appeler Profile paresseusement
'conf_file':'sip_accounts',
},
'voicemail':{
'reload_fields':["voicemail_password"],
'gen_conf':lambda _:Profile.asterisk_voicemail_config,
'conf_file':'sip_accounts_voicemail',
},
'dialplan':{
'reload_fields':["num"],
'gen_conf':lambda _:Profile.asterisk_alias_config,
'conf_file':'sip_accounts_alias',
},
},
'conf_path':'/usr/scripts/var/sip/'
}
class Profile(models.Model):
""" Profil d'utilisateur Voip """
......@@ -39,6 +71,63 @@ class Profile(models.Model):
num = models.CharField(u"Numéro de téléphone", max_length=10, null=False, blank=False)
voicemail_password = models.CharField(u"Mot de pass Boîte Vocale", max_length=6, null=False, blank=False)
def what_to_reload(self):
""" Defini les modules asterisk dont il faut régénérer la configuration """
def has_changed(self, field):
if not self.pk:
return True
try:
old_value = self.__class__._default_manager.\
filter(pk=self.pk).values(field).get()[field]
except self.DoesNotExist:
return True
return not getattr(self, field) == old_value
modules={}
for module in asterisk_conf['modules'].keys():
modules[module]=reduce( # List.fold_left
lambda x,y: x or has_changed(self,y),
asterisk_conf['modules'][module]['reload_fields'],
False
)
return modules
def reload_what_it_is_to_reload(self,modules):
""" Écrit la configuration des modules asterisk à recharger, puis les recharge """
def writeconf(gen_conf_module,path):
conf=""
for profile in Profile.objects.order_by('user__username').all():
conf+=gen_conf_module(profile).encode('utf-8') + '\n'
file=open(path + '.new','w')
file.write(conf)
file.close()
os.rename(path + '.new', path)
for module in asterisk_conf['modules'].keys():
if modules[module]:
writeconf(
asterisk_conf['modules'][module]['gen_conf'](''),
asterisk_conf['conf_path'] + asterisk_conf['modules'][module][ 'conf_file']
)
if reduce(lambda x,y: x or y, modules.values(), False):
asterisk_reload_conf.reload_config('all')
else:
for module in asterisk_conf['modules'].keys():
if modules[module]:
asterisk_reload_conf.reload_config(module)
def delete(self):
ret=super(Profile,self).delete()
self.reload_what_it_is_to_reload(self.what_to_reload())
return ret
def save(self):
to_reload=self.what_to_reload()
ret=super(Profile,self).save()
self.reload_what_it_is_to_reload(to_reload)
return ret
def set_user(self, new_user):
""" Défini l'utilisateur propriétaire du profil. Cette fonction calcule
également le numéro de téléphone correspondant, à partir de la base LDAP.
......@@ -96,6 +185,7 @@ mailbox=%(num)s@666
'uid': self.user.username,
}
def get_category(self):
""" Renvoie la catégorie du profil, dans l'annuaire """
return "Cr@ns"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment