Example #1
0
    def setUp(self):
        self._api_data_dict = {
            'api_key_1': 1,
            'api_key_2': '2',
            'api_key_3': [1, '2']
        }
        self._model_data_dict = {
            'model_key_1': 1,
            'model_key_2': '2',
            'model_key_3': [1, '2']
        }

        self._mapper = Mock()
        self._mapper.MAPPING = {
            'model_key_1': 'api_key_1',
            'model_key_2': 'api_key_2',
            'model_key_3': 'api_key_3'
        }
        self._mapper.add_links_to_dict = Mock()
        self._mapper.add_links_to_dict.side_effect = self.add_links_to_dict

        self._serializer = Mock(serializer)
        self._serializer.decode.side_effect = json.loads
        self._serializer.encode.side_effect = json.dumps

        self.model = Mock(model_key_1=1, model_key_2='2', model_key_3=[1, '2'])
        self.model.to_user_data.return_value = self._model_data_dict

        self._model_class = Mock(AbstractModels)
        self._model_class.from_user_data.return_value = self.model

        self.formatter = Formatter(self._mapper, self._serializer,
                                   self._model_class)
Example #2
0
    def setUp(self):
        self._api_data_dict = {
            'api_key_1': 1,
            'api_key_2': '2',
            'api_key_3': [1, '2']
        }
        self._model_data_dict = {
            'model_key_1': 1,
            'model_key_2': '2',
            'model_key_3': [1, '2']
        }

        self._mapper = Mock()
        self._mapper.MAPPING = {
            'model_key_1': 'api_key_1',
            'model_key_2': 'api_key_2',
            'model_key_3': 'api_key_3'
        }
        self._mapper.add_links_to_dict = Mock()
        self._mapper.add_links_to_dict.side_effect = self.add_links_to_dict

        self._serializer = Mock(serializer)
        self._serializer.decode.side_effect = json.loads
        self._serializer.encode.side_effect = json.dumps

        self.model = Mock(
            model_key_1=1,
            model_key_2='2',
            model_key_3=[1, '2']
        )
        self.model.to_user_data.return_value = self._model_data_dict

        self._model_class = Mock(AbstractModels)
        self._model_class.from_user_data.return_value = self.model

        self.formatter = Formatter(self._mapper,
                                   self._serializer,
                                   self._model_class)
Example #3
0
 def dict_to_model(self, api_data, user_id):
     model = Formatter.dict_to_model(self, api_data)
     model.user_id = user_id
     return model
Example #4
0
 def __init__(self):
     Formatter.__init__(self, mapper, serializer, UserVoicemail)
Example #5
0
from xivo_restapi.helpers.route_generator import RouteGenerator
from xivo_restapi.helpers.formatter import Formatter
from xivo_restapi.resources.voicemails import mapper
from xivo_restapi.helpers import serializer
from xivo_restapi.helpers.common import extract_search_parameters
from xivo_dao.data_handler.voicemail.model import Voicemail
from xivo_dao.data_handler.voicemail import services as voicemail_services

from xivo_restapi.flask_http_server import content_parser
from xivo_restapi.helpers.premacop import Field, Unicode, Int, Boolean


logger = logging.getLogger(__name__)
blueprint = Blueprint('voicemails', __name__, url_prefix='/%s/voicemails' % config.VERSION_1_1)
route = RouteGenerator(blueprint)
formatter = Formatter(mapper, serializer, Voicemail)

document = content_parser.document(
    Field('id', Int()),
    Field('name', Unicode()),
    Field('number', Unicode()),
    Field('context', Unicode()),
    Field('password', Unicode()),
    Field('email', Unicode()),
    Field('language', Unicode()),
    Field('timezone', Unicode()),
    Field('max_messages', Int()),
    Field('attach_audio', Boolean()),
    Field('delete_messages', Boolean()),
    Field('ask_password', Boolean())
)
Example #6
0
from flask.blueprints import Blueprint

