Commit cb12deab authored by Vojtech Sodoma's avatar Vojtech Sodoma

Merge branch '3389-labels-add-api' into 'master'

3389 labels add api

See merge request !66
parents c5a79321 5329501c
xivo-dao (2020.13.00) xivo-freya; urgency=medium
* 3389 Create labels api
-- Tomas Taraba <[email protected]> Wed, 22 Jul 2020 13:10:36 +0200
xivo-dao (2020.12.00) xivo-freya; urgency=medium
* Bump target version to 2020.12
......
......@@ -14,7 +14,7 @@
#
# 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 sqlalchemy import select, func
from sqlalchemy import select, func, UniqueConstraint
from sqlalchemy.orm import relationship, column_property
from sqlalchemy.schema import Column, PrimaryKeyConstraint
from sqlalchemy.types import Integer, String, Text
......@@ -27,6 +27,7 @@ class Labels(Base):
__tablename__ = 'labels'
__table_args__ = (
PrimaryKeyConstraint('id'),
UniqueConstraint('display_name')
)
id = Column(Integer, nullable=False, autoincrement=True)
......
......@@ -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
......@@ -35,6 +37,11 @@ def get(label_id):
return query.first()
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)
Session.flush()
......
......@@ -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