Commit e4f6ab54 authored by Francois Blackburn's avatar Francois Blackburn

outcall: move extension relationship to dialpattern

reason: An Extension(type='outcall', typeval='id') is mapped to a
dialpattern id and not to an outcall id ...
parent 0ff07fd0
......@@ -16,9 +16,11 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import relationship
from sqlalchemy.schema import Column, PrimaryKeyConstraint
from sqlalchemy.types import Integer, String
from xivo_dao.alchemy.extension import Extension
from xivo_dao.helpers.db_manager import Base
......@@ -38,6 +40,13 @@ class DialPattern(Base):
stripnum = Column(Integer)
callerid = Column(String(80))
extension = relationship('Extension',
primaryjoin="""and_(Extension.type == 'outcall',
Extension.typeval == cast(DialPattern.id, String))""",
foreign_keys='[Extension.typeval]',
uselist=False,
cascade='all, delete-orphan')
@hybrid_property
def external_prefix(self):
return self.externprefix
......@@ -52,8 +61,14 @@ class DialPattern(Base):
@pattern.setter
def pattern(self, value):
self._update_exten(value)
self.exten = value
def _update_exten(self, exten):
if not self.extension:
self.extension = Extension(type='outcall')
self.extension.exten = exten
@hybrid_property
def strip_digits(self):
return self.stripnum
......
......@@ -23,7 +23,6 @@ from sqlalchemy.schema import Column, PrimaryKeyConstraint, UniqueConstraint
from sqlalchemy.sql import func, cast, not_
from sqlalchemy.types import Integer, String, Text, Boolean
from xivo_dao.alchemy.extension import Extension
from xivo_dao.helpers.db_manager import Base
......@@ -51,13 +50,6 @@ class Outcall(Base):
foreign_keys='DialPattern.typeid',
cascade='all, delete-orphan')
extensions = relationship('Extension',
primaryjoin="""and_(Extension.type == 'outcall',
Extension.typeval == cast(Outcall.id, String),
Extension.context == Outcall.context)""",
foreign_keys='[Extension.typeval, Extension.context]',
cascade='all, delete-orphan')
@hybrid_property
def internal_caller_id(self):
return self.internal == 1
......@@ -106,10 +98,8 @@ class Outcall(Base):
@patterns.setter
def patterns(self, patterns):
old_dialpatterns = self.dialpatterns if self.dialpatterns else []
old_extensions = self.extensions if self.extensions else []
self.dialpatterns = []
self.extensions = []
for pattern, dialpattern, extension in itertools.izip_longest(patterns, old_dialpatterns, old_extensions):
for pattern, dialpattern in itertools.izip_longest(patterns, old_dialpatterns):
if not pattern:
continue
......@@ -117,9 +107,6 @@ class Outcall(Base):
dialpattern = pattern
dialpattern.type = 'outcall'
if not extension:
extension = Extension(type='outcall')
dialpattern.external_prefix = pattern.external_prefix
dialpattern.prefix = pattern.prefix
dialpattern.pattern = pattern.pattern
......@@ -127,5 +114,8 @@ class Outcall(Base):
dialpattern.caller_id = pattern.caller_id
self.dialpatterns.append(dialpattern)
extension.exten = pattern.pattern
self.extensions.append(extension)
def fix_context(self):
for pattern in self.dialpatterns:
if pattern.extension is None:
continue
pattern.extension.context = self.context
......@@ -56,11 +56,13 @@ class OutcallPersistor(CriteriaBuilderMixin):
def create(self, outcall):
self.session.add(outcall)
outcall.fix_context()
self.session.flush()
return outcall
def edit(self, outcall):
self.session.add(outcall)
outcall.fix_context()
self.session.flush()
def delete(self, outcall):
......
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