Commit da05bd05 authored by Etienne Allovon's avatar Etienne Allovon

Merge branch '2371-fix-agent-api' into 'master'

2371 fix agent api

See merge request !37
parents 5cab3200 e046ea1c
xivo-dao (2019.04.00) xivo-callisto; urgency=medium
* 2371 Fix agent api by porting the following fixes
* 2375 Add a CtiProfile to user when it's not set and a login is set
* 2352 Add api to create func key destination for agent
-- Jean-Pierre Thomasset <jpthomasset@gmail.com> Fri, 22 Feb 2019 09:44:53 +0000
xivo-dao (2019.03.00) xivo-callisto; urgency=medium
[ Laurent Meiller ]
......
......@@ -39,7 +39,15 @@ class FuncKey(Base):
destination_type = relationship(FuncKeyDestinationType, foreign_keys=destination_type_id)
@classmethod
def new_for_user(cls):
def new_for_type(cls, dest_type):
type_id = FuncKeyType.query_id('speeddial').as_scalar()
destination_type_id = FuncKeyDestinationType.query_id('user').as_scalar()
destination_type_id = FuncKeyDestinationType.query_id(dest_type).as_scalar()
return cls(type_id=type_id, destination_type_id=destination_type_id)
@classmethod
def new_for_user(cls):
return FuncKey().new_for_type('user')
@classmethod
def new_for_agent(cls):
return FuncKey().new_for_type('agent')
......@@ -19,10 +19,12 @@
from sqlalchemy import and_
from xivo_dao.alchemy.callfiltermember import Callfiltermember
from xivo_dao.alchemy.extension import Extension
from xivo_dao.alchemy.func_key import FuncKey
from xivo_dao.alchemy.func_key_mapping import FuncKeyMapping
from xivo_dao.alchemy.func_key_dest_bsfilter import FuncKeyDestBSFilter
from xivo_dao.alchemy.func_key_dest_user import FuncKeyDestUser
from xivo_dao.alchemy.func_key_dest_agent import FuncKeyDestAgent
class DestinationPersistor(object):
......@@ -37,6 +39,16 @@ class DestinationPersistor(object):
self.session.add(destination)
self.session.flush()
def create_agent_destination(self, agent):
typevals = ['agentstaticlogin', 'agentstaticlogoff', 'agentstaticlogtoggle']
extensions = self.session.query(Extension).filter(Extension.typeval.in_(typevals))
for extension in extensions:
func_key = FuncKey.new_for_agent()
destination = FuncKeyDestAgent(func_key=func_key, agent=agent, extension=extension)
self.session.add(func_key)
self.session.add(destination)
self.session.flush()
def delete_user_destination(self, user):
self.delete_user_func_key(user)
self.delete_bsfilter_func_key(user)
......
......@@ -15,7 +15,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
from hamcrest import assert_that, none, contains, is_not, equal_to
from hamcrest import assert_that, none, contains, is_not, equal_to, has_length
from xivo_dao.tests.test_dao import DAOTestCase
from xivo_dao.resources.func_key.tests.test_helpers import FuncKeyHelper
......@@ -53,6 +53,25 @@ class TestCreateUserDestination(TestFuncKeyDao):
.first())
assert_that(row, is_not(none()))
class TestCreateAgentDestination(TestFuncKeyDao):
def prepare_exten(self):
self.add_extenfeatures('_*31.', 'agentstaticlogin')
self.add_extenfeatures('_*32.', 'agentstaticlogoff')
self.add_extenfeatures('_*30.', 'agentstaticlogtoggle')
def test_given_agent_destination_then_func_key_created(self):
self.prepare_exten()
agent_row = self.add_agent()
self.persistor.create_agent_destination(agent_row)
agent_destination_rows = self.find_all_destinations('agent', agent_row.id).all()
self.assert_func_key_rows_created(agent_destination_rows)
def assert_func_key_rows_created(self, destination_rows):
assert_that(destination_rows, is_not(none()))
assert_that(destination_rows, has_length(3))
class TestDeleteUserDestination(TestFuncKeyDao):
......
......@@ -61,6 +61,7 @@ class FuncKeyHelper(object):
'features': (FuncKeyDestFeaturesSchema, 'features_id', 8),
'paging': (FuncKeyDestPagingSchema, 'paging_id', 9),
'custom': (FuncKeyDestCustomSchema, 'exten', 10),
'agent': (FuncKeyDestAgentSchema, 'agent_id', 11),
'bsfilter': (FuncKeyDestBSFilterSchema, 'filtermember_id', 12),
}
......@@ -228,15 +229,17 @@ class FuncKeyHelper(object):
position=position)
self.add_me(mapping_row)
def find_destination(self, destination, destination_id):
def find_all_destinations(self, destination, destination_id):
schema, column_name, _ = self.destinations[destination]
column = getattr(schema, column_name)
row = (self.session.query(schema)
.filter(column == destination_id)
.first())
rows = (self.session.query(schema)
.filter(column == destination_id))
return rows
return row
def find_destination(self, destination, destination_id):
return self.find_all_destinations(destination, destination_id).first()
def assert_destination_deleted(self, destination, destination_id):
row = self.find_destination(destination, destination_id)
......
......@@ -75,6 +75,7 @@ def find_all_by(**criteria):
def create(user):
created_user = persistor().create(user)
UserFixes(Session).fix(created_user.id)
DestinationPersistor(Session).create_user_destination(created_user)
return created_user
......
......@@ -16,6 +16,7 @@
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from xivo_dao.alchemy.cti_profile import CtiProfile
from xivo_dao.alchemy.userfeatures import UserFeatures as User
from xivo_dao.alchemy.user_line import UserLine
from xivo_dao.alchemy.voicemail import Voicemail
......@@ -31,6 +32,7 @@ class UserFixes(object):
def fix(self, user_id):
self.fix_user(user_id)
self.fix_line(user_id)
self.fix_cti_profile(user_id)
self.session.flush()
def fix_user(self, user_id):
......@@ -57,6 +59,20 @@ class UserFixes(object):
if line_id:
LineFixes(self.session).fix(line_id)
def fix_cti_profile(self, user_id):
user = self.session.query(User).filter(User.id == user_id).first()
updated = False
if user.loginclient is not None and len(user.loginclient) > 0:
if user.cti_profile is None:
user.cti_profile = self.session.query(CtiProfile).first()
updated = True
if user.enableclient == 0:
user.enableclient = 1
updated = True
if updated:
self.session.add(user)
def find_line_id(self, user_id):
return (self.session
.query(UserLine.line_id)
......
......@@ -28,7 +28,9 @@ from hamcrest import none
from hamcrest import has_items
from hamcrest import contains
from hamcrest import not_
from hamcrest import not_none
from xivo_dao.alchemy.cti_profile import CtiProfile
from xivo_dao.alchemy.entity import Entity
from xivo_dao.alchemy.userfeatures import UserFeatures as User
from xivo_dao.alchemy.schedule import Schedule
......@@ -789,6 +791,20 @@ class TestCreate(TestUser):
destunc='789',
musiconhold='music_on_hold'))
def test_create_fix_cti_profile(self):
cti_profile = CtiProfile()
cti_profile.name = 'test_profile'
self.add_me(cti_profile)
created_user = user_dao.create(User(firstname="John",
lastname="Smith",
loginclient="jsmith",
passwdclient="0000"))
updated_user = self.session.query(User).filter(User.id == created_user.id).first()
assert_that(updated_user.cti_profile, not_none())
assert_that(updated_user.cti_profile_id, equal_to(cti_profile.id))
assert_that(updated_user.enableclient, equal_to(1))
def test_that_the_user_uuid_is_unique(self):
shared_uuid = str(uuid.uuid4())
self.prepare_user(firstname='Alice', uuid=shared_uuid)
......
......@@ -16,9 +16,11 @@
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from hamcrest import assert_that, equal_to
from hamcrest import assert_that, equal_to, not_none
from xivo_dao.alchemy.cti_profile import CtiProfile
from xivo_dao.alchemy.voicemail import Voicemail
from xivo_dao.alchemy.userfeatures import UserFeatures
from xivo_dao.alchemy.usersip import UserSIP
from xivo_dao.alchemy.sccpline import SCCPLine
from xivo_dao.tests.test_dao import DAOTestCase
......@@ -76,3 +78,16 @@ class TestUserFixes(DAOTestCase):
sccp = self.session.query(SCCPLine).first()
assert_that(sccp.cid_name, equal_to("John Smith"))
assert_that(sccp.cid_num, equal_to("1000"))
def test_given_user_has_login_cti_wo_profile(self):
cti_profile = CtiProfile()
cti_profile.name = 'test_profile'
self.add_me(cti_profile)
user = self.add_user(firstname="John", lastname="Smith", loginclient="jsmith", passwdclient="0000")
self.fixes.fix(user.id)
updated_user = self.session.query(UserFeatures).filter(UserFeatures.id == user.id).first()
assert_that(updated_user.cti_profile, not_none())
assert_that(updated_user.cti_profile_id, equal_to(cti_profile.id))
assert_that(updated_user.enableclient, equal_to(1))
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