Commit 55fe7af2 authored by XivoBuilder's avatar XivoBuilder

Add service data model voicemail class with mapping and support functions

parent c3140b26
......@@ -13,5 +13,7 @@ setup(
license='GPLv3',
packages=['xivo_dao',
'xivo_dao.alchemy',
'xivo_dao.helpers'],
'xivo_dao.helpers',
'xivo_dao.service_data_model',
'xivo_dao.mapping_alchemy_sdm'],
)
# -*- 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 in default_values:
value = default_values[dst_field]
setattr(dst_object, dst_field, value)
for src_field in mapping:
if src_field in src_object.__dict__:
dst_field = mapping[src_field]
value = getattr(src_object, src_field)
setattr(dst_object, dst_field, value)
return dst_object
# -*- 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.
import unittest
from xivo_dao.helpers import object_mapping
class TestObjectMapping(unittest.TestCase):
def test_map_object(self):
class MySampleClass:
pass
class YourSampleClass:
pass
object_from = MySampleClass()
object_from.prop1_my = 'value1'
object_from.prop2_my = 2
object_to = YourSampleClass()
mapping = {'prop0_my': 'unused',
'prop1_my': 'prop1_your',
'prop2_my': 'prop2_your'}
default_values = {'prop2_your': 3,
'prop3_your': None,
'prop4_your': "c'est vache"
}
object_mapping.map_attributes(object_from, object_to, mapping, 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) 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.
from xivo_dao.alchemy.voicemail import Voicemail
from xivo_dao.mapping_alchemy_sdm import voicemail_mapping
from xivo_dao.service_data_model.voicemail_sdm import VoicemailSdm
import unittest
class TestVoicemailMapping(unittest.TestCase):
def test_alchemy_to_sdm(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.context = "default"
voicemail_alchemy.tz = "eu-fr"
result = voicemail_mapping.sdm_to_alchemy(voicemail_sdm)
self.assertEquals(voicemail_alchemy.todict(), result.todict())
def test_sdm_to_alchemy_dict(self):
voicemail_dict_sdm = {}
voicemail_dict_alchemy = {}
fullname = "John doe sdf &é'é- "
deletevoicemail = True
voicemail_dict_alchemy['fullname'] = fullname
voicemail_dict_sdm['fullname'] = fullname
voicemail_dict_alchemy['deletevoicemail'] = deletevoicemail
voicemail_dict_sdm['deleteaftersend'] = deletevoicemail
result = voicemail_mapping.sdm_to_alchemy_dict(voicemail_dict_sdm)
self.assertEquals(voicemail_dict_alchemy, result)
def test_sdm_to_alchemy_dict_fails(self):
voicemail_dict_sdm = {}
fullname = "John doe sdf &é'é- "
deletevoicemail = True
voicemail_dict_sdm['fullname'] = fullname
voicemail_dict_sdm['notExistingKey'] = deletevoicemail
self.assertRaises(AttributeError, voicemail_mapping.sdm_to_alchemy_dict, voicemail_dict_sdm)
# -*- 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.
from xivo_dao.alchemy.voicemail import Voicemail
from xivo_dao.service_data_model.voicemail_sdm import VoicemailSdm
from xivo_dao.helpers import object_mapping
import re
#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'}
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]
else:
raise AttributeError()
return result
# -*- 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.
class UserSdm(object):
def __init__(self):
self.id = None
self.firstname = ""
self.lastname = ""
self.callerid = ""
self.username = ""
self.password = ""
self.enableclient = False
self.musiconhold = ""
self.outcallerid = ""
self.mobilephonenumber = ""
# TODO: explain
self.userfield = ""
self.timezone = ""
self.language = ""
self.description = ""
self.cti_profile_id = None
self.voicemailid = None
self.agentid = None
self.entityid = None
# -*- 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.
class VoicemailSdm(object):
def __init__(self):
# Integer: ID attributed by the database
self.id = None
# String: email used to send voicemail notifications
self.email = ""
self.fullname = ""
self.mailbox = ""
self.password = ""
self.attach = False
self.skipcheckpass = False
self.deleteaftersend = False
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