from xivo_restapi import config
from xivo_restapi.helpers.route_generator import RouteGenerator
from xivo_restapi.helpers import serializer
from flask.helpers import make_response
from xivo_dao.data_handler.cti_profile import services
from xivo_restapi.helpers.formatter import Formatter
from xivo_dao.data_handler.cti_profile.model import CtiProfile
from . import mapper

blueprint = Blueprint('cti_profiles',
                      __name__,
                      url_prefix='/%s/cti_profiles' % config.VERSION_1_1)
route = RouteGenerator(blueprint)
formatter = Formatter(mapper, serializer, CtiProfile)


@route('', methods=['GET'])
def find_all():
    profiles = services.find_all()
    result = formatter.list_to_api(profiles)
    return make_response(result, 200)


@route('/<int:cti_profile_id>', methods=['GET'])
def get(cti_profile_id):
    profile = services.get(cti_profile_id)
    result = formatter.to_api(profile)
    return make_response(result, 200)
Example #7
0
 def dict_to_model(self, api_data, user_id):
     model = Formatter.dict_to_model(self, api_data)
     model.user_id = user_id
     return model
Example #8
0
 def dict_to_model(self, dict_data, line_id):
     model = Formatter.dict_to_model(self, dict_data)
     model.line_id = line_id
     return model
