Commit d23ffa56 authored by Cedric Abunar's avatar Cedric Abunar Committed by Sebastien Duthil

add creation voicemail

parent 7b6aaca6
......@@ -21,6 +21,7 @@ setup(
'xivo_dao.data_handler.context',
'xivo_dao.data_handler.device',
'xivo_dao.data_handler.extension',
'xivo_dao.data_handler.language',
'xivo_dao.data_handler.line',
'xivo_dao.data_handler.user',
'xivo_dao.data_handler.user_line_extension',
......
# -*- 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.helpers.db_manager import Base
from sqlalchemy.schema import Column
from sqlalchemy.types import Integer, String
class StaticVoicemail(Base):
__tablename__ = 'staticvoicemail'
id = Column(Integer, primary_key=True)
cat_metric = Column(Integer, nullable=False, default=0)
var_metric = Column(Integer, nullable=False, default=0)
commented = Column(Integer, nullable=False, default=0)
filename = Column(String(128), nullable=False)
category = Column(String(128), nullable=False)
var_name = Column(String(128), nullable=False)
var_val = Column(String(128))
# -*- 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/>
LANGUAGE = [
'de_DE',
'en_US',
'es_ES',
'fr_FR',
'fr_CA',
'it_IT',
'nl_NL'
]
def find_all():
return LANGUAGE
......@@ -27,6 +27,7 @@ from xivo_dao.helpers.sysconfd_connector import SysconfdError
from xivo_dao.data_handler.exception import ElementNotExistsError, \
ElementCreationError, ElementEditionError, ElementDeletionError
from xivo_dao.helpers.abstract_model import SearchResult
from xivo_dao.alchemy.staticvoicemail import StaticVoicemail
@daosession
......@@ -91,6 +92,19 @@ def _apply_skip_and_limit(query, skip, limit):
return query
@daosession
def find_all_timezone(session):
rows = (session.query(StaticVoicemail.var_name)
.filter(StaticVoicemail.category == 'zonemessages')
.all())
res = []
for row in rows:
res.append(row[0])
return res
@daosession
def get_by_number_context(session, number, context):
row = (session.query(VoicemailSchema)
......
......@@ -89,6 +89,17 @@ class VoicemailDBConverter(DatabaseConverter):
return model
def to_source(self, model):
source = DatabaseConverter.to_source(self, model)
if source.attach is not None:
source.attach = int(bool(source.attach))
if source.deletevoicemail is not None:
source.deletevoicemail = int(bool(source.deletevoicemail))
if source.skipcheckpass is not None:
source.skipcheckpass = int(bool(source.skipcheckpass))
return source
db_converter = VoicemailDBConverter()
......
# -*- coding: utf-8 -*-
#
# Copyright (C) 2013 Avencall
#
# This program is free software: you can redistribute it and/or modify
......@@ -15,19 +15,25 @@
# 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.language import dao as language_dao
from xivo_dao.data_handler.voicemail import dao as voicemail_dao
from xivo_dao.helpers import sysconfd_connector
from xivo_dao.data_handler.context import services as context_services
from xivo_dao.data_handler.voicemail import notifier
from xivo_dao.helpers.sysconfd_connector import SysconfdError
from xivo_dao.data_handler.exception import MissingParametersError, \
InvalidParametersError, ElementAlreadyExistsError, ElementNotExistsError
InvalidParametersError, ElementAlreadyExistsError, ElementNotExistsError, \
NonexistentParametersError
from xivo_dao.helpers.validator import is_positive_number, is_context_exist
def find_all(skip=None, limit=None, order=None, direction=None, search=None):
return voicemail_dao.find_all(skip=skip, limit=limit, order=order, direction=direction, search=search)
def find_all_timezone():
return voicemail_dao.find_all_timezone()
def get_by_number_context(number, context):
return voicemail_dao.get_by_number_context(number, context)
......@@ -62,6 +68,7 @@ def delete(voicemail):
def _validate(voicemail):
_check_missing_parameters(voicemail)
_check_invalid_parameters(voicemail)
_check_nonexistent_parameters(voicemail)
def _check_missing_parameters(voicemail):
......@@ -72,16 +79,30 @@ def _check_missing_parameters(voicemail):
def _check_invalid_parameters(voicemail):
invalid_parameters = []
if not voicemail.name:
if voicemail.name is not None and not voicemail.name:
invalid_parameters.append('name')
if not voicemail.number.isdigit():
if voicemail.number is not None and not is_positive_number(voicemail.number):
invalid_parameters.append('number')
if not context_services.find_by_name(voicemail.context):
invalid_parameters.append('context')
if voicemail.max_messages is not None and not is_positive_number(voicemail.max_messages):
invalid_parameters.append('max_messages')
if voicemail.password is not None and not is_positive_number(voicemail.password):
invalid_parameters.append('password')
if invalid_parameters:
raise InvalidParametersError(invalid_parameters)
def _check_nonexistent_parameters(voicemail):
nonexistent_parameters = {}
if not is_context_exist(voicemail.context):
nonexistent_parameters['context'] = voicemail.context
if voicemail.language is not None and voicemail.language not in language_dao.find_all():
nonexistent_parameters['language'] = voicemail.language
if voicemail.timezone is not None and voicemail.timezone not in find_all_timezone():
nonexistent_parameters['timezone'] = voicemail.timezone
if nonexistent_parameters:
raise NonexistentParametersError(**nonexistent_parameters)
def _check_for_existing_voicemail(voicemail):
try:
if voicemail_dao.get_by_number_context(voicemail.number, voicemail.context):
......
......@@ -23,7 +23,7 @@ from xivo_dao.data_handler.voicemail import services as voicemail_services
from xivo_dao.data_handler.voicemail.model import Voicemail, VoicemailOrder
from xivo_dao.data_handler.exception import MissingParametersError, \
InvalidParametersError, ElementAlreadyExistsError, ElementCreationError, \
ElementDeletionError
ElementDeletionError, NonexistentParametersError
class TestVoicemail(unittest.TestCase):
......@@ -94,7 +94,7 @@ class TestVoicemail(unittest.TestCase):
self.assertRaises(MissingParametersError, voicemail_services.create, voicemail)
self.assertEquals(voicemail_notifier.created.call_count, 0)
@patch('xivo_dao.data_handler.context.services.find_by_name', Mock(return_value=None))
@patch('xivo_dao.data_handler.context.services.find_by_name', Mock(return_value=True))
@patch('xivo_dao.data_handler.voicemail.notifier.created')
@patch('xivo_dao.data_handler.voicemail.dao.create')
def test_create_empty_name(self, voicemail_dao_create, voicemail_notifier_created):
......@@ -138,7 +138,7 @@ class TestVoicemail(unittest.TestCase):
number=number,
context=context)
self.assertRaises(InvalidParametersError, voicemail_services.create, voicemail)
self.assertRaises(NonexistentParametersError, voicemail_services.create, voicemail)
self.assertEquals(voicemail_dao_create.call_count, 0)
self.assertEquals(voicemail_notifier_created.call_count, 0)
......
# -*- 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 mock import patch
from hamcrest import *
from unittest import TestCase
from xivo_dao.helpers.validator import is_positive_number
class TestValidator(TestCase):
def test_is_positive_number(self):
res = is_positive_number('toto')
assert_that(res, equal_to(False))
res = is_positive_number('-445')
assert_that(res, equal_to(False))
res = is_positive_number(-445)
assert_that(res, equal_to(False))
res = is_positive_number(35.4)
assert_that(res, equal_to(False))
res = is_positive_number('0000')
assert_that(res, equal_to(False))
res = is_positive_number('12345')
assert_that(res, equal_to(True))
res = is_positive_number(1234)
assert_that(res, equal_to(True))
# -*- 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.data_handler.context import services as context_services
def is_positive_number(string):
if str(string).isdigit() and int(string) > 0:
return True
else:
return False
def is_context_exist(context_name):
return context_services.find_by_name(context_name)
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