Skip to content

Commit ed3d030

Browse files
committed
outcalls: add label and autogenerate name
1 parent 2d6f35f commit ed3d030

7 files changed

Lines changed: 65 additions & 32 deletions

File tree

integration_tests/suite/base/test_outcalls.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2016-2022 The Wazo Authors (see the AUTHORS file)
1+
# Copyright 2016-2023 The Wazo Authors (see the AUTHORS file)
22
# SPDX-License-Identifier: GPL-3.0-or-later
33

44
from hamcrest import (
@@ -53,6 +53,12 @@ def error_checks(url):
5353
yield s.check_bogus_field_returns_error, url, 'name', 1234
5454
yield s.check_bogus_field_returns_error, url, 'name', []
5555
yield s.check_bogus_field_returns_error, url, 'name', {}
56+
yield s.check_bogus_field_returns_error, url, 'name', 'name'
57+
yield s.check_bogus_field_returns_error, url, 'label', 123
58+
yield s.check_bogus_field_returns_error, url, 'label', None
59+
yield s.check_bogus_field_returns_error, url, 'label', True
60+
yield s.check_bogus_field_returns_error, url, 'label', {}
61+
yield s.check_bogus_field_returns_error, url, 'label', []
5662
yield s.check_bogus_field_returns_error, url, 'internal_caller_id', 1234
5763
yield s.check_bogus_field_returns_error, url, 'internal_caller_id', 'invalid'
5864
yield s.check_bogus_field_returns_error, url, 'internal_caller_id', None
@@ -69,20 +75,12 @@ def error_checks(url):
6975
yield s.check_bogus_field_returns_error, url, 'enabled', []
7076
yield s.check_bogus_field_returns_error, url, 'enabled', {}
7177

72-
for check in unique_error_checks(url):
73-
yield check
74-
75-
76-
@fixtures.outcall(name='unique')
77-
def unique_error_checks(url, outcall):
78-
yield s.check_bogus_field_returns_error, url, 'name', outcall['name']
79-
8078

81-
@fixtures.outcall(description='search')
79+
@fixtures.outcall(label='search', description='search')
8280
@fixtures.outcall(description='hidden')
8381
def test_search(outcall, hidden):
8482
url = confd.outcalls
85-
searches = {'description': 'search'}
83+
searches = {'label': 'search', 'description': 'search'}
8684

8785
for field, term in searches.items():
8886
yield check_search, url, outcall, hidden, field, term

integration_tests/suite/helpers/helpers/outcall.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2016-2021 The Wazo Authors (see the AUTHORS file)
1+
# Copyright 2016-2023 The Wazo Authors (see the AUTHORS file)
22
# SPDX-License-Identifier: GPL-3.0-or-later
33

44
import random
@@ -9,7 +9,7 @@
99

1010

1111
def generate_outcall(**parameters):
12-
parameters.setdefault('name', generate_name())
12+
parameters.setdefault('label', generate_label())
1313
parameters.setdefault('context', CONTEXT)
1414
return add_outcall(**parameters)
1515

@@ -25,14 +25,14 @@ def delete_outcall(outcall_id, check=False, **params):
2525
response.assert_ok()
2626

2727

28-
def generate_name():
28+
def generate_label():
2929
response = confd.outcalls.get()
30-
names = set(d['name'] for d in response.items)
31-
return _random_name(names)
30+
labels = set(d['label'] for d in response.items)
31+
return _random_label(labels)
3232

3333

34-
def _random_name(names):
35-
name = ''.join(random.choice(string.ascii_letters) for _ in range(10))
36-
if name in names:
37-
return _random_name(names)
38-
return name
34+
def _random_label(labels):
35+
label = ''.join(random.choice(string.ascii_lowercase) for i in range(10))
36+
while label in labels:
37+
label = ''.join(random.choice(string.ascii_lowercase) for i in range(10))
38+
return label

wazo_confd/plugins/outcall/api.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ definitions:
133133
- $ref: '#/definitions/OutcallRelationSchedules'
134134
- $ref: '#/definitions/OutcallRelationCallPermissions'
135135
- required:
136-
- name
136+
- label
137137
OutcallRelationBase:
138138
properties:
139139
id:
@@ -143,6 +143,10 @@ definitions:
143143
name:
144144
type: string
145145
description: The name of the outcall
146+
readOnly: true
147+
label:
148+
type: string
149+
description: The label of the outcall
146150
OutcallRelationTrunks:
147151
properties:
148152
trunks:

wazo_confd/plugins/outcall/plugin.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
# Copyright 2016-2019 The Wazo Authors (see the AUTHORS file)
1+
# Copyright 2016-2023 The Wazo Authors (see the AUTHORS file)
22
# SPDX-License-Identifier: GPL-3.0-or-later
33

4+
from xivo_dao.resources.tenant import dao as tenant_dao
5+
46
from .resource import OutcallItem, OutcallList
57
from .service import build_service
68

@@ -10,7 +12,7 @@ def load(self, dependencies):
1012
api = dependencies['api']
1113
service = build_service()
1214

13-
api.add_resource(OutcallList, '/outcalls', resource_class_args=(service,))
15+
api.add_resource(OutcallList, '/outcalls', resource_class_args=(tenant_dao, service))
1416

1517
api.add_resource(
1618
OutcallItem,

wazo_confd/plugins/outcall/resource.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright 2016-2023 The Wazo Authors (see the AUTHORS file)
22
# SPDX-License-Identifier: GPL-3.0-or-later
33

4-
from flask import url_for
4+
from flask import request, url_for
55

66
from xivo_dao.alchemy.outcall import Outcall
77

@@ -14,13 +14,28 @@
1414
class OutcallList(ListResource):
1515
model = Outcall
1616
schema = OutcallSchema
17+
outcall_name_fmt = 'outcall-{tenant_slug}-{outcall_id}'
18+
19+
def __init__(self, tenant_dao, *args, **kwargs):
20+
super().__init__(*args, **kwargs)
21+
self._tenant_dao = tenant_dao
1722

1823
def build_headers(self, outcall):
1924
return {'Location': url_for('outcalls', id=outcall.id, _external=True)}
2025

2126
@required_acl('confd.outcalls.create')
2227
def post(self):
23-
return super().post()
28+
form = self.schema().load(request.get_json())
29+
form = self.add_tenant_to_form(form)
30+
31+
tenant = self._tenant_dao.get(form['tenant_uuid'])
32+
form['name'] = self.outcall_name_fmt.format(
33+
tenant_slug=tenant.slug,
34+
outcall_id=form['id'],
35+
)
36+
model = self.model(**form)
37+
model = self.service.create(model)
38+
return self.schema().dump(model), 201, self.build_headers(model)
2439

2540
@required_acl('confd.outcalls.read')
2641
def get(self):

wazo_confd/plugins/outcall/schema.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
# Copyright 2016-2022 The Wazo Authors (see the AUTHORS file)
1+
# Copyright 2016-2023 The Wazo Authors (see the AUTHORS file)
22
# SPDX-License-Identifier: GPL-3.0-or-later
33

4+
import logging
45

5-
from marshmallow import fields, post_dump
6+
from marshmallow import fields, post_dump, pre_load
67
from marshmallow.validate import Length, Range
78

89
from wazo_confd.helpers.mallow import BaseSchema, StrictBoolean, Link, ListLink, Nested
910

11+
logger = logging.getLogger(__name__)
12+
1013

1114
class OutcallSchema(BaseSchema):
1215
id = fields.Integer(dump_only=True)
1316
tenant_uuid = fields.String(dump_only=True)
14-
name = fields.String(validate=Length(max=128), required=True)
17+
name = fields.String(dump_only=True)
18+
label = fields.String(validate=Length(max=128), required=True)
1519
internal_caller_id = StrictBoolean()
1620
preprocess_subroutine = fields.String(validate=Length(max=39), allow_none=True)
1721
ring_time = fields.Integer(validate=Range(min=0), allow_none=True)
@@ -40,6 +44,18 @@ class OutcallSchema(BaseSchema):
4044
dump_only=True,
4145
)
4246

47+
# DEPRECATED 23.10
48+
@pre_load
49+
def copy_name_to_label(self, data, **kwargs):
50+
if 'label' in data:
51+
return data
52+
if 'name' in data:
53+
logger.warning(
54+
'The "name" field of outcall is deprecated. Use "label" instead'
55+
)
56+
data['label'] = data['name']
57+
return data
58+
4359

4460
class DialPatternSchema(BaseSchema):
4561
external_prefix = fields.String()
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
# Copyright 2016-2020 The Wazo Authors (see the AUTHORS file)
1+
# Copyright 2016-2023 The Wazo Authors (see the AUTHORS file)
22
# SPDX-License-Identifier: GPL-3.0-or-later
33

44
from xivo_dao.resources.outcall import dao as outcall_dao
55

66
from wazo_confd.helpers.validator import (
77
UniqueField,
8-
UniqueFieldChanged,
98
ValidationGroup,
109
)
1110

@@ -15,5 +14,4 @@ def build_validator():
1514
create=[
1615
UniqueField('name', lambda name: outcall_dao.find_by(name=name), 'Outcall')
1716
],
18-
edit=[UniqueFieldChanged('name', outcall_dao.find_by, 'Outcall')],
1917
)

0 commit comments

Comments
 (0)