Example #9
0
class TestFormatter(unittest.TestCase):
    def setUp(self):
        self._api_data_dict = {
            'api_key_1': 1,
            'api_key_2': '2',
            'api_key_3': [1, '2']
        }
        self._model_data_dict = {
            'model_key_1': 1,
            'model_key_2': '2',
            'model_key_3': [1, '2']
        }

        self._mapper = Mock()
        self._mapper.MAPPING = {
            'model_key_1': 'api_key_1',
            'model_key_2': 'api_key_2',
            'model_key_3': 'api_key_3'
        }
        self._mapper.add_links_to_dict = Mock()
        self._mapper.add_links_to_dict.side_effect = self.add_links_to_dict

        self._serializer = Mock(serializer)
        self._serializer.decode.side_effect = json.loads
        self._serializer.encode.side_effect = json.dumps

        self.model = Mock(model_key_1=1, model_key_2='2', model_key_3=[1, '2'])
        self.model.to_user_data.return_value = self._model_data_dict

        self._model_class = Mock(AbstractModels)
        self._model_class.from_user_data.return_value = self.model

        self.formatter = Formatter(self._mapper, self._serializer,
                                   self._model_class)

    def add_links_to_dict(self, data_dict, _):
        data_dict.update({'links': 'links'})

    def test_list_to_api(self):
        expected_result = '{"items": [{"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"], "links": "links"}, {"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"], "links": "links"}], "total": 2}'

        list_model = [self.model, self.model]

        result = self.formatter.list_to_api(list_model)

        assert_that(result, equal_to(expected_result))

    def test_list_to_api_with_total(self):
        expected_result = '{"items": [{"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"], "links": "links"}, {"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"], "links": "links"}], "total": 10}'
        total = 10

        list_model = [self.model, self.model]

        result = self.formatter.list_to_api(list_model, total)

        assert_that(result, equal_to(expected_result))

    @patch('xivo_restapi.helpers.mapper.map_to_api')
    def test_to_api(self, map_to_api):
        expected_result = '{"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"], "links": "links"}'

        map_to_api.return_value = self._api_data_dict

        result = self.formatter.to_api(self.model)

        assert_that(result, equal_to(expected_result))
        self.model.to_user_data.assert_called_once_with()
        map_to_api.assert_called_once_with(self._mapper.MAPPING,
                                           self._model_data_dict)
        self._mapper.add_links_to_dict.assert_called_once_with(
            self._api_data_dict, self.model)
        self._serializer.encode.assert_called_once_with(self._api_data_dict)

    @patch('xivo_restapi.helpers.mapper.map_to_model')
    def test_to_model(self, map_to_model):
        expected_result = self.model
        api_data = '{"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"]}'

        map_to_model.return_value = self._model_data_dict

        result = self.formatter.to_model(api_data)

        assert_that(result, equal_to(expected_result))
        self._serializer.decode.assert_called_once_with(api_data)
        map_to_model.assert_called_once_with(self._mapper.MAPPING,
                                             self._api_data_dict)
        self._model_class.from_user_data.assert_called_once_with(
            self._model_data_dict)

    @patch('xivo_restapi.helpers.mapper.map_to_model')
    def test_dict_to_model(self, map_to_model):
        expected_result = self.model
        dict_data = {"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"]}

        map_to_model.return_value = self._model_data_dict

        result = self.formatter.dict_to_model(dict_data)

        assert_that(result, equal_to(expected_result))
        map_to_model.assert_called_once_with(self._mapper.MAPPING,
                                             self._api_data_dict)
        self._model_class.from_user_data.assert_called_once_with(
            self._model_data_dict)

    @patch('xivo_restapi.helpers.mapper.map_to_model')
    def test_update_model(self, map_to_model):
        my_model = Mock(model_key_1='lol',
                        model_key_2='lol',
                        model_key_3=['lol', 'lol'])

        def update_from_data(data_dict):
            for key, val in data_dict.iteritems():
                setattr(my_model, key, val)

        my_model.update_from_data.side_effect = update_from_data

        api_data = '{"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"]}'

        map_to_model.return_value = self._model_data_dict

        self.formatter.update_model(api_data, my_model)

        assert_that(
            my_model,
            all_of(
                has_property('model_key_1',
                             self._model_data_dict['model_key_1']),
                has_property('model_key_2',
                             self._model_data_dict['model_key_2']),
                has_property('model_key_3',
                             self._model_data_dict['model_key_3'])))
        map_to_model.assert_called_once_with(self._mapper.MAPPING,
                                             self._api_data_dict)
        my_model.update_from_data.assert_called_once_with(
            self._model_data_dict)

    @patch('xivo_restapi.helpers.mapper.map_to_model')
    def test_update_dict_model(self, map_to_model):
        my_model = Mock(model_key_1='lol',
                        model_key_2='lol',
                        model_key_3=['lol', 'lol'])

        def update_from_data(data_dict):
            for key, val in data_dict.iteritems():
                setattr(my_model, key, val)

        my_model.update_from_data.side_effect = update_from_data

        data_dict = {"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"]}

        map_to_model.return_value = self._model_data_dict

        self.formatter.update_dict_model(data_dict, my_model)

        assert_that(
            my_model,
            all_of(
                has_property('model_key_1',
                             self._model_data_dict['model_key_1']),
                has_property('model_key_2',
                             self._model_data_dict['model_key_2']),
                has_property('model_key_3',
                             self._model_data_dict['model_key_3'])))
        map_to_model.assert_called_once_with(self._mapper.MAPPING,
                                             self._api_data_dict)
        my_model.update_from_data.assert_called_once_with(
            self._model_data_dict)
Example #10
0
from flask.helpers import make_response
from xivo_dao.data_handler.line import services as line_services
from xivo_dao.data_handler.line.model import LineSIP
from xivo_restapi import config
from xivo_restapi.helpers import serializer
from xivo_restapi.helpers.route_generator import RouteGenerator
from xivo_restapi.helpers.formatter import Formatter

from xivo_restapi.flask_http_server import content_parser
from xivo_restapi.helpers.premacop import Field, Int, Unicode


logger = logging.getLogger(__name__)
blueprint = Blueprint('lines_sip', __name__, url_prefix='/%s/lines_sip' % config.VERSION_1_1)
route = RouteGenerator(blueprint)
formatter = Formatter(mapper_sip, serializer, LineSIP)

