Fix handling of temp extension data

This commit is contained in:
clerie 2023-06-18 16:04:21 +02:00
parent ca9c82dd59
commit 6825ba8156
3 changed files with 45 additions and 47 deletions

View File

@ -5,6 +5,8 @@ import mitel_ommclient2
import time import time
import hashlib import hashlib
from .extensions import ExtensionConfig
logger = logging.getLogger("fieldpoc.dect") logger = logging.getLogger("fieldpoc.dect")
class Dect: class Dect:
@ -29,12 +31,13 @@ class Dect:
used_temp_extensions = self.c.find_users(lambda u: u.num.startswith(self.temp_num_prefix)) used_temp_extensions = self.c.find_users(lambda u: u.num.startswith(self.temp_num_prefix))
for u in used_temp_extensions: for u in used_temp_extensions:
temp_num = u.num temp_num = u.num
self.fp.temp_extensions[temp_num] = { self.fp.temp_extensions[temp_num] = ExtensionConfig(
"name": f"Temp {temp_num[4:]}", num=temp_num,
"type": "dect", name=f"Temp {temp_num[4:]}",
"trunk": False, type="dect",
"dialout_allowed": False, trunk=False,
} dialout_allowed=False,
)
def get_temp_number(self): def get_temp_number(self):
current_temp_extension = 0 current_temp_extension = 0
@ -145,12 +148,13 @@ class Dect:
logger.debug(f'Creating and binding tmp-user for {d}: {temp_num}') logger.debug(f'Creating and binding tmp-user for {d}: {temp_num}')
user = self.create_and_bind_user(d, temp_num) user = self.create_and_bind_user(d, temp_num)
self.c.set_user_name(user.uid, f"Temp {temp_num[4:]}") self.c.set_user_name(user.uid, f"Temp {temp_num[4:]}")
self.fp.temp_extensions[temp_num] = { self.fp.temp_extensions[temp_num] = ExtensionConfig(
"name": f"Temp {temp_num[4:]}", num=temp_num,
"type": "dect", name=f"Temp {temp_num[4:]}",
"trunk": False, type="dect",
"dialout_allowed": False, trunk=False,
} dialout_allowed=False,
)
created_tmp_ext = True created_tmp_ext = True
# update rounting when new extensions got created # update rounting when new extensions got created

View File

@ -24,7 +24,7 @@ class ExtensionConfig:
if self.type is None: if self.type is None:
raise Exception("Type field for extension {} is required".format(self.num)) raise Exception("Type field for extension {} is required".format(self.num))
elif self.type == "dect": elif self.type == "dect":
if self.dect_ipei is None and self.dect_claim_token is None: if self.dect_ipei is not None and self.dect_claim_token is not None:
raise Exception("dect_ipei can't be used together with dect_claim_token in extension {}".format(self.num)) raise Exception("dect_ipei can't be used together with dect_claim_token in extension {}".format(self.num))
elif self.type == "sip": elif self.type == "sip":
if self.sip_password is None: if self.sip_password is None:

View File

@ -304,7 +304,7 @@ class YwsdForkRankMemberModel(ForkRankMemberModel):
return super().delete() return super().delete()
class BackendNerd(diffsync.DiffSync): class BackendFieldPOC(diffsync.DiffSync):
yate = YateModel yate = YateModel
extension = ExtensionModel extension = ExtensionModel
user = UserModel user = UserModel
@ -317,7 +317,7 @@ class BackendNerd(diffsync.DiffSync):
self.fp = fp self.fp = fp
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
def load(self, data): def load(self, extensions):
#yate_dect = self.yate( #yate_dect = self.yate(
# guru3_identifier="dect", hostname="dect", voip_listener="local" # guru3_identifier="dect", hostname="dect", voip_listener="local"
#) #)
@ -327,18 +327,18 @@ class BackendNerd(diffsync.DiffSync):
) )
self.add(yate_sip) self.add(yate_sip)
for key, value in data["extensions"].items(): for num, extension in extensions.items():
yate = None yate = None
if value["type"] in ["dect", "static"]: if extension.type in ["dect", "static"]:
# yate = yate_dect.guru3_identifier # yate = yate_dect.guru3_identifier
yate = yate_sip.guru3_identifier yate = yate_sip.guru3_identifier
elif value["type"] in ["sip"]: elif extension.type in ["sip"]:
yate = yate_sip.guru3_identifier yate = yate_sip.guru3_identifier
elif value["type"] in ["callgroup"]: elif extension.type in ["callgroup"]:
forkrank = self.forkrank(extension=key, index=0, mode="DEFAULT") forkrank = self.forkrank(extension=num, index=0, mode="DEFAULT")
self.add(forkrank) self.add(forkrank)
for member in value["callgroup_members"]: for member in extension.callgroup_members:
frm = self.forkrankmember( frm = self.forkrankmember(
forkrank=forkrank.get_unique_id(), forkrank=forkrank.get_unique_id(),
extension=member, extension=member,
@ -346,36 +346,30 @@ class BackendNerd(diffsync.DiffSync):
active=True, active=True,
) )
self.add(frm) self.add(frm)
elif value["type"] in ["temp"]: elif extension.type in ["temp"]:
continue continue
extension = self.extension( self.add(self.extension(
extension=key, extension=num,
**dict( name=extension.name,
(k, v) outgoing_extension=extension.outgoing_extension,
for k, v in value.items() dialout_allowed=extension.dialout_allowed,
if k in ["name", "outgoing_extension", "dialout_allowed"] extension_type=("SIMPLE" if extension.trunk is not None else "TRUNK") if not extension.type == "callgroup" else "GROUP",
),
extension_type=("SIMPLE" if not value["trunk"] else "TRUNK")
if not value["type"] == "callgroup"
else "GROUP",
forwarding_mode="DISABLED", forwarding_mode="DISABLED",
lang="en-GB", lang="en-GB",
yate=yate, yate=yate,
) ))
self.add(extension)
if value["type"] in ["sip", "static", "dect"]: if extension.type in ["sip", "static", "dect"]:
user_type = {"sip": "user", "dect": "user", "static": "static"} user_type = {"sip": "user", "dect": "user", "static": "static"}
user = self.user( self.add(self.user(
username=key, username=num,
displayname=value["name"], displayname=extension.name,
password=value.get("sip_password", self.fp._dect.get_sip_password_for_number(key)), password=extension.sip_password if extension.sip_password is not None else self.fp._dect.get_sip_password_for_number(num),
user_type=user_type[value["type"]], user_type=user_type[extension.type],
trunk=value["trunk"], trunk=extension.trunk,
static_target=value.get("static_target", ""), static_target=extension.static_target if extension.static_target is not None else "",
) ))
self.add(user)
class BackendYwsd(diffsync.DiffSync): class BackendYwsd(diffsync.DiffSync):
@ -491,9 +485,9 @@ class Routing:
logger.info("syncing") logger.info("syncing")
state_fieldpoc = BackendNerd(self.fp) state_fieldpoc = BackendFieldPOC(self.fp)
extensions = self.fp.extensions._c.copy() extensions = self.fp.extensions.extensions_by_num.copy()
extensions['extensions'].update(self.fp.temp_extensions) extensions.update(self.fp.temp_extensions)
state_fieldpoc.load(extensions) state_fieldpoc.load(extensions)
state_yate = BackendYwsd() state_yate = BackendYwsd()
state_yate.load("postgresql+psycopg2://{}:{}@{}/{}".format( state_yate.load("postgresql+psycopg2://{}:{}@{}/{}".format(