diff --git a/apiserver/paasng/paasng/extensions/declarative/application/fields.py b/apiserver/paasng/paasng/extensions/declarative/application/fields.py index f988c5c503..067ca1bc10 100644 --- a/apiserver/paasng/paasng/extensions/declarative/application/fields.py +++ b/apiserver/paasng/paasng/extensions/declarative/application/fields.py @@ -18,10 +18,10 @@ """ import logging -from paasng.extensions.declarative.application.constants import APP_NAME_FIELD from paasng.extensions.declarative.application.validations import ApplicationDesc from paasng.extensions.declarative.exceptions import DescriptionValidationError from paasng.platform.applications.models import Application +from paasng.platform.applications.signals import prepare_change_application_name logger = logging.getLogger(__name__) @@ -38,14 +38,25 @@ def handle_desc(self, desc: ApplicationDesc): class AppNameField(AppField): def handle_desc(self, desc: ApplicationDesc): + update_field_dict = {} + app_code = self.application.name.code if self.application.name != desc.name_zh_cn: - raise DescriptionValidationError({APP_NAME_FIELD: '该字段不允许被修改'}) + # 修改中文名 + logger.warning("应用<%s> 的英文名将从 '%s' 修改成 '%s'", app_code, self.application.name, desc.name_zh_cn) + update_field_dict["name"] = desc.name_zh_cn + self.application.name = desc.name_zh_cn + self.application.save(update_fields=["name", "updated"]) + if self.application.name_en != desc.name_en: - # 允许修改英文名 - logger.warning("应用<%s> 的英文名将从 '%s' 修改成 '%s'", desc.name_zh_cn, self.application.name_en, desc.name_en) + # 修改英文名 + logger.warning("应用<%s> 的英文名将从 '%s' 修改成 '%s'", app_code, self.application.name_en, desc.name_en) + update_field_dict["name_en"] = desc.name_en self.application.name_en = desc.name_en self.application.save(update_fields=["name_en", "updated"]) + # 应用名称修改后需要同步给桌面 + prepare_change_application_name.send(sender="", code=app_code, **update_field_dict) + class AppRegionField(AppField): def handle_desc(self, desc: ApplicationDesc): diff --git a/apiserver/paasng/tests/extensions/declarative/test_application.py b/apiserver/paasng/tests/extensions/declarative/test_application.py index c11ef17937..17a5e3198a 100644 --- a/apiserver/paasng/tests/extensions/declarative/test_application.py +++ b/apiserver/paasng/tests/extensions/declarative/test_application.py @@ -198,13 +198,17 @@ def test_region_modified(self, bk_user, existed_app): def test_name_modified(self, bk_user, existed_app): # Use new name + new_name = existed_app.name + "2" + new_name_en = existed_app.name + "en" app_json = make_app_desc(existed_app.code) - app_json["bk_app_name"] = existed_app.name + '2' + app_json["bk_app_name"] = new_name + app_json["bk_app_name_en"] = new_name_en controller = AppDeclarativeController(bk_user) - with pytest.raises(DescriptionValidationError) as exc_info: - controller.perform_action(get_app_description(app_json)) - assert 'bk_app_name' in exc_info.value.detail + controller.perform_action(get_app_description(app_json)) + product = Product.objects.get(code=existed_app.code) + assert product.name == new_name + assert product.name_en == new_name_en def test_normal(self, bk_user, existed_app): app_json = make_app_desc(existed_app.code)