document = content_parser.document(
    Field('id', Int()),
    Field('username', Unicode()),
    Field('secret', Unicode()),
    Field('callerid', Unicode()),
    Field('provisioning_extension', Unicode()),
    Field('device_slot', Int()),
    Field('context', Unicode()),
)


@route('')
def list_sip():
    lines = line_services.find_all_by_protocol('sip')
Example #11
0
import logging

from flask import request, make_response

from xivo_restapi.helpers.formatter import Formatter
from xivo_restapi.resources.func_keys import mapper
from xivo_restapi.helpers import serializer
from xivo_restapi.helpers.common import extract_search_parameters
from xivo_dao.data_handler.func_key.model import FuncKey
from xivo_dao.data_handler.func_key import services as func_key_services


logger = logging.getLogger(__name__)
formatter = Formatter(mapper, serializer, FuncKey)


def list():
    search_parameters = extract_search_parameters(request.args)
    search_result = func_key_services.search(**search_parameters)
    result = formatter.list_to_api(search_result.items, search_result.total)
    return make_response(result, 200)


def get(func_key_id):
    func_key = func_key_services.get(func_key_id)
    result = formatter.to_api(func_key)
    return make_response(result, 200)
Example #12
0
# along with this program.  If not, see <http://www.gnu.org/licenses/>

import logging

from . import mapper
from .routes import line_route as route

from flask.globals import request
from flask.helpers import make_response
from xivo_dao.data_handler.line.model import Line
from xivo_dao.data_handler.line import services as line_services
from xivo_restapi.helpers import serializer
from xivo_restapi.helpers.formatter import Formatter

logger = logging.getLogger(__name__)
formatter = Formatter(mapper, serializer, Line)


@route('')
def list():
    if 'q' in request.args:
        lines = line_services.find_all_by_name(request.args['q'])
    else:
        lines = line_services.find_all()

    result = formatter.list_to_api(lines)
    return make_response(result, 200)


@route('/<int:lineid>')
def get(lineid):
Example #13
0
 def __init__(self):
     Formatter.__init__(self, mapper, serializer, UserLine)
Example #14
0
 def __init__(self):
     Formatter.__init__(self, mapper, serializer, UserCtiProfile)
Example #15
0
 def dict_to_model(self, line_id, api_data):
     model = Formatter.dict_to_model(self, api_data)
     model.line_id = line_id
     return model
