Commit d4f54d5e authored by Jean Aunis's avatar Jean Aunis

added model for user; refactored to use an abstract class for mappings

parent 6a10df08
# -*- coding: UTF-8 -*-
#
# Copyright (C) 2012 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
def map_attributes(src_object, dst_object, mapping, default_values={}):
for dst_field, value in default_values.iteritems():
setattr(dst_object, dst_field, value)
for src_field in mapping:
if hasattr(src_object, src_field):
dst_field = mapping[src_field]
value = getattr(src_object, src_field)
setattr(dst_object, dst_field, value)
return dst_object
# -*- coding: utf-8 -*-
from abc import ABCMeta
# Copyright (C) 2013 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/>
class AbstractMapping(object):
__metaclass__ = ABCMeta
def alchemy_to_sdm(self, alchemy_instance):
sdm_instance = self.sdm_class()
return self.map_attributes(alchemy_instance,
sdm_instance,
self.mapping,
self.sdm_types)
def sdm_to_alchemy(self, sdm_instance):
alchemy_instance = self.alchemy_class()
return self.map_attributes(sdm_instance,
alchemy_instance,
self.reverse_mapping,
self.alchemy_types,
self.alchemy_default_values)
def sdm_to_alchemy_dict(self, user_dict):
result = {}
for k in user_dict:
if k in self.reverse_mapping:
new_key = self.reverse_mapping[k]
result[new_key] = user_dict[k]
if (new_key in self.alchemy_types):
result[new_key] = self.alchemy_types[new_key](result[new_key])
else:
raise AttributeError()
return result
def map_attributes(self, src_object, dst_object, mapping, cast, default_values={}):
for dst_field in default_values:
value = default_values[dst_field]
setattr(dst_object, dst_field, value)
for src_field in mapping:
if(hasattr(src_object, src_field)):
dst_field = mapping[src_field]
value = getattr(src_object, src_field)
setattr(dst_object, dst_field, value)
for dst_field, cast_fct in cast.iteritems():
value = getattr(dst_object, dst_field)
if (value is not None):
setattr(dst_object, dst_field, cast_fct(value))
return dst_object
# -*- coding: UTF-8 -*-
#
# Copyright (C) 2012 Avencall
# -*- coding: utf-8 -*-
# Copyright (C) 2013 Avencall
#
# This program is free software; you can redistribute it and/or modify
# 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
# 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,
......@@ -12,15 +12,14 @@
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# 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 unittest
from xivo_dao.helpers import object_mapping
from xivo_dao.mapping_alchemy_sdm.abstract_mapping import AbstractMapping
class TestObjectMapping(unittest.TestCase):
class TestAbstractMapping(unittest.TestCase):
def test_map_object(self):
class MySampleClass:
......@@ -29,11 +28,15 @@ class TestObjectMapping(unittest.TestCase):
class YourSampleClass:
pass
class ConcreteMapping(AbstractMapping):
pass
object_from = MySampleClass()
object_from.prop0_my = None
object_from.prop1_my = 'value1'
object_from.prop2_my = 2
object_to = YourSampleClass()
mapping = {'prop0_my': 'unused',
mapping = {'prop0_my': 'prop0_your',
'prop1_my': 'prop1_your',
'prop2_my': 'prop2_your'}
......@@ -41,11 +44,12 @@ class TestObjectMapping(unittest.TestCase):
'prop3_your': None,
'prop4_your': "c'est vache"
}
object_mapping.map_attributes(object_from, object_to, mapping, default_values)
cast = {
'prop0_your': int
}
my_mapping = ConcreteMapping()
my_mapping.map_attributes(object_from, object_to, mapping, cast, default_values)
self.assertEquals(object_from.prop1_my, object_to.prop1_your)
self.assertEquals(object_from.prop2_my, object_to.prop2_your)
self.assertEquals(object_to.prop3_your, None)
self.assertEquals(object_to.prop4_your, "c'est vache")
# -*- coding: utf-8 -*-
# Copyright (C) 2013 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.userfeatures import UserFeatures
from xivo_dao.mapping_alchemy_sdm import user_mapping
from xivo_dao.service_data_model.user_sdm import UserSdm
import unittest
from xivo_dao.mapping_alchemy_sdm.user_mapping import UserMapping
class TestUserMapping(unittest.TestCase):
def setUp(self):
user_alchemy = UserFeatures()
user_sdm = UserSdm()
user_sdm.id = user_alchemy.id = None
user_sdm.firstname = user_alchemy.firstname = "John"
user_sdm.lastname = user_alchemy.lastname = "Doe"
user_sdm.callerid = user_alchemy.callerid = "123"
user_sdm.username = user_alchemy.loginclient = "john"
user_sdm.password = user_alchemy.passwdclient = "123"
user_sdm.enableclient = user_alchemy.enableclient = False
user_sdm.musiconhold = user_alchemy.musiconhold = "my_music"
user_sdm.outcallerid = user_alchemy.outcallerid = "456"
user_sdm.mobilephonenumber = user_alchemy.mobilephonenumber = "06123123123"
user_sdm.userfield = user_alchemy.userfield = "abc"
user_sdm.timezone = user_alchemy.timezone = "Europe/France"
user_sdm.language = user_alchemy.language = "fr_FR"
user_sdm.description = user_alchemy.description = "sample user"
user_sdm.ctiprofileid = user_alchemy.cti_profile_id = None
user_sdm.voicemailid = user_alchemy.voicemailid = None
user_sdm.agentid = user_alchemy.agentid = None
user_sdm.entityid = user_alchemy.entityid = None
self.user_alchemy = user_alchemy
self.user_sdm = user_sdm
self.user_mapping = UserMapping()
def test_alchemy_to_sdm(self):
result = self.user_mapping.alchemy_to_sdm(self.user_alchemy)
self.assertEquals(self.user_sdm.__dict__, result.__dict__)
def test_sdm_to_alchemy(self):
self.user_alchemy.enableclient = 0
self.user_alchemy.ringintern = ''
self.user_alchemy.ringextern = ''
self.user_alchemy.ringgroup = ''
self.user_alchemy.ringforward = ''
self.user_alchemy.rightcallcode = ''
result = self.user_mapping.sdm_to_alchemy(self.user_sdm)
print self.user_alchemy.todict()
print result.todict()
self.assertEquals(self.user_alchemy.todict(), result.todict())
def test_sdm_to_alchemy_dict(self):
user_dict_sdm = {}
user_dict_alchemy = {}
firstname = "John"
enableclient = True
passwdclient = "supercesret"
user_dict_alchemy['firstname'] = firstname
user_dict_sdm['firstname'] = firstname
user_dict_alchemy['enableclient'] = int(enableclient)
user_dict_sdm['enableclient'] = enableclient
user_dict_alchemy['passwdclient'] = passwdclient
user_dict_sdm['password'] = passwdclient
result = self.user_mapping.sdm_to_alchemy_dict(user_dict_sdm)
self.assertEquals(user_dict_alchemy, result)
self.assertEquals(type(user_dict_alchemy['enableclient']), type(result['enableclient']))
def test_sdm_to_alchemy_dict_fails(self):
user_dict_sdm = {}
firstname = "John"
deletevoicemail = True
user_dict_sdm['firstname'] = firstname
user_dict_sdm['notExistingKey'] = deletevoicemail
self.assertRaises(AttributeError, self.user_mapping.sdm_to_alchemy_dict, user_dict_sdm)
......@@ -17,46 +17,47 @@
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from xivo_dao.alchemy.voicemail import Voicemail
from xivo_dao.mapping_alchemy_sdm import voicemail_mapping
from xivo_dao.mapping_alchemy_sdm.voicemail_mapping import VoicemailMapping
from xivo_dao.service_data_model.voicemail_sdm import VoicemailSdm
import unittest
class TestVoicemailMapping(unittest.TestCase):
def test_alchemy_to_sdm(self):
def setUp(self):
voicemail_alchemy = Voicemail()
voicemail_sdm = VoicemailSdm()
voicemail_alchemy.uniqueid = voicemail_sdm.id = 1
voicemail_alchemy.email = voicemail_sdm.email = "test@xivo.org"
voicemail_alchemy.fullname = voicemail_sdm.fullname = "John doe sdf &é'é- "
voicemail_alchemy.mailbox = voicemail_sdm.mailbox = "123@default.com"
voicemail_alchemy.password = voicemail_sdm.password = "delamortqui tue"
voicemail_alchemy.attach = voicemail_sdm.attach = False
voicemail_alchemy.skipcheckpass = voicemail_sdm.skipcheckpass = True
voicemail_alchemy.deletevoicemail = voicemail_sdm.deleteaftersend = True
result = voicemail_mapping.alchemy_to_sdm(voicemail_alchemy)
self.assertEquals(voicemail_sdm.__dict__, result.__dict__)
def test_sdm_to_alchemy(self):
voicemail_sdm = VoicemailSdm()
voicemail_alchemy = Voicemail()
voicemail_alchemy.uniqueid = voicemail_sdm.id = 1
voicemail_alchemy.email = voicemail_sdm.email = "test@xivo.org"
voicemail_alchemy.fullname = voicemail_sdm.fullname = "John doe sdf &é'é- "
voicemail_alchemy.mailbox = voicemail_sdm.mailbox = "123@default.com"
voicemail_alchemy.password = voicemail_sdm.password = "delamortqui tue"
voicemail_alchemy.attach = voicemail_sdm.attach = False
voicemail_alchemy.skipcheckpass = voicemail_sdm.skipcheckpass = True
voicemail_alchemy.deletevoicemail = voicemail_sdm.deleteaftersend = True
voicemail_alchemy.attach = 0
voicemail_sdm.attach = False
voicemail_alchemy.skipcheckpass = 1
voicemail_sdm.skipcheckpass = True
voicemail_alchemy.deletevoicemail = 1
voicemail_sdm.deleteaftersend = True
voicemail_alchemy.context = "default"
voicemail_alchemy.tz = "eu-fr"
result = voicemail_mapping.sdm_to_alchemy(voicemail_sdm)
self.assertEquals(voicemail_alchemy.todict(), result.todict())
self.voicemail_alchemy = voicemail_alchemy
self.voicemail_sdm = voicemail_sdm
self.voicemail_mapping = VoicemailMapping()
def test_alchemy_to_sdm(self):
result = self.voicemail_mapping.alchemy_to_sdm(self.voicemail_alchemy)
self.assertEquals(self.voicemail_sdm.__dict__, result.__dict__)
self.assertEquals(type(self.voicemail_sdm.deleteaftersend),
type(result.deleteaftersend))
self.assertEquals(type(self.voicemail_sdm.skipcheckpass),
type(result.skipcheckpass))
self.assertEquals(type(self.voicemail_sdm.attach),
type(result.attach))
def test_sdm_to_alchemy(self):
result = self.voicemail_mapping.sdm_to_alchemy(self.voicemail_sdm)
self.assertEquals(self.voicemail_alchemy.todict(), result.todict())
def test_sdm_to_alchemy_dict(self):
voicemail_dict_sdm = {}
......@@ -68,7 +69,7 @@ class TestVoicemailMapping(unittest.TestCase):
voicemail_dict_alchemy['deletevoicemail'] = int(deletevoicemail)
voicemail_dict_sdm['deleteaftersend'] = deletevoicemail
result = voicemail_mapping.sdm_to_alchemy_dict(voicemail_dict_sdm)
result = self.voicemail_mapping.sdm_to_alchemy_dict(voicemail_dict_sdm)
self.assertEquals(voicemail_dict_alchemy, result)
self.assertEquals(type(voicemail_dict_alchemy['deletevoicemail']), type(result['deletevoicemail']))
......@@ -79,7 +80,7 @@ class TestVoicemailMapping(unittest.TestCase):
voicemail_dict_sdm['fullname'] = fullname
voicemail_dict_sdm['notExistingKey'] = deletevoicemail
self.assertRaises(AttributeError, voicemail_mapping.sdm_to_alchemy_dict, voicemail_dict_sdm)
self.assertRaises(AttributeError, self.voicemail_mapping.sdm_to_alchemy_dict, voicemail_dict_sdm)
......
# -*- coding: utf-8 -*-
# Copyright (C) 2013 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.service_data_model.user_sdm import UserSdm
from xivo_dao.alchemy.userfeatures import UserFeatures
from xivo_dao.mapping_alchemy_sdm.abstract_mapping import AbstractMapping
class UserMapping(AbstractMapping):
#mapping = {alchemy_field: sdm_field}
mapping = {'id': 'id',
'firstname': 'firstname',
'lastname': 'lastname',
'callerid': 'callerid',
'loginclient': 'username',
'passwdclient': 'password',
'enableclient': 'enableclient',
'musiconhold': 'musiconhold',
'outcallerid': 'outcallerid',
'mobilephonenumber': 'mobilephonenumber',
'userfield': 'userfield',
'timezone': 'timezone',
'language': 'language',
'description': 'description',
'cti_profile_id': 'ctiprofileid',
'voicemailid': 'voicemailid',
'agentid': 'agentid',
'entityid': 'entityid'
}
reverse_mapping = dict([[v, k] for k, v in mapping.items()])
alchemy_default_values = {'entityid': 1,
'musiconhold': 'default',
'outcallerid': 'default',
'enableclient': 0,
'ringintern': '',
'ringextern': '',
'ringgroup': '',
'ringforward': '',
'rightcallcode': '',
'description': ''
}
alchemy_types = {
'enableclient': int,
}
sdm_types = {'enableclient': bool}
def __init__(self):
self.sdm_class = UserSdm
self.alchemy_class = UserFeatures
......@@ -18,62 +18,38 @@
from xivo_dao.alchemy.voicemail import Voicemail
from xivo_dao.service_data_model.voicemail_sdm import VoicemailSdm
from xivo_dao.helpers import object_mapping
#mapping = {alchemy_field: sdm_field}
mapping = {'uniqueid': 'id',
'email': 'email',
'fullname': 'fullname',
'mailbox': 'mailbox',
'password': 'password',
'attach': 'attach',
'skipcheckpass': 'skipcheckpass',
'deletevoicemail': 'deleteaftersend'
}
reverse_mapping = dict((v, k) for k, v in mapping.items())
alchemy_default_values = {'context': 'default',
'tz': 'eu-fr'}
alchemy_types = {
'uniqueid': int,
'email': str,
'fullname': str,
'mailbox': str,
'password': str,
'attach': int,
'skipcheckpass': int,
'deletevoicemail': int
from xivo_dao.mapping_alchemy_sdm.abstract_mapping import AbstractMapping
class VoicemailMapping(AbstractMapping):
#mapping = {alchemy_field: sdm_field}
mapping = {'uniqueid': 'id',
'email': 'email',
'fullname': 'fullname',
'mailbox': 'mailbox',
'password': 'password',
'attach': 'attach',
'skipcheckpass': 'skipcheckpass',
'deletevoicemail': 'deleteaftersend'
}
reverse_mapping = dict((v, k) for k, v in mapping.items())
alchemy_default_values = {'context': 'default',
'tz': 'eu-fr'}
alchemy_types = {
'attach': int,
'skipcheckpass': int,
'deletevoicemail': int
}
sdm_types = {
'attach': bool,
'skipcheckpass': bool,
'deleteaftersend': bool
}
def alchemy_to_sdm(voicemail_alchemy):
voicemail_sdm = VoicemailSdm()
return object_mapping.map_attributes(voicemail_alchemy,
voicemail_sdm,
mapping)
def sdm_to_alchemy(voicemail_sdm):
voicemail_alchemy = Voicemail()
return object_mapping.map_attributes(voicemail_sdm,
voicemail_alchemy,
reverse_mapping,
alchemy_default_values)
def sdm_to_alchemy_dict(voicemail_dict):
result = {}
for k in voicemail_dict:
if k in reverse_mapping:
new_key = reverse_mapping[k]
result[new_key] = voicemail_dict[k]
result[new_key] = alchemy_types[new_key](result[new_key])
else:
raise AttributeError()
return result
def __init__(self):
self.sdm_class = VoicemailSdm
self.alchemy_class = Voicemail
'''
Created on Mar 14, 2013
# -*- coding: utf-8 -*-
# Copyright (C) 2013 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/>
@author: jean
'''
from xivo_dao.service_data_model.base_sdm import BaseSdm
import unittest
from xivo_dao.service_data_model.sdm_exception import IncorrectParametersException
......
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