diff --git a/appsettings/tests/test_appsettings.py b/appsettings/tests/test_appsettings.py index ba277032..c2006e48 100644 --- a/appsettings/tests/test_appsettings.py +++ b/appsettings/tests/test_appsettings.py @@ -73,6 +73,8 @@ class AppSettingsTestCase(TestCase): def post(self, key, value): response = self.client.post(reverse("appsettings"), {key: value}) + # not followed: the messages would stay in the cookie for the next post + self.client.cookies.pop("messages", None) return [str(m) for m in get_messages(response.wsgi_request)] def test_a_listed_setting_takes_only_its_choices(self): @@ -99,6 +101,30 @@ class AppSettingsTestCase(TestCase): page = self.client.get(reverse("appsettings")).content.decode() self.assertIn('name="CLONE_INSTANCE_DEFAULT_PREFIX" value="web-"', page) + def test_the_clone_name_prefix_makes_clone_names(self): + # every name offered to a user who is not a superuser would be refused + for bad in ("bad prefix", "web_"): + with self.subTest(bad=bad): + self.assertTrue(any("letters, digits and '-' only" in m for m in self.post("CLONE_INSTANCE_DEFAULT_PREFIX", bad))) + self.assertEqual(AppSettings.objects.get(key="CLONE_INSTANCE_DEFAULT_PREFIX").value, "instance") + + def test_native_disk_io_needs_a_cache_libvirt_takes_with_it(self): + # libvirt: "io='native' needs either no disk cache or directsync cache mode" + self.post("INSTANCE_VOLUME_DEFAULT_CACHE", "writeback") + self.assertTrue(any("needs disk cache none or directsync" in m + for m in self.post("INSTANCE_VOLUME_DEFAULT_IO", "native"))) + self.assertEqual(AppSettings.objects.get(key="INSTANCE_VOLUME_DEFAULT_IO").value, "default") + response = self.client.post(reverse("appsettings"), {"INSTANCE_VOLUME_DEFAULT_CACHE": "none", + "INSTANCE_VOLUME_DEFAULT_IO": "native"}) + self.assertEqual(response.status_code, 302) + self.assertEqual(AppSettings.objects.get(key="INSTANCE_VOLUME_DEFAULT_IO").value, "native") + self.assertTrue(any("needs disk cache" in m for m in self.post("INSTANCE_VOLUME_DEFAULT_CACHE", "unsafe"))) + self.assertEqual(AppSettings.objects.get(key="INSTANCE_VOLUME_DEFAULT_CACHE").value, "none") + # a pair saved before does not block another setting + AppSettings.objects.filter(key="INSTANCE_VOLUME_DEFAULT_CACHE").update(value="writeback") + self.post("LOGS_PER_PAGE", "50") + self.assertEqual(AppSettings.objects.get(key="LOGS_PER_PAGE").value, "50") + def page(self): from lxml import html diff --git a/appsettings/views.py b/appsettings/views.py index ebfa215f..ba253418 100644 --- a/appsettings/views.py +++ b/appsettings/views.py @@ -1,3 +1,4 @@ +import re from pathlib import Path from admin.decorators import superuser_only @@ -66,6 +67,18 @@ def refusal(setting, value): elif setting.choices and value not in setting.choices_as_list(): return _("%(setting)s must be one of %(choices)s") % { "setting": setting.name, "choices": ", ".join(setting.choices_as_list())} + elif setting.key == "CLONE_INSTANCE_DEFAULT_PREFIX" and not re.fullmatch(r"[a-zA-Z0-9-]+", value): + # a clone's name is the prefix and a number, and takes only these + return _("%(setting)s takes letters, digits and '-' only") % {"setting": setting.name} + return None + + +def combined_refusal(values): + """Why settings that work only together cannot be saved as values + (key: value, saved or new), or None.""" + # libvirt refuses such a disk: every new VM would fail + if values["INSTANCE_VOLUME_DEFAULT_IO"] == "native" and values["INSTANCE_VOLUME_DEFAULT_CACHE"] not in ("none", "directsync"): + return _("Disk I/O native needs disk cache none or directsync") return None @@ -98,6 +111,11 @@ def appsettings(request): key=lambda item: item[0].name) changed = [(s, value) for s, value in posted if value != request.POST.get(f"was-{s.key}", s.value)] refusals = [refused for refused in (refusal(s, value) for s, value in changed) if refused] + # checked only when the pair changes: a pair saved before does not block other settings + pair = {"INSTANCE_VOLUME_DEFAULT_IO", "INSTANCE_VOLUME_DEFAULT_CACHE"} + if not refusals and pair & {s.key for s, _v in changed} and (refused := combined_refusal( + {key: s.value for key, s in settings_by_key.items()} | {s.key: value for s, value in changed})): + refusals = [refused] for refused in refusals: messages.error(request, refused) if changed and not refusals: diff --git a/locale/tr/LC_MESSAGES/django.mo b/locale/tr/LC_MESSAGES/django.mo index 97a2eb4b..fa80520b 100644 Binary files a/locale/tr/LC_MESSAGES/django.mo and b/locale/tr/LC_MESSAGES/django.mo differ diff --git a/locale/tr/LC_MESSAGES/django.po b/locale/tr/LC_MESSAGES/django.po index 8a6643ab..1d98f55a 100644 --- a/locale/tr/LC_MESSAGES/django.po +++ b/locale/tr/LC_MESSAGES/django.po @@ -11,7 +11,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2026-11-12 10:25+0000\n" +"POT-Creation-Date: 2026-11-12 12:21+0000\n" "PO-Revision-Date: 2020-11-04 12:00+0000\n" "Last-Translator: catborise , 2020\n" "Language-Team: Turkish (https://www.transifex.com/catborise/teams/110663/tr/)\n" @@ -1736,79 +1736,89 @@ msgstr "Temayı değiştir" msgid "Save %(group_title)s" msgstr "%(group_title)s kaydet" -#: appsettings/views.py:37 +#: appsettings/views.py:38 msgid "New VM defaults" msgstr "Yeni sanal makine varsayılanları" -#: appsettings/views.py:40 instances/templates/instance.html:70 +#: appsettings/views.py:41 instances/templates/instance.html:70 #: instances/templates/instances/destroy_instance_form.html:33 #: instances/templates/instances/resize_tab.html:20 #: instances/templates/instances/settings_tab.html:197 msgid "Disks" msgstr "Diskler" -#: appsettings/views.py:44 +#: appsettings/views.py:45 msgid "Clones" msgstr "Klonlar" -#: appsettings/views.py:45 console/templates/console-base.html:10 +#: appsettings/views.py:46 console/templates/console-base.html:10 #: console/templates/console-base.html:23 #: instances/templates/instance_actions.html:11 #: instances/templates/instances/settings_tab.html:32 msgid "Console" msgstr "Konsol" -#: appsettings/views.py:46 +#: appsettings/views.py:47 msgid "VM access and owners" msgstr "Sanal makine erişimi ve sahipleri" -#: appsettings/views.py:48 +#: appsettings/views.py:49 msgid "Snapshots and jobs" msgstr "Anlık görüntüler ve işler" -#: appsettings/views.py:50 +#: appsettings/views.py:51 msgid "Login" msgstr "Oturum açma" -#: appsettings/views.py:51 +#: appsettings/views.py:52 msgid "Lists and pages" msgstr "Listeler ve sayfalar" -#: appsettings/views.py:59 +#: appsettings/views.py:60 #, python-format msgid "%(setting)s takes at most %(length)s characters" msgstr "%(setting)s en fazla %(length)s karakter alır" -#: appsettings/views.py:64 +#: appsettings/views.py:65 #, python-format msgid "%(setting)s must be a whole number between %(low)s and %(high)s" msgstr "%(setting)s, %(low)s ile %(high)s arasında bir tam sayı olmalıdır" -#: appsettings/views.py:67 +#: appsettings/views.py:68 #, python-format msgid "%(setting)s must be one of %(choices)s" msgstr "%(setting)s şunlardan biri olmalıdır: %(choices)s" -#: appsettings/views.py:85 +#: appsettings/views.py:72 +#, python-format +msgid "%(setting)s takes letters, digits and '-' only" +msgstr "%(setting)s yalnızca harf, rakam ve '-' alır" + +#: appsettings/views.py:81 +msgid "Disk I/O native needs disk cache none or directsync" +msgstr "" +"Disk G/Ç native, disk önbelleği none veya directsync olmasını gerektirir" + +#: appsettings/views.py:98 #, python-format msgid "Unknown theme: %(theme)s" msgstr "Bilinmeyen tema: %(theme)s" -#: appsettings/views.py:91 +#: appsettings/views.py:104 #, python-format msgid "Theme is changed. Now: %(theme)s" msgstr "Tema değiştirildi. Şimdi: %(theme)s" -#: appsettings/views.py:114 +#: appsettings/views.py:132 #, python-format msgid "Saved: %(settings)s" msgstr "Kaydedildi: %(settings)s" -#: appsettings/views.py:116 +#: appsettings/views.py:134 msgid "Nothing changed" msgstr "Hiçbir şey değişmedi" -#: appsettings/views.py:123 vrtManager/nodedevs.py:294 +#: appsettings/views.py:141 vrtManager/nodedevs.py:294 #: vrtManager/nodedevs.py:333 msgid "Other" msgstr "Diğer" diff --git a/locale/zh/LC_MESSAGES/django.mo b/locale/zh/LC_MESSAGES/django.mo index 4ba918d0..0fdd6024 100644 Binary files a/locale/zh/LC_MESSAGES/django.mo and b/locale/zh/LC_MESSAGES/django.mo differ diff --git a/locale/zh/LC_MESSAGES/django.po b/locale/zh/LC_MESSAGES/django.po index 22a42672..cb9ad6f5 100644 --- a/locale/zh/LC_MESSAGES/django.po +++ b/locale/zh/LC_MESSAGES/django.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2026-11-12 10:25+0000\n" +"POT-Creation-Date: 2026-11-12 12:21+0000\n" "PO-Revision-Date: \n" "Last-Translator: \n" "Language-Team: \n" @@ -1679,79 +1679,88 @@ msgstr "更改主题" msgid "Save %(group_title)s" msgstr "保存%(group_title)s" -#: appsettings/views.py:37 +#: appsettings/views.py:38 msgid "New VM defaults" msgstr "新建虚拟机默认值" -#: appsettings/views.py:40 instances/templates/instance.html:70 +#: appsettings/views.py:41 instances/templates/instance.html:70 #: instances/templates/instances/destroy_instance_form.html:33 #: instances/templates/instances/resize_tab.html:20 #: instances/templates/instances/settings_tab.html:197 msgid "Disks" msgstr "磁盘" -#: appsettings/views.py:44 +#: appsettings/views.py:45 msgid "Clones" msgstr "克隆" -#: appsettings/views.py:45 console/templates/console-base.html:10 +#: appsettings/views.py:46 console/templates/console-base.html:10 #: console/templates/console-base.html:23 #: instances/templates/instance_actions.html:11 #: instances/templates/instances/settings_tab.html:32 msgid "Console" msgstr "控制台" -#: appsettings/views.py:46 +#: appsettings/views.py:47 msgid "VM access and owners" msgstr "虚拟机访问与所有者" -#: appsettings/views.py:48 +#: appsettings/views.py:49 msgid "Snapshots and jobs" msgstr "快照与任务" -#: appsettings/views.py:50 +#: appsettings/views.py:51 msgid "Login" msgstr "登录" -#: appsettings/views.py:51 +#: appsettings/views.py:52 msgid "Lists and pages" msgstr "列表与页面" -#: appsettings/views.py:59 +#: appsettings/views.py:60 #, python-format msgid "%(setting)s takes at most %(length)s characters" msgstr "%(setting)s 最多 %(length)s 个字符" -#: appsettings/views.py:64 +#: appsettings/views.py:65 #, python-format msgid "%(setting)s must be a whole number between %(low)s and %(high)s" msgstr "%(setting)s 必须是 %(low)s 到 %(high)s 之间的整数" -#: appsettings/views.py:67 +#: appsettings/views.py:68 #, python-format msgid "%(setting)s must be one of %(choices)s" msgstr "%(setting)s 必须是以下之一:%(choices)s" -#: appsettings/views.py:85 +#: appsettings/views.py:72 +#, python-format +msgid "%(setting)s takes letters, digits and '-' only" +msgstr "%(setting)s 只能包含字母、数字和 '-'" + +#: appsettings/views.py:81 +msgid "Disk I/O native needs disk cache none or directsync" +msgstr "磁盘 I/O native 需要磁盘缓存为 none 或 directsync" + +#: appsettings/views.py:98 #, python-format msgid "Unknown theme: %(theme)s" msgstr "未知主题:%(theme)s" -#: appsettings/views.py:91 +#: appsettings/views.py:104 #, python-format msgid "Theme is changed. Now: %(theme)s" msgstr "主题已更改。当前:%(theme)s" -#: appsettings/views.py:114 +#: appsettings/views.py:132 #, python-format msgid "Saved: %(settings)s" msgstr "已保存:%(settings)s" -#: appsettings/views.py:116 +#: appsettings/views.py:134 msgid "Nothing changed" msgstr "没有任何更改" -#: appsettings/views.py:123 vrtManager/nodedevs.py:294 +#: appsettings/views.py:141 vrtManager/nodedevs.py:294 #: vrtManager/nodedevs.py:333 msgid "Other" msgstr "其他"