Example #16
0
class TestFormatter(unittest.TestCase):

    def setUp(self):
        self._api_data_dict = {
            'api_key_1': 1,
            'api_key_2': '2',
            'api_key_3': [1, '2']
        }
        self._model_data_dict = {
            'model_key_1': 1,
            'model_key_2': '2',
            'model_key_3': [1, '2']
        }

        self._mapper = Mock()
        self._mapper.MAPPING = {
            'model_key_1': 'api_key_1',
            'model_key_2': 'api_key_2',
            'model_key_3': 'api_key_3'
        }
        self._mapper.add_links_to_dict = Mock()
        self._mapper.add_links_to_dict.side_effect = self.add_links_to_dict

        self._serializer = Mock(serializer)
        self._serializer.decode.side_effect = json.loads
        self._serializer.encode.side_effect = json.dumps

        self.model = Mock(
            model_key_1=1,
            model_key_2='2',
            model_key_3=[1, '2']
        )
        self.model.to_user_data.return_value = self._model_data_dict

        self._model_class = Mock(AbstractModels)
        self._model_class.from_user_data.return_value = self.model

        self.formatter = Formatter(self._mapper,
                                   self._serializer,
                                   self._model_class)

    def add_links_to_dict(self, data_dict, _):
        data_dict.update({'links': 'links'})

    def test_list_to_api(self):
        expected_result = '{"items": [{"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"], "links": "links"}, {"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"], "links": "links"}], "total": 2}'

        list_model = [self.model, self.model]

        result = self.formatter.list_to_api(list_model)

        assert_that(result, equal_to(expected_result))

    def test_list_to_api_with_total(self):
        expected_result = '{"items": [{"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"], "links": "links"}, {"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"], "links": "links"}], "total": 10}'
        total = 10

        list_model = [self.model, self.model]

        result = self.formatter.list_to_api(list_model, total)

        assert_that(result, equal_to(expected_result))

    @patch('xivo_restapi.helpers.mapper.map_to_api')
    def test_to_api(self, map_to_api):
        expected_result = '{"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"], "links": "links"}'

        map_to_api.return_value = self._api_data_dict

        result = self.formatter.to_api(self.model)

        assert_that(result, equal_to(expected_result))
        self.model.to_user_data.assert_called_once_with()
        map_to_api.assert_called_once_with(self._mapper.MAPPING, self._model_data_dict)
        self._mapper.add_links_to_dict.assert_called_once_with(self._api_data_dict, self.model)
        self._serializer.encode.assert_called_once_with(self._api_data_dict)

    @patch('xivo_restapi.helpers.mapper.map_to_model')
    def test_to_model(self, map_to_model):
        expected_result = self.model
        api_data = '{"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"]}'

        map_to_model.return_value = self._model_data_dict

        result = self.formatter.to_model(api_data)

        assert_that(result, equal_to(expected_result))
        self._serializer.decode.assert_called_once_with(api_data)
        map_to_model.assert_called_once_with(self._mapper.MAPPING, self._api_data_dict)
        self._model_class.from_user_data.assert_called_once_with(self._model_data_dict)

    @patch('xivo_restapi.helpers.mapper.map_to_model')
    def test_dict_to_model(self, map_to_model):
        expected_result = self.model
        dict_data = {"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"]}

        map_to_model.return_value = self._model_data_dict

        result = self.formatter.dict_to_model(dict_data)

        assert_that(result, equal_to(expected_result))
        map_to_model.assert_called_once_with(self._mapper.MAPPING, self._api_data_dict)
        self._model_class.from_user_data.assert_called_once_with(self._model_data_dict)

    @patch('xivo_restapi.helpers.mapper.map_to_model')
    def test_update_model(self, map_to_model):
        my_model = Mock(model_key_1='lol',
                        model_key_2='lol',
                        model_key_3=['lol', 'lol'])

        def update_from_data(data_dict):
            for key, val in data_dict.iteritems():
                setattr(my_model, key, val)

        my_model.update_from_data.side_effect = update_from_data

        api_data = '{"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"]}'

        map_to_model.return_value = self._model_data_dict

        self.formatter.update_model(api_data, my_model)

        assert_that(my_model, all_of(
            has_property('model_key_1', self._model_data_dict['model_key_1']),
            has_property('model_key_2', self._model_data_dict['model_key_2']),
            has_property('model_key_3', self._model_data_dict['model_key_3'])
        ))
        map_to_model.assert_called_once_with(self._mapper.MAPPING, self._api_data_dict)
        my_model.update_from_data.assert_called_once_with(self._model_data_dict)

    @patch('xivo_restapi.helpers.mapper.map_to_model')
    def test_update_dict_model(self, map_to_model):
        my_model = Mock(model_key_1='lol',
                        model_key_2='lol',
                        model_key_3=['lol', 'lol'])

        def update_from_data(data_dict):
            for key, val in data_dict.iteritems():
                setattr(my_model, key, val)

        my_model.update_from_data.side_effect = update_from_data

        data_dict = {"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"]}

        map_to_model.return_value = self._model_data_dict

        self.formatter.update_dict_model(data_dict, my_model)

        assert_that(my_model, all_of(
            has_property('model_key_1', self._model_data_dict['model_key_1']),
            has_property('model_key_2', self._model_data_dict['model_key_2']),
            has_property('model_key_3', self._model_data_dict['model_key_3'])
        ))
        map_to_model.assert_called_once_with(self._mapper.MAPPING, self._api_data_dict)
        my_model.update_from_data.assert_called_once_with(self._model_data_dict)
