Commit 5065863f authored by Tomas Taraba's avatar Tomas Taraba

3389 Add duplicate display name validation

parent fac6388c
......@@ -14,6 +14,8 @@
#
# 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 sqlite3 import IntegrityError
from xivo_dao.alchemy.labels import Labels
from xivo_dao.helpers.db_manager import Session
from xivo_dao.helpers.db_manager import daosession
......@@ -34,10 +36,11 @@ def get(label_id):
query = Session.query(Labels).filter_by(id=label_id)
return query.first()
def create(label):
Session.add(label)
Session.flush()
return label
def get_by_display_name(label_display_name):
query = Session.query(Labels).filter(Labels.display_name == label_display_name)
return query.first()
def create(label):
Session.add(label)
......
......@@ -17,7 +17,9 @@
from hamcrest import assert_that, equal_to
from xivo_dao.alchemy.labels import Labels
from xivo_dao.resources.labels import dao as label_dao
from xivo_dao.helpers.exception import ResourceError
from xivo_dao.resources.labels import dao as label_dao, validator
from xivo_dao.resources.user import dao as user_dao
from xivo_dao.resources.utils.search import SearchResult
from xivo_dao.tests.test_dao import DAOTestCase
......@@ -56,6 +58,15 @@ class TestLabelExist(DAOTestCase):
expected = label_dao.get(label.id)
assert_that(label, equal_to(expected))
def test_create_duplicate_label(self):
props = {
'display_name': 'MyLabel',
'description': 'Any Label'
}
label = label_dao.create(Labels(**props))
self.assertRaises(ResourceError, validator.validate_create, label.display_name)
def test_get_all_labels_with_associated_user(self):
label_row = self.add_label()
user = self.add_user(callerid='"John Doe"')
......
# -*- coding: utf-8 -*-
# Copyright (C) 2020 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 import errors
from xivo_dao.resources.labels import dao
def validate_create(label_display_name):
_validate_exists_already(label_display_name)
def _validate_exists_already(label_display_name):
existing_label = _find_by_name(label_display_name)
if existing_label:
raise errors.resource_exists('Label', name=label_display_name)
def _find_by_name(label_display_name):
return dao.get_by_display_name(label_display_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