Commit 01a94e94 authored by Francois Blackburn's avatar Francois Blackburn Committed by Etienne Lessard

add outcall-extension relationship

parent 1cb0accc
# -*- coding: utf-8 -*-
# Copyright (C) 2012-2016 Avencall
# Copyright (C) 2016 Proformatique Inc.
#
# 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
......@@ -44,7 +45,15 @@ class DialPattern(Base):
Extension.typeval == cast(DialPattern.id, String))""",
foreign_keys='Extension.typeval',
uselist=False,
passive_deletes='all')
passive_deletes='all',
back_populates='dialpattern')
outcall = relationship('Outcall',
primaryjoin="""and_(DialPattern.type == 'outcall',
DialPattern.typeid == Outcall.id)""",
foreign_keys='DialPattern.typeid',
uselist=False,
back_populates='dialpatterns')
@hybrid_property
def external_prefix(self):
......
# -*- coding: utf-8 -*-
# Copyright (C) 2013-2016 Avencall
# Copyright (C) 2016 Proformatique Inc.
#
# 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
......@@ -15,8 +16,9 @@
# 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.schema import Column, UniqueConstraint, Index, \
PrimaryKeyConstraint
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.schema import Column, UniqueConstraint, Index, PrimaryKeyConstraint
from sqlalchemy.orm import relationship
from sqlalchemy.types import Integer, String, Boolean
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.sql import cast
......@@ -44,6 +46,15 @@ class Extension(Base):
type = Column(enum.extenumbers_type, nullable=False)
typeval = Column(IntAsString(255), nullable=False, server_default='')
dialpattern = relationship('DialPattern',
primaryjoin="""and_(Extension.type == 'outcall',
Extension.typeval == cast(DialPattern.id, String))""",
foreign_keys='Extension.typeval',
uselist=False,
back_populates='extension')
outcall = association_proxy('dialpattern', 'outcall')
@property
def name(self):
return self.typeval
......
# -*- coding: utf-8 -*-
#
# Copyright (C) 2014-2016 Avencall
# Copyright (C) 2016 Proformatique Inc.
#
# 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
......@@ -50,7 +51,8 @@ class Outcall(Base):
primaryjoin="""and_(DialPattern.type == 'outcall',
DialPattern.typeid == Outcall.id)""",
foreign_keys='DialPattern.typeid',
cascade='all, delete-orphan')
cascade='all, delete-orphan',
back_populates='outcall')
extensions = association_proxy('dialpatterns', 'extension',
creator=lambda _extension: DialPattern(type='outcall',
......
# -*- coding: utf-8 -*-
# Copyright (C) 2013-2016 Avencall
# Copyright (C) 2016 Proformatique Inc.
#
# 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
......@@ -15,7 +16,15 @@
# 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 hamcrest import assert_that, all_of, equal_to, has_items, has_property, none, contains, has_properties, contains_inanyorder
from hamcrest import (assert_that,
all_of,
contains,
contains_inanyorder,
equal_to,
has_items,
has_properties,
has_property,
none)
from xivo_dao.tests.test_dao import DAOTestCase
from xivo_dao.alchemy.extension import Extension
......@@ -547,3 +556,25 @@ class TestFindAllAgentActionExtensions(DAOTestCase):
result = extension_dao.find_all_agent_action_extensions()
assert_that(result, contains(expected))
class TestRelationship(DAOTestCase):
def test_incall_relationship(self):
incall_row = self.add_incall()
extension_row = self.add_extension(type='incall', typeval=str(incall_row.id))
extension = extension_dao.get(extension_row.id)
assert_that(extension, equal_to(extension_row))
assert_that(extension.incall, equal_to(incall_row))
def test_outcall_relationship(self):
outcall_row = self.add_outcall()
extension_row = self.add_extension()
outcall_row.associate_extension(extension_row)
extension = extension_dao.get(extension_row.id)
assert_that(extension, equal_to(extension_row))
assert_that(extension.outcall, equal_to(outcall_row))
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