Commit 425639d5 authored by Gregory Eric Sanderson's avatar Gregory Eric Sanderson Committed by Cedric Abunar

delete user_line_extension service

parent ba0fc655
...@@ -26,7 +26,6 @@ setup( ...@@ -26,7 +26,6 @@ setup(
'xivo_dao.data_handler.line_extension', 'xivo_dao.data_handler.line_extension',
'xivo_dao.data_handler.user', 'xivo_dao.data_handler.user',
'xivo_dao.data_handler.user_line', 'xivo_dao.data_handler.user_line',
'xivo_dao.data_handler.user_line_extension',
'xivo_dao.data_handler.user_voicemail', 'xivo_dao.data_handler.user_voicemail',
'xivo_dao.data_handler.voicemail', 'xivo_dao.data_handler.voicemail',
'xivo_dao.data_handler.cti_profile', 'xivo_dao.data_handler.cti_profile',
......
# -*- coding: utf-8 -*-
# Copyright (C) 2013-2014 Avencall
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# 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 xivo_dao.alchemy.user_line import UserLine as ULESchema
from xivo_dao.helpers.db_manager import daosession
from xivo_dao.data_handler.exception import ElementNotExistsError, \
ElementCreationError, ElementDeletionError, ElementEditionError
from sqlalchemy.exc import SQLAlchemyError
from model import UserLineExtension
from xivo_dao.data_handler.user import dao as user_dao
@daosession
def get(session, ule_id):
res = _new_query(session).filter(ULESchema.id == ule_id).first()
if not res:
raise ElementNotExistsError('UserLineExtension', id=ule_id)
return UserLineExtension.from_data_source(res)
@daosession
def find_all(session):
res = session.query(ULESchema).all()
if not res:
return []
tmp = []
for ule in res:
tmp.append(UserLineExtension.from_data_source(ule))
return tmp
@daosession
def find_all_by_user_id(session, user_id):
ules = session.query(ULESchema).filter(ULESchema.user_id == user_id).all()
return [UserLineExtension.from_data_source(ule) for ule in ules]
@daosession
def find_all_by_extension_id(session, extension_id):
ules = session.query(ULESchema).filter(ULESchema.extension_id == extension_id).all()
return [UserLineExtension.from_data_source(ule) for ule in ules]
@daosession
def find_all_by_line_id(session, line_id):
ules = session.query(ULESchema).filter(ULESchema.line_id == line_id).all()
return [UserLineExtension.from_data_source(ule) for ule in ules]
@daosession
def find_main_user(session, ule):
row = (session.query(ULESchema.user_id)
.filter(ULESchema.main_user == True)
.filter(ULESchema.line_id == ule.line_id)
.first())
if not row:
return user_dao.get(ule.user_id)
return user_dao.get(row[0])
@daosession
def create(session, user_line_extension):
user_line_extension_row = user_line_extension.to_data_source(ULESchema)
session.begin()
session.add(user_line_extension_row)
try:
session.commit()
except SQLAlchemyError as e:
session.rollback()
raise ElementCreationError('UserLineExtension', e)
user_line_extension.id = user_line_extension_row.id
return user_line_extension
@daosession
def edit(session, user_line_extension):
session.begin()
nb_row_affected = (session.query(ULESchema)
.filter(ULESchema.id == user_line_extension.id)
.update(user_line_extension.to_data_dict()))
try:
session.commit()
except SQLAlchemyError as e:
session.rollback()
raise ElementEditionError('UserLineExtension', e)
if nb_row_affected == 0:
raise ElementEditionError('UserLineExtension', 'user_line_extension_id %s not exsit' % user_line_extension.id)
return nb_row_affected
@daosession
def delete(session, user_line_extension):
session.begin()
try:
nb_row_affected = session.query(ULESchema).filter(ULESchema.id == user_line_extension.id).delete()
session.commit()
except SQLAlchemyError, e:
session.rollback()
raise ElementDeletionError('UserLineExtension', e)
if nb_row_affected == 0:
raise ElementDeletionError('UserLineExtension', 'user_line_extension_id %s not exsit' % user_line_extension.id)
return nb_row_affected
@daosession
def already_linked(session, user_id, line_id):
count = (session.query(ULESchema)
.filter(ULESchema.user_id == user_id)
.filter(ULESchema.line_id == line_id)
.count())
return count > 0
@daosession
def main_user_is_allowed_to_delete(session, main_line_id):
count = (session.query(ULESchema)
.filter(ULESchema.line_id == main_line_id)
.count())
return count == 1
def _new_query(session):
return session.query(ULESchema)
# -*- coding: utf-8 -*-
# Copyright (C) 2013-2014 Avencall
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# 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 xivo_dao.helpers.abstract_model import AbstractModels
class UserLineExtension(AbstractModels):
MANDATORY = [
'user_id',
'line_id',
'extension_id',
]
# mapping = {db_field: model_field}
_MAPPING = {
'id': 'id',
'user_id': 'user_id',
'line_id': 'line_id',
'extension_id': 'extension_id',
'main_user': 'main_user',
'main_line': 'main_line'
}
_RELATION = {}
def __init__(self, *args, **kwargs):
AbstractModels.__init__(self, *args, **kwargs)
# -*- coding: utf-8 -*-
# Copyright (C) 2013-2014 Avencall
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# 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 xivo_bus.resources.user_line_extension import event
from xivo_dao import helpers
from xivo_dao.helpers import bus_manager
def created(user_line_extension):
data = _build_edit_user_phone(user_line_extension)
helpers.sysconfd_connector.exec_request_handlers(data)
bus_manager.send_bus_command(event.CreateUserLineExtensionEvent(user_line_extension.id,
user_line_extension.user_id,
user_line_extension.line_id,
user_line_extension.extension_id,
user_line_extension.main_user,
user_line_extension.main_line))
def edited(user_line_extension):
data = _build_edit_user_phone(user_line_extension)
helpers.sysconfd_connector.exec_request_handlers(data)
bus_manager.send_bus_command(event.EditUserLineExtensionEvent(user_line_extension.id,
user_line_extension.user_id,
user_line_extension.line_id,
user_line_extension.extension_id,
user_line_extension.main_user,
user_line_extension.main_line))
def deleted(user_line_extension):
data = _build_edit_user_phone(user_line_extension)
helpers.sysconfd_connector.exec_request_handlers(data)
bus_manager.send_bus_command(event.DeleteUserLineExtensionEvent(user_line_extension.id,
user_line_extension.user_id,
user_line_extension.line_id,
user_line_extension.extension_id,
user_line_extension.main_user,
user_line_extension.main_line))
def _build_edit_user_phone(user_line_extension):
return _new_sysconfd_data([
'xivo[user,edit,%s]' % user_line_extension.user_id,
'xivo[phone,edit,%s]' % user_line_extension.line_id
])
def _new_sysconfd_data(ctibus_commands):
return {
'ctibus': ctibus_commands,
'dird': [],
'ipbx': ['dialplan reload', 'sip reload'],
'agentbus': []
}
# -*- coding: utf-8 -*-
# Copyright (C) 2013-2014 Avencall
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
import dao
import notifier
import validator
from xivo_dao.data_handler.exception import ElementNotExistsError
from xivo_dao.data_handler.line import services as line_services
from xivo_dao.data_handler.line import dao as line_dao
from xivo_dao.data_handler.user import services as user_services
from xivo_dao.data_handler.extension import services as extension_services
from xivo_dao.data_handler.extension import dao as extension_dao
from xivo import caller_id
def get(ule_id):
return dao.get(ule_id)
def find_all():
return dao.find_all()
def find_all_by_user_id(user_id):
return dao.find_all_by_user_id(user_id)
def find_all_by_extension_id(extension_id):
return dao.find_all_by_extension_id(extension_id)
def find_all_by_line_id(line_id):
return dao.find_all_by_line_id(line_id)
def create(ule):
user, line, extension = validator.validate_create(ule)
main_user = dao.find_main_user(ule)
_adjust_optional_parameters(ule, main_user)
ule = dao.create(ule)
_make_secondary_associations(main_user, line, extension)
notifier.created(ule)
return ule
def edit(ule):
validator.validate(ule)
dao.edit(ule)
notifier.edited(ule)
def delete(ule):
validator.validate(ule)
validator.is_allowed_to_delete(ule)
dao.delete(ule)
notifier.deleted(ule)
def delete_everything(ule):
user, line, extension = validator.validate(ule)
dao.delete(ule)
_remove_user(user)
_remove_line(line)
_remove_exten(extension)
notifier.deleted(ule)
def _adjust_optional_parameters(ule, main_user):
ule.main_line = True
ule.main_user = (ule.user_id == main_user.id)
def _make_secondary_associations(main_user, line, extension):
_associate_extension(main_user, extension)
_associate_line(line, extension, main_user)
def _associate_extension(main_user, extension):
extension_dao.associate_to_user(main_user, extension)
def _associate_line(line, extension, main_user):
line.number = extension.exten
line.context = extension.context
callerid, _, _ = caller_id.build_caller_id('', main_user.fullname, extension.exten)
line.callerid = callerid
line_dao.edit(line)
line_dao.update_xivo_userid(line, main_user)
def _remove_user(user):
try:
user_services.delete(user)
except ElementNotExistsError:
return
def _remove_line(line):
try:
line_services.delete(line)
except ElementNotExistsError:
return
def _remove_exten(extension):
try:
extension_services.delete(extension)
except ElementNotExistsError:
return
# -*- coding: utf-8 -*-
# Copyright (C) 2013-2014 Avencall
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# 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 .. import notifier
from mock import Mock, patch
from unittest import TestCase
ULE_ID = 1
USER_ID = 2
LINE_ID = 3
EXTENSION_ID = 4
MAIN_USER = True
MAIN_LINE = True
SYSCONFD_DATA = {
'ctibus': [
'xivo[user,edit,%s]' % USER_ID,
'xivo[phone,edit,%s]' % LINE_ID
],
'dird': [],
'ipbx': ['dialplan reload', 'sip reload'],
'agentbus': []
}
ULE = Mock(id=ULE_ID,
user_id=USER_ID,
line_id=LINE_ID,
extension_id=EXTENSION_ID,
main_user=MAIN_USER,
main_line=MAIN_LINE)
class TestUserLinkNotifier(TestCase):
def setUp(self):
pass
def tearDown(self):
pass
@patch('xivo_bus.resources.user_line_extension.event.CreateUserLineExtensionEvent')
@patch('xivo_dao.helpers.sysconfd_connector.exec_request_handlers')
@patch('xivo_dao.helpers.bus_manager.send_bus_command')
def test_created(self, send_bus_command, exec_request_handlers, event_init):
new_command = event_init.return_value = Mock()
notifier.created(ULE)
exec_request_handlers.assert_called_once_with(SYSCONFD_DATA)
event_init.assert_called_once_with(ULE_ID,
USER_ID,
LINE_ID,
EXTENSION_ID,
MAIN_USER,
MAIN_LINE)
send_bus_command.assert_called_once_with(new_command)
@patch('xivo_bus.resources.user_line_extension.event.EditUserLineExtensionEvent')
@patch('xivo_dao.helpers.sysconfd_connector.exec_request_handlers')
@patch('xivo_dao.helpers.bus_manager.send_bus_command')
def test_edited(self, send_bus_command, exec_request_handlers, event_init):
new_command = event_init.return_value = Mock()
notifier.edited(ULE)
exec_request_handlers.assert_called_once_with(SYSCONFD_DATA)
event_init.assert_called_once_with(ULE_ID,
USER_ID,
LINE_ID,
EXTENSION_ID,
MAIN_USER,
MAIN_LINE)
send_bus_command.assert_called_once_with(new_command)
@patch('xivo_bus.resources.user_line_extension.event.DeleteUserLineExtensionEvent')
@patch('xivo_dao.helpers.sysconfd_connector.exec_request_handlers')
@patch('xivo_dao.helpers.bus_manager.send_bus_command')
def test_deleted(self, send_bus_command, exec_request_handlers, event_init):
new_command = event_init.return_value = Mock()
notifier.deleted(ULE)
exec_request_handlers.assert_called_once_with(SYSCONFD_DATA)
event_init.assert_called_once_with(ULE_ID,
USER_ID,
LINE_ID,
EXTENSION_ID,
MAIN_USER,
MAIN_LINE)
send_bus_command.assert_called_once_with(new_command)
# -*- coding: utf-8 -*-
# Copyright (C) 2013-2014 Avencall
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# 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 xivo_dao.data_handler.exception import MissingParametersError, \
InvalidParametersError, ElementNotExistsError, NonexistentParametersError
from xivo_dao.data_handler.context import services as context_services
from xivo_dao.data_handler.context.services import ContextRange
from xivo_dao.data_handler.extension import dao as extension_dao
from xivo_dao.data_handler.line import dao as line_dao
from xivo_dao.data_handler.user import dao as user_dao
from xivo_dao.data_handler.user_line_extension import dao as ule_dao
def validate_create(ule):
user, line, extension = validate(ule)
check_if_user_and_line_already_linked(user, line)
check_if_extension_in_context_range(extension)
check_line_links_for_extension(line, extension)
return user, line, extension
def validate(ule):
_check_missing_parameters(ule)
_check_invalid_parameters(ule)
user, line, extension = _get_secondary_associations(ule)
return user, line, extension
def is_allowed_to_delete(ule):
if ule.main_user is True and not ule_dao.main_user_is_allowed_to_delete(ule.line_id):
raise InvalidParametersError(['There are secondary users associated to this link'])
def _check_missing_parameters(ule):
missing = ule.missing_parameters()
if missing:
raise MissingParametersError(missing)
def _check_invalid_parameters(ule_id):
invalid_parameters = []
if not isinstance(ule_id.user_id, int):
invalid_parameters.append('user_id must be integer')
if ule_id.user_id == 0:
invalid_parameters.append('user_id equal to 0')
if not isinstance(ule_id.line_id, int):
invalid_parameters.append('line_id must be integer')
if ule_id.line_id == 0:
invalid_parameters.append('line_id equal to 0')
if not isinstance(ule_id.extension_id, int):
invalid_parameters.append('extension_id must be integer')
if ule_id.extension_id == 0:
invalid_parameters.append('extension_id equal to 0')
if hasattr(ule_id, 'main_user') and not isinstance(ule_id.main_user, bool):
invalid_parameters.append('main_user must be bool')
if hasattr(ule_id, 'main_line') and not isinstance(ule_id.main_line, bool):
invalid_parameters.append('main_line must be bool')
if invalid_parameters:
raise InvalidParametersError(invalid_parameters)
def _get_secondary_associations(ule):
nonexistent = {}
try:
extension = extension_dao.get(ule.extension_id)
except ElementNotExistsError:
nonexistent['extension_id'] = ule.extension_id
try:
line = line_dao.get(ule.line_id)
except ElementNotExistsError:
nonexistent['line_id'] = ule.line_id
try:
user = user_dao.get(ule.user_id)
except ElementNotExistsError:
nonexistent['user_id'] = ule.user_id
if len(nonexistent) > 0:
raise NonexistentParametersError(**nonexistent)
return user, line, extension
def check_if_user_and_line_already_linked(user, line):
if ule_dao.already_linked(user.id, line.id):
raise InvalidParametersError(['user is already associated to this line'])
def check_if_extension_in_context_range(extension):
if not context_services.is_extension_valid_for_context_range(extension, ContextRange.users):
raise InvalidParametersError(['Exten %s not inside user range of context %s' % (extension.exten, extension.context)])
def check_line_links_for_extension(line, extension):
user_lines = ule_dao.find_all_by_extension_id(extension.id)
for user_line in user_lines:
if user_line.line_id != line.id:
raise InvalidParametersError(['Extension %s@%s already linked to a line' % (extension.exten, extension.context)])
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