Commit ff527cd6 authored by Francois Blackburn's avatar Francois Blackburn Committed by Etienne Lessard

outcall: add extension association

Also remove the concept of patterns (not used)
Since an extension is associated to an outcall, each extension has its
own context. So the outcall should not have a context
parent f06399f3
......@@ -20,7 +20,6 @@ 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
......@@ -43,9 +42,9 @@ class DialPattern(Base):
extension = relationship('Extension',
primaryjoin="""and_(Extension.type == 'outcall',
Extension.typeval == cast(DialPattern.id, String))""",
foreign_keys='[Extension.typeval]',
foreign_keys='Extension.typeval',
uselist=False,
cascade='all, delete-orphan')
passive_deletes='all')
@hybrid_property
def external_prefix(self):
......@@ -55,20 +54,6 @@ class DialPattern(Base):
def external_prefix(self, value):
self.externprefix = value
@hybrid_property
def pattern(self):
return self.exten
@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
......
......@@ -15,19 +15,17 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
import itertools
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.ext.orderinglist import ordering_list
from sqlalchemy.orm import relationship
from sqlalchemy.schema import Column, PrimaryKeyConstraint, UniqueConstraint
from sqlalchemy.sql import func, cast, not_
from xivo_dao.alchemy.dialpattern import DialPattern
from sqlalchemy.types import Integer, String, Text, Boolean
from xivo_dao.helpers.db_manager import Base
from xivo_dao.alchemy.outcalltrunk import OutcallTrunk
from xivo_dao.alchemy.dialpattern import DialPattern
class Outcall(Base):
......@@ -54,6 +52,10 @@ class Outcall(Base):
foreign_keys='DialPattern.typeid',
cascade='all, delete-orphan')
extensions = association_proxy('dialpatterns', 'extension',
creator=lambda _extension: DialPattern(type='outcall',
exten=_extension.exten,
extension=_extension))
outcall_trunks = relationship('OutcallTrunk',
order_by='OutcallTrunk.priority',
collection_class=ordering_list('priority'),
......@@ -104,31 +106,23 @@ class Outcall(Base):
def enabled(self, value):
self.commented = int(value == 0)
@property
def patterns(self):
return self.dialpatterns
@patterns.setter
def patterns(self, patterns):
old_dialpatterns = self.dialpatterns if self.dialpatterns else []
self.dialpatterns = []
for pattern, dialpattern in itertools.izip_longest(patterns, old_dialpatterns):
if not pattern:
continue
if not dialpattern:
dialpattern = pattern
dialpattern.type = 'outcall'
dialpattern.external_prefix = pattern.external_prefix
dialpattern.prefix = pattern.prefix
dialpattern.pattern = pattern.pattern
dialpattern.strip_digits = pattern.strip_digits
dialpattern.caller_id = pattern.caller_id
self.dialpatterns.append(dialpattern)
def fix_context(self):
for pattern in self.dialpatterns:
if pattern.extension is None:
continue
pattern.extension.context = self.context
def associate_extension(self, extension, **kwargs):
extension.type = 'outcall'
dialpattern = DialPattern(type='outcall',
exten=extension.exten,
extension=extension,
**kwargs)
self.dialpatterns.append(dialpattern)
self._fix_context()
def dissociate_extension(self, extension):
self.extensions.remove(extension)
extension.type = 'user'
extension.typeval = '0'
self._fix_context()
def _fix_context(self):
for extension in self.extensions:
self.context = extension.context
return
self.context = None
......@@ -56,13 +56,11 @@ 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):
......
......@@ -23,7 +23,6 @@ from xivo_dao.resources.utils.search import (SearchSystem,
config = SearchConfig(table=Outcall,
columns={'id': Outcall.id,
'context': Outcall.context,
'description': Outcall.description,
'name': Outcall.name,
'preprocess_subroutine': Outcall.preprocess_subroutine},
......
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