...
 
Commits (8)
......@@ -6,6 +6,9 @@ xivo-dao (2019.05.00) xivo-callisto; urgency=medium
[ Vojtech Sodoma ]
* 2428 Migrate outcalls to routes
[ Jean-Pierre Thomasset ]
* 2397 - Add helper to get extension list and mds of a given user and group
-- Tomas Taraba <ttaraba@avencall.com> Mon, 15 Apr 2019 17:26:00 +0000
xivo-dao (2019.04.00) xivo-callisto; urgency=medium
......
git+https://gitlab.com/xivo.solutions/xivo-lib-python.git
git+https://gitlab.com/xivo.solutions/xivo-provd-client.git
psycopg2==2.7.5
psycopg2-binary==2.7.5
pyyaml==3.12
sqlalchemy==1.0.15
......@@ -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,65 @@ 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_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 [row.todict() for row in rows]
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 [_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_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'])))
.order_by(text('exten'))
.all())
return [_extension_row_to_dict(row) for row in rows]
@daosession
......@@ -353,7 +401,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)
......
......@@ -18,7 +18,7 @@
from xivo_dao.alchemy.groupfeatures import GroupFeatures
from xivo_dao.alchemy.queuemember import QueueMember
from xivo_dao.helpers.db_manager import daosession
from xivo_dao.alchemy.mediaserver import MediaServer
@daosession
def get(session, group_id):
......@@ -45,3 +45,15 @@ def is_user_member_of_group(session, user_id, group_id):
@daosession
def all(session):
return session.query(GroupFeatures).all()
@daosession
def get_mds_by_group_id(session, group_id):
mds = (session.query(MediaServer.name)
.join(GroupFeatures, MediaServer.id == GroupFeatures.mediaserverid)
.filter(GroupFeatures.id == int(group_id))
.scalar())
if not mds:
raise LookupError('Could not find a group with id %s', group_id)
else:
return mds
......@@ -513,7 +513,7 @@ class TestSimpleSearch(TestSearch):
context=user_line.extension.context,
provisioning_code=user_line.linefeatures.provisioning_code,
protocol=user_line.linefeatures.endpoint,
configregistrar=None,)])
configregistrar="default",)])
self.assert_search_returns_result(expected, view='summary')
......
......@@ -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')
......
......@@ -279,6 +279,7 @@ class DAOTestCase(unittest.TestCase):
kwargs.setdefault('protocolid', kwargs.get('endpoint_id', self._generate_int()))
kwargs.setdefault('provisioningid', int(''.join(random.choice('123456789') for _ in range(6))))
kwargs.setdefault('id', self._generate_int())
kwargs.setdefault('configregistrar', 'default')
line = LineFeatures(**kwargs)
self.add_me(line)
......
......@@ -62,3 +62,10 @@ class TestGroupDAO(DAOTestCase):
queue_member.category = 'group'
self.add_me(queue_member)
def test_get_mds(self):
mediaserver = self.add_mediaserver(name="mds1")
group = self.add_groupfeatures(name='test_name', number='1234', context='my_ctx', mediaserverid=mediaserver.id)
mds = group_dao.get_mds_by_group_id(group.id)
self.assertEqual(mds, 'mds1')
......@@ -76,3 +76,14 @@ class TestUserFeaturesDAO(DAOTestCase):
assert_that(res.UserFeatures.agentid, equal_to(agent.id))
assert_that(res.agent_number, equal_to(unicode(agent.number)))
assert_that(res.agent_group_id, equal_to(agent.numgroup))
def test_get_mds_by_user_id(self):
user = self.add_user()
line = self.add_line(configregistrar='mds1')
self.add_user_line(user_id=user.id, line_id=line.id)
res = user_dao.get_mds_by_user_id(user.id)
assert_that(res, equal_to('mds1'))
......@@ -83,3 +83,17 @@ def get_user_with_agent_number(session, user_id):
raise errors.not_found('User', id=user_id)
return result
@daosession
def get_mds_by_user_id(session, user_id):
mds = (session.query(LineFeatures.configregistrar)
.join(UserLine, and_(UserLine.line_id == LineFeatures.id,
UserLine.user_id == int(user_id),
UserLine.main_user == True,
UserLine.main_line == True))
.scalar())
if not mds:
raise LookupError('Could not find a line for user %s', user_id)
else:
return mds