Example #17
0
from flask.globals import request
from flask.helpers import make_response

from xivo_dao.data_handler.user import services as user_services
from xivo_dao.data_handler.user.model import User

from xivo_restapi.helpers import serializer
from xivo_restapi.helpers.common import extract_search_parameters
from xivo_restapi.helpers.formatter import Formatter
from xivo_restapi.resources.users.routes import route

from xivo_restapi.flask_http_server import content_parser
from xivo_restapi.helpers.premacop import Field, Unicode, Int

logger = logging.getLogger(__name__)
formatter = Formatter(mapper, serializer, User)

document = content_parser.document(
    Field('id', Int()),
    Field('firstname', Unicode()),
    Field('lastname', Unicode()),
    Field('caller_id', Unicode()),
    Field('outgoing_caller_id', Unicode()),
    Field('username', Unicode()),
    Field('password', Unicode()),
    Field('music_on_hold', Unicode()),
    Field('mobile_phone_number', Unicode()),
    Field('userfield', Unicode()),
    Field('timezone', Unicode()),
    Field('language', Unicode()),
    Field('description', Unicode()),
Example #18
0
 def __init__(self):
     Formatter.__init__(self, mapper, serializer, LineExtension)
Example #19
0
                                   Field('mac', Unicode()),
                                   Field('sn', Unicode()),
                                   Field('plugin', Unicode()),
                                   Field('vendor', Unicode()),
                                   Field('model', Unicode()),
                                   Field('version', Unicode()),
                                   Field('description', Unicode()),
                                   Field('status', Unicode()),
                                   Field('template_id', Unicode()))

logger = logging.getLogger(__name__)
blueprint = Blueprint('devices',
                      __name__,
                      url_prefix='/%s/devices' % config.VERSION_1_1)
route = RouteGenerator(blueprint)
formatter = Formatter(mapper, serializer, Device)


@route('/<deviceid>')
def get(deviceid):
    device = device_services.get(deviceid)
    result = formatter.to_api(device)
    return make_response(result, 200)


@route('')
def list():
    search_parameters = extract_search_parameters(request.args)
    search_result = device_services.search(**search_parameters)
    result = formatter.list_to_api(search_result.items, search_result.total)
    return make_response(result, 200)
Example #20
0
 def __init__(self):
     Formatter.__init__(self, mapper, serializer, UserLine)
Example #21
0
 def dict_to_model(self, line_id, api_data):
     model = Formatter.dict_to_model(self, api_data)
     model.line_id = line_id
     return model
Example #22
0
from flask import url_for, request
from flask.helpers import make_response
from xivo_dao.data_handler.extension import services as extension_services
from xivo_dao.data_handler.extension.model import Extension
from xivo_restapi.helpers import serializer
from xivo_restapi.helpers.common import extract_search_parameters
from xivo_restapi.helpers.formatter import Formatter
from xivo_restapi.resources.extensions.routes import extension_route as route

from xivo_restapi.flask_http_server import content_parser
from xivo_restapi.helpers.premacop import Field, Int, Unicode, Boolean


logger = logging.getLogger(__name__)
formatter = Formatter(mapper, serializer, Extension)
extra_parameters = ['type']

document = content_parser.document(
    Field('id', Int()),
    Field('exten', Unicode()),
    Field('context', Unicode()),
    Field('commented', Boolean())
)


@route('')
def list():
    parameters = extract_search_parameters(request.args, extra_parameters)
    search_result = extension_services.search(**parameters)
    result = formatter.list_to_api(search_result.items, search_result.total)
Example #23
0
 def __init__(self):
     Formatter.__init__(self, mapper, serializer, UserCtiProfile)