Skip to content

Commit 2e24377

Browse files
committed
[req-changes] Flagged strings for translation and code formatting
1 parent a37b0dc commit 2e24377

3 files changed

Lines changed: 75 additions & 64 deletions

File tree

openwisp_controller/config/base/config.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -187,24 +187,25 @@ def _get_templates_from_pk_set(cls, pk_set):
187187
return templates
188188

189189
@classmethod
190-
def validate_duplicate_vpn_templates(
190+
def clean_duplicate_vpn_client_templates(
191191
cls, action, instance, templates, raw_data=None
192192
):
193193
"""
194-
Validates if there are duplicate templates for the same VPN server.
194+
Multiple VPN client templates related to the same VPN server are not allowed:
195+
it hardly makes sense, preventing it keeps things simple and avoids headaches.
195196
Raises a ValidationError if duplicates are found.
196197
"""
197-
if action != 'pre_add':
198+
if action != "pre_add":
198199
return
199200

200201
def format_template_list(names):
201202
quoted = [f'"{name}"' for name in names]
202203
if len(quoted) == 2:
203-
return ' and '.join(quoted)
204-
return ', '.join(quoted[:-1]) + ' and ' + quoted[-1]
204+
return " and ".join(quoted)
205+
return ", ".join(quoted[:-1]) + " and " + quoted[-1]
205206

206207
def add_vpn_templates(templates_queryset):
207-
for template in templates_queryset.filter(type='vpn'):
208+
for template in templates_queryset.filter(type="vpn"):
208209
if template.name not in vpn_templates[template.vpn.name]:
209210
vpn_templates[template.vpn.name].append(template.name)
210211

@@ -221,19 +222,23 @@ def add_vpn_templates(templates_queryset):
221222
add_vpn_templates(templates)
222223

223224
error_lines = [
224-
'You cannot select multiple VPN client templates related to'
225-
' the same VPN server.'
225+
_(
226+
"You cannot select multiple VPN client templates related to"
227+
" the same VPN server."
228+
)
226229
]
227230
for vpn_name, template_names in vpn_templates.items():
228231
if len(template_names) < 2:
229232
continue
230233
template_list = format_template_list(sorted(template_names))
231234
error_lines.append(
232-
f'The templates {template_list} are all linked'
233-
f' to the same VPN server: "{vpn_name}".'
235+
_(
236+
"The templates {template_list} are all linked"
237+
' to the same VPN server: "{vpn_name}".'
238+
).format(template_list=template_list, vpn_name=vpn_name)
234239
)
235240
if len(error_lines) > 1:
236-
raise ValidationError('\n'.join(error_lines))
241+
raise ValidationError("\n".join(str(line) for line in error_lines))
237242

238243
@classmethod
239244
def clean_templates(cls, action, instance, pk_set, raw_data=None, **kwargs):
@@ -253,7 +258,7 @@ def clean_templates(cls, action, instance, pk_set, raw_data=None, **kwargs):
253258
)
254259
if not templates:
255260
return
256-
cls.validate_duplicate_vpn_templates(
261+
cls.clean_duplicate_vpn_client_templates(
257262
action, instance, templates, raw_data=raw_data
258263
)
259264
backend = instance.get_backend_instance(template_instances=templates)

openwisp_controller/config/tests/test_api.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,70 +1553,70 @@ def test_device_detail_api_change_config(self):
15531553

15541554
def test_multiple_vpn_templates_same_vpn(self):
15551555
"""
1556-
Ensure that assigning multiple templates of type 'vpn' referencing the same VPN
1557-
to a device's config does not create duplicate VpnClient objects.
1556+
Assigning multiple templates of type 'vpn' referencing the same VPN
1557+
to a device's config raises error.
15581558
"""
15591559
org = self._get_org()
15601560
vpn = self._create_vpn(organization=org)
15611561
# Create two templates of type 'vpn' referencing the same VPN
15621562
vpn_template1 = self._create_template(
1563-
type='vpn', vpn=vpn, organization=org, name='VPN Client 1'
1563+
type="vpn", vpn=vpn, organization=org, name="VPN Client 1"
15641564
)
15651565
vpn_template2 = self._create_template(
1566-
type='vpn', vpn=vpn, organization=org, name='VPN Client 2'
1566+
type="vpn", vpn=vpn, organization=org, name="VPN Client 2"
15671567
)
15681568
device = self._create_device(organization=org)
15691569
config = self._create_config(device=device)
1570-
path = reverse('config_api:device_detail', args=[device.pk])
1570+
path = reverse("config_api:device_detail", args=[device.pk])
15711571
data = {
1572-
'name': device.name,
1573-
'organization': str(org.id),
1574-
'mac_address': device.mac_address,
1575-
'config': {
1576-
'backend': 'netjsonconfig.OpenWrt',
1577-
'context': {'lan_ip': '192.168.1.1'},
1578-
'config': {'interfaces': [{'name': 'wlan0', 'type': 'wireless'}]},
1572+
"name": device.name,
1573+
"organization": str(org.id),
1574+
"mac_address": device.mac_address,
1575+
"config": {
1576+
"backend": "netjsonconfig.OpenWrt",
1577+
"context": {"lan_ip": "192.168.1.1"},
1578+
"config": {"interfaces": [{"name": "wlan0", "type": "wireless"}]},
15791579
},
15801580
}
15811581
expected_error_message = (
1582-
'You cannot select multiple VPN client templates related'
1583-
' to the same VPN server.\n'
1582+
"You cannot select multiple VPN client templates related"
1583+
" to the same VPN server.\n"
15841584
'The templates "VPN Client 1" and "VPN Client 2" are all '
15851585
'linked to the same VPN server: "test".'
15861586
)
15871587

1588-
with self.subTest('Add both templates at once'):
1589-
data['config']['templates'] = [str(vpn_template1.pk), str(vpn_template2.pk)]
1590-
response = self.client.put(path, data, content_type='application/json')
1588+
with self.subTest("Add both templates at once"):
1589+
data["config"]["templates"] = [str(vpn_template1.pk), str(vpn_template2.pk)]
1590+
response = self.client.put(path, data, content_type="application/json")
15911591
self.assertEqual(response.status_code, 400)
15921592
self.assertEqual(
1593-
str(response.data['config'][0]),
1593+
str(response.data["config"][0]),
15941594
expected_error_message,
15951595
)
15961596
self.assertEqual(config.templates.count(), 0)
15971597
self.assertEqual(config.vpnclient_set.count(), 0)
15981598

1599-
with self.subTest('Add one template at a time'):
1600-
data['config']['templates'] = [str(vpn_template1.pk)]
1601-
response = self.client.put(path, data, content_type='application/json')
1599+
with self.subTest("Add one template at a time"):
1600+
data["config"]["templates"] = [str(vpn_template1.pk)]
1601+
response = self.client.put(path, data, content_type="application/json")
16021602
self.assertEqual(response.status_code, 200)
16031603
self.assertEqual(config.templates.count(), 1)
16041604
self.assertEqual(config.vpnclient_set.count(), 1)
16051605

16061606
# Now add the second template
1607-
data['config']['templates'] = [str(vpn_template1.pk), str(vpn_template2.pk)]
1608-
response = self.client.put(path, data, content_type='application/json')
1607+
data["config"]["templates"] = [str(vpn_template1.pk), str(vpn_template2.pk)]
1608+
response = self.client.put(path, data, content_type="application/json")
16091609
self.assertEqual(response.status_code, 400)
16101610
self.assertEqual(
1611-
str(response.data['config'][0]),
1611+
str(response.data["config"][0]),
16121612
expected_error_message,
16131613
)
16141614
self.assertEqual(config.templates.filter(id=vpn_template1.id).count(), 1)
16151615
self.assertEqual(config.vpnclient_set.count(), 1)
16161616

1617-
with self.subTest('Change existing template with another'):
1618-
data['config']['templates'] = [str(vpn_template2.pk)]
1619-
response = self.client.put(path, data, content_type='application/json')
1617+
with self.subTest("Change existing template with another"):
1618+
data["config"]["templates"] = [str(vpn_template2.pk)]
1619+
response = self.client.put(path, data, content_type="application/json")
16201620
self.assertEqual(response.status_code, 200)
16211621
self.assertEqual(config.templates.filter(id=vpn_template2.id).count(), 1)
16221622
self.assertEqual(config.vpnclient_set.count(), 1)

openwisp_controller/config/tests/test_config.py

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -893,53 +893,59 @@ class TestTransactionConfig(
893893
TransactionTestCase,
894894
):
895895
def test_multiple_vpn_templates_same_vpn(self):
896-
vpn1 = self._create_vpn(name='vpn1')
897-
vpn2 = self._create_vpn(name='vpn2')
896+
vpn1 = self._create_vpn(name="vpn1")
897+
vpn2 = self._create_vpn(name="vpn2")
898898
vpn1_template1 = self._create_template(
899-
name='vpn1-template1', type='vpn', vpn=vpn1
899+
name="vpn1-template1", type="vpn", vpn=vpn1
900900
)
901901
vpn1_template2 = self._create_template(
902-
name='vpn1-template2', type='vpn', vpn=vpn1
902+
name="vpn1-template2", type="vpn", vpn=vpn1
903903
)
904904
vpn2_template1 = self._create_template(
905-
name='vpn2-template1', type='vpn', vpn=vpn2
905+
name="vpn2-template1", type="vpn", vpn=vpn2
906906
)
907907
vpn2_template2 = self._create_template(
908-
name='vpn2-template2', type='vpn', vpn=vpn2
908+
name="vpn2-template2", type="vpn", vpn=vpn2
909909
)
910910
vpn2_template3 = self._create_template(
911-
name='vpn2-template3', type='vpn', vpn=vpn2
911+
name="vpn2-template3", type="vpn", vpn=vpn2
912912
)
913913
config = self._create_config(device=self._create_device())
914914
config.templates.add(vpn1_template1)
915-
with self.subTest('Adding template one by one'):
915+
with self.subTest("Adding duplicate vpn-client template one at time"):
916916
with self.assertRaises(ValidationError) as context_manager:
917917
config.templates.add(vpn1_template2)
918-
self.assertEqual(
919-
context_manager.exception.message,
920-
'You cannot select multiple VPN client templates related to the'
921-
' same VPN server.\n'
922-
'The templates "vpn1-template1" and "vpn1-template2" are all'
923-
' linked to the same VPN server: "vpn1".',
924-
)
918+
try:
919+
self.assertEqual(
920+
context_manager.exception.message,
921+
"You cannot select multiple VPN client templates related to the"
922+
" same VPN server.\n"
923+
'The templates "vpn1-template1" and "vpn1-template2" are all'
924+
' linked to the same VPN server: "vpn1".',
925+
)
926+
except AssertionError:
927+
self.fail("ValidationError not raised")
925928

926-
with self.subTest('Add duplicate templates for multiple VPN'):
929+
with self.subTest("Add duplicate templates for multiple VPN"):
927930
config.refresh_from_db()
928931
self.assertEqual(config.templates.count(), 1)
929932
self.assertEqual(config.vpnclient_set.count(), 1)
930933
with self.assertRaises(ValidationError) as context_manager:
931934
config.templates.add(
932935
vpn1_template2, vpn2_template1, vpn2_template2, vpn2_template3
933936
)
934-
self.assertEqual(
935-
context_manager.exception.message,
936-
'You cannot select multiple VPN client templates related to the'
937-
' same VPN server.\n'
938-
'The templates "vpn1-template1" and "vpn1-template2" are all'
939-
' linked to the same VPN server: "vpn1".\n'
940-
'The templates "vpn2-template1", "vpn2-template2" and "vpn2-template3"'
941-
' are all linked to the same VPN server: "vpn2".',
942-
)
937+
try:
938+
self.assertEqual(
939+
context_manager.exception.message,
940+
"You cannot select multiple VPN client templates related to the"
941+
" same VPN server.\n"
942+
'The templates "vpn1-template1" and "vpn1-template2" are all'
943+
' linked to the same VPN server: "vpn1".\n'
944+
'The templates "vpn2-template1", "vpn2-template2" and'
945+
' "vpn2-template3" are all linked to the same VPN server: "vpn2".',
946+
)
947+
except AssertionError:
948+
self.fail("ValidationError not raised")
943949

944950
def test_certificate_renew_invalidates_checksum_cache(self):
945951
config = self._create_config(organization=self._get_org())

0 commit comments

Comments
 (0)