Commit 9a750a87 authored by Jean-Pierre Thomasset's avatar Jean-Pierre Thomasset

2397 - Change extension generation to have one query per object kind instead of per user

parent 0084ed06
......@@ -19,7 +19,7 @@ from collections import defaultdict
from sqlalchemy.sql import text
from sqlalchemy.sql.expression import and_, or_, literal, cast, func
from sqlalchemy.types import Integer
from sqlalchemy.types import Integer, String
from sqlalchemy.util import KeyedTuple
from xivo_dao.helpers.db_manager import daosession
......@@ -61,6 +61,8 @@ from xivo_dao.alchemy.func_key_mapping import FuncKeyMapping
from xivo_dao.alchemy.func_key_dest_custom import FuncKeyDestCustom
from xivo_dao.alchemy.trunkfeatures import TrunkFeatures
from xivo_dao.alchemy.mediaserver import MediaServer
from xivo_dao.alchemy.incall import Incall
from xivo_dao.alchemy.dialaction import Dialaction
@daosession
......@@ -312,19 +314,134 @@ def find_extenfeatures_settings(session, features=None):
@daosession
def find_exten_settings(session, context_name):
rows = (session.query(Extension)
.outerjoin(UserLine, and_(UserLine.extension_id == Extension.id,
rows = (_find_user_exten_settings(session, context_name) +
_find_group_exten_settings(session, context_name) +
_find_queue_meetme_exten_settings(session, context_name) +
_find_incall_exten_settings(session, context_name) +
_find_other_exten_settings(session, context_name))
return rows
def _extension_row_to_dict(row):
result = row.Extension.todict()
result["mediaserver"] = row.mediaserver
return result
def _find_user_exten_settings(session, context_name):
rows = (session.query(Extension,
LineFeatures.configregistrar.label('mediaserver'))
.join(UserLine, and_(UserLine.extension_id == Extension.id,
UserLine.main_user == True,
UserLine.main_line == True))
.outerjoin(LineFeatures, LineFeatures.id == UserLine.line_id)
.join(LineFeatures, LineFeatures.id == UserLine.line_id)
.filter(and_(Extension.context == context_name,
Extension.commented == 0,
or_(UserLine.line_id == None,
LineFeatures.commented == 0)))
Extension.type == 'user',
LineFeatures.commented == 0))
.order_by(text('exten'))
.all())
return [_extension_row_to_dict(row) for row in rows]
def _find_group_exten_settings(session, context_name):
rows = (session.query(Extension,
MediaServer.name.label('mediaserver'))
.join(GroupFeatures, cast(GroupFeatures.id, String) == Extension.typeval)
.join(MediaServer, MediaServer.id == GroupFeatures.mediaserverid)
.filter(and_(Extension.context == context_name,
Extension.type == 'group'))
.order_by(text('exten'))
.all())
return [row.todict() for row in rows]
return [_extension_row_to_dict(row) for row in rows]
def _find_queue_meetme_exten_settings(session, context_name):
rows = (session.query(Extension,
literal('default').label('mediaserver'))
.filter(and_(Extension.context == context_name,
Extension.type.in_(['queue', 'meetme'])))
.order_by(text('exten'))
.all())
return [_extension_row_to_dict(row) for row in rows]
def _find_incall_exten_settings(session, context_name):
rows = (_find_incall_user_exten_settings(session, context_name) +
_find_incall_group_exten_settings(session, context_name) +
_find_incall_queues_meetme_exten_settings(session, context_name) +
_find_incall_other_exten_settings(session, context_name))
return rows;
def _find_incall_user_exten_settings(session, context_name):
rows = (session.query(Extension,
LineFeatures.configregistrar.label('mediaserver'))
.join(Dialaction, and_(Dialaction.category == 'incall',
Dialaction.event == 'answer',
Dialaction.action == 'user',
Dialaction.categoryval == Extension.typeval))
.join(UserLine, and_(cast(UserLine.user_id, String) == Dialaction.actionarg1,
UserLine.main_user == True,
UserLine.main_line == True))
.join(LineFeatures, LineFeatures.id == UserLine.line_id)
.filter(and_(Extension.context == context_name,
Extension.commented == 0,
Extension.type == 'incall',
LineFeatures.commented == 0))
.order_by(text('exten'))
.all())
return [_extension_row_to_dict(row) for row in rows]
def _find_incall_group_exten_settings(session, context_name):
rows = (session.query(Extension,
MediaServer.name.label('mediaserver'))
.join(Dialaction, and_(Dialaction.category == 'incall',
Dialaction.event == 'answer',
Dialaction.action == 'group',
Dialaction.categoryval == Extension.typeval))
.join(GroupFeatures, cast(GroupFeatures.id, String) == Dialaction.actionarg1)
.join(MediaServer, MediaServer.id == GroupFeatures.mediaserverid)
.filter(and_(Extension.context == context_name,
Extension.type == 'incall'))
.order_by(text('exten'))
.all())
return [_extension_row_to_dict(row) for row in rows]
def _find_incall_queues_meetme_exten_settings(session, context_name):
rows = (session.query(Extension,
literal('default').label('mediaserver'))
.join(Dialaction, and_(Dialaction.category == 'incall',
Dialaction.event == 'answer',
Dialaction.action.in_(['queue', 'meetme']),
Dialaction.categoryval == Extension.typeval))
.filter(and_(Extension.context == context_name,
Extension.type == 'incall'))
.order_by(text('exten'))
.all())
return [_extension_row_to_dict(row) for row in rows]
def _find_incall_other_exten_settings(session, context_name):
rows = (session.query(Extension,
literal(None).label('mediaserver'))
.join(Dialaction, and_(Dialaction.category == 'incall',
Dialaction.event == 'answer',
Dialaction.action.notin_(['user', 'group', 'queue', 'meetme']),
Dialaction.categoryval == Extension.typeval))
.filter(and_(Extension.context == context_name,
Extension.type == 'incall'))
.order_by(text('exten'))
.all())
return [_extension_row_to_dict(row) for row in rows]
def _find_other_exten_settings(session, context_name):
rows = (session.query(Extension,
literal(None).label('mediaserver'))
.filter(and_(Extension.context == context_name,
Extension.type.notin_(['meetme', 'queue', 'user', 'group', 'incall'])))
.order_by(text('exten'))
.all())
return [_extension_row_to_dict(row) for row in rows]
@daosession
......@@ -353,7 +470,6 @@ def is_extension_on_current_mds(session, exten_number, exten_context, current_md
else:
return False
@daosession
def is_sip_trunk_on_current_mds(session, trunk_name, current_mds_name):
match = (session.query(TrunkFeatures)
......
......@@ -570,6 +570,7 @@ class TestAsteriskConfDAO(DAOTestCase, PickupHelperMixin):
'context': u'default',
'typeval': u'',
'type': 'user',
'mediaserver': 'default',
'id': extension_row.id}
]
......@@ -590,22 +591,36 @@ class TestAsteriskConfDAO(DAOTestCase, PickupHelperMixin):
assert_that(result, contains())
def test_find_exten_settings_multiple_extensions(self):
user1 = self.add_user()
line1 = self.add_line()
exten1 = self.add_extension(exten='12', context='default')
user2 = self.add_user()
line2 = self.add_line()
exten2 = self.add_extension(exten='23', context='default')
self.add_extension(exten='41', context='toto')
self.add_user_line(user_id=user1.id,
extension_id=exten1.id,
line_id=line1.id)
self.add_user_line(user_id=user2.id,
extension_id=exten2.id,
line_id=line2.id)
expected_result = [
{'exten': u'12',
'commented': 0,
'context': u'default',
'typeval': u'',
'type': 'user',
'mediaserver': 'default',
'id': exten1.id},
{'exten': u'23',
'commented': 0,
'context': u'default',
'typeval': u'',
'type': 'user',
'mediaserver': 'default',
'id': exten2.id}
]
......@@ -613,6 +628,67 @@ class TestAsteriskConfDAO(DAOTestCase, PickupHelperMixin):
assert_that(extensions, contains_inanyorder(*expected_result))
def test_find_exten_group(self):
mds = self.add_mediaserver(name='mds1', display_name='MDS1', voip_ip='10.10.0.1')
group = self.add_groupfeatures(mediaserverid=mds.id)
extension_row = self.add_extension(exten='12', context='default', type='group', typeval=str(group.id))
expected_result = [
{'exten': u'12',
'commented': 0,
'context': u'default',
'typeval': str(group.id),
'type': 'group',
'mediaserver': 'mds1',
'id': extension_row.id}
]
result = asterisk_conf_dao.find_exten_settings('default')
assert_that(result, contains(*expected_result))
def test_find_exten_queue_meetme_on_default_mds(self):
queue = self.add_extension(exten='3000', context='default', type='queue', typeval='2')
meetme = self.add_extension(exten='4000', context='default', type='meetme', typeval='3')
expected_result = [
{'exten': u'3000',
'commented': 0,
'context': u'default',
'typeval': '2',
'type': 'queue',
'mediaserver': 'default',
'id': queue.id},
{'exten': u'4000',
'commented': 0,
'context': u'default',
'typeval': '3',
'type': 'meetme',
'mediaserver': 'default',
'id': meetme.id},
]
result = asterisk_conf_dao.find_exten_settings('default')
assert_that(result, contains(*expected_result))
def test_find_exten_other_no_mds(self):
vm = self.add_extension(exten='4000', context='default', type='voicemenu', typeval='2')
expected_result = [
{'exten': u'4000',
'commented': 0,
'context': u'default',
'typeval': '2',
'type': 'voicemenu',
'mediaserver': None,
'id': vm.id}
]
result = asterisk_conf_dao.find_exten_settings('default')
assert_that(result, contains(*expected_result))
def test_is_user_extension_on_current_mds(self):
self.add_netiface(networktype='voip', address='1.1.1.1')
self.add_mediaserver(name='mds1', display_name='MDS1', voip_ip='10.10.0.1')
......@@ -656,7 +732,7 @@ class TestAsteriskConfDAO(DAOTestCase, PickupHelperMixin):
# if there is local media server for given extension, route is locally
result = asterisk_conf_dao.is_extension_on_current_mds('2000', 'default', 'mds1', 'group')
assert_that(result, equal_to(False))
def test_is_sip_trunk_on_current_mds(self):
self.add_netiface(networktype='voip', address='1.1.1.1')
self.add_usersip(id='5', category='trunk', name='trunk1')
......
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