Skip to content

Commit

Permalink
feat: smart 应用支持修改中文名 (#941)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiayuan929 authored Nov 29, 2023
1 parent 440d59e commit d2594bf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d2594bf

Please sign in to comment.