|
19 | 19 |
|
20 | 20 | @app.route('/connect/', methods=['POST']) |
21 | 21 | def connect(): |
22 | | - |
23 | | - print('obtaining lock') |
24 | 22 | with lock: |
25 | | - print('lock obtained') |
26 | | - |
27 | 23 | req_json = request.get_json() |
28 | 24 |
|
29 | 25 | user_extension = requests.get(f"http://{config.dect_wip_internal_ip}:{config.dect_wip_port}/api/v1/GetUserExtensionByToken/{req_json['token']}").json() |
30 | 26 |
|
31 | 27 | extension = user_extension['extension'] |
32 | 28 | password = user_extension['password'] |
33 | 29 | displayname = user_extension['name'] |
34 | | - |
| 30 | + |
| 31 | + # TODO get temp_extension from omm |
35 | 32 | temp_extenison = requests.get(f"http://{config.dect_wip_internal_ip}:{config.dect_wip_port}/api/v1/GetTempExtensionByCallerid/{req_json['callerid']}").json() |
36 | | - |
37 | | - ppn = temp_extenison['ppn'] |
38 | | - uid = temp_extenison['uid'] |
39 | | - |
40 | | - print(ppn) |
41 | | - print(uid) |
42 | | - |
43 | | - client.detach_user_device(uid,ppn) |
44 | | - |
45 | | - newuser = client.create_user(extension) |
46 | | - client.set_user_sipauth(newuser.uid, extension, password) |
47 | | - client.set_user_name(newuser.uid, displayname) |
48 | | - |
49 | | - print(client.attach_user_device(int(newuser.uid), ppn)) |
50 | | - # no implemented in current ommclient ... |
51 | | - #client.delete_user(uid) |
52 | | - |
| 33 | + |
| 34 | + # deletes existing users with the same extension |
| 35 | + # this also detaches the (dynamically linked) device |
| 36 | + for user in client.find_users(lambda user: user.num == extension): |
| 37 | + print(f"deleting uid {user.uid} (duplicate of extension {extension})") |
| 38 | + client.delete_user(user.uid) |
| 39 | + |
| 40 | + new_user = client.create_user(extension) |
| 41 | + client.set_user_sipauth(new_user.uid, extension, password) |
| 42 | + client.set_user_name(new_user.uid, displayname) |
| 43 | + |
| 44 | + print(f"detaching ppn {temp_extenison['ppn']} from uid {temp_extenison['uid']}") |
| 45 | + client.detach_user_device(temp_extenison['uid'], temp_extenison['ppn']) |
| 46 | + |
| 47 | + print(f"deleting uid {temp_extenison['uid']}") |
| 48 | + client.delete_user(temp_extenison['uid']) |
| 49 | + |
| 50 | + print(f"attaching ppn {temp_extenison['ppn']} to uid {new_user.uid}, extension {extension}") |
| 51 | + client.attach_user_device(int(new_user.uid), temp_extenison['ppn']) |
| 52 | + |
53 | 53 | response = make_response(jsonify( {"message": "extension added"}), 200) |
54 | 54 | return response |
55 | 55 |
|
56 | 56 | @scheduler.task('interval', id='makeTemps', seconds=5, next_run_time=datetime.now(), max_instances=1) |
57 | 57 | def makeTemps(): |
58 | | - print("lets go") |
59 | | - |
60 | | - print('obtaining lock') |
61 | 58 | with lock: |
62 | | - print('lock obtained') |
63 | | - |
64 | 59 | for device in list(client.find_devices(lambda d: d.relType == mitel_ommclient2.types.PPRelTypeType("Unbound"))): |
65 | 60 | extension = 't_' + namegenerator.generate_name() + utilities.getRandomNumber(4) |
66 | 61 | password = utilities.getRandomStr(20) |
67 | | - |
| 62 | + |
68 | 63 | newuser = client.create_user(extension) |
69 | 64 | client.set_user_sipauth(newuser.uid, extension, password) |
70 | 65 | client.set_user_name(newuser.uid, extension[2:21]) |
71 | 66 | client.attach_user_device(int(newuser.uid), int(device.ppn)) |
72 | | - |
| 67 | + |
73 | 68 | data = { |
74 | 69 | "extension": extension, |
75 | 70 | "password": password, |
76 | 71 | "uid": newuser.uid, |
77 | 72 | "ppn": device.ppn |
78 | 73 | } |
79 | 74 |
|
80 | | - print(data) |
81 | | - |
| 75 | + print("new temp extension created", data) |
| 76 | + |
82 | 77 | response = requests.post(f"http://{config.dect_wip_internal_ip}:{config.dect_wip_port}/api/v1/AddTempExtensionToDB", json=data) |
83 | | - print(response.text) |
| 78 | + if response.status_code != 200: |
| 79 | + raise RuntimeError(f"adding temp extension to db failed: {response.status_code} {response.text}") |
84 | 80 |
|
85 | 81 | def init(config_path): |
86 | 82 |
|
|
0 commit comments