Skip to content

Commit

Permalink
refactor: cr调整
Browse files Browse the repository at this point in the history
  • Loading branch information
neronkl committed Nov 7, 2023
1 parent f9c1e00 commit c0c7602
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 59 deletions.
18 changes: 14 additions & 4 deletions src/bk-user/bkuser/apis/web/tenant_setting/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ class TenantUserValidityPeriodConfigRetrieveUpdateApi(

def get_object(self) -> TenantUserValidityPeriodConfig:
tenant_id = self.get_current_tenant_id()
account_validity_period_config = self.queryset.filter(tenant_id=tenant_id).first()
if not account_validity_period_config:
tenant_user_validity_period_config = self.queryset.filter(tenant_id=tenant_id).first()
if not tenant_user_validity_period_config:
raise error_codes.OBJECT_NOT_FOUND.f(_("账户有效期配置丢失,请联系系统管理员"))

return account_validity_period_config
return tenant_user_validity_period_config

@swagger_auto_schema(
tags=["tenant-setting"],
Expand Down Expand Up @@ -165,7 +165,17 @@ def put(self, request, *args, **kwargs):
valid_time=data["valid_time"],
remind_before_expire=data["remind_before_expire"],
enabled_notification_methods=data["enabled_notification_methods"],
notification_templates=[NotificationTemplate(**item) for item in data["notification_templates"]],
notification_templates=[
NotificationTemplate(
method=template["method"],
scene=template["scene"],
title=template.get("title", None),
sender=template["sender"],
content=template["content"],
content_html=template["content_html"],
)
for template in data["notification_templates"]
],
)

TenantUserValidityPeriodConfigHandler.update_tenant_user_validity_period_config(
Expand Down
121 changes: 66 additions & 55 deletions src/bk-user/bkuser/biz/tenant_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,82 +74,82 @@ class ValidityPeriodConfig(BaseModel):

class TenantUserValidityPeriodConfigHandler:
# 账户有效期配置初始化,默认值
DEFAULT_TENANT_USER_VALIDITY_PERIOD_CONFIG = {
"enabled_validity_period": True,
"valid_time": -1,
"remind_before_expire": [7],
"enabled_notification_methods": [NotificationMethod.EMAIL],
"notification_templates": [
{
"method": NotificationMethod.EMAIL,
"scene": NotificationScene.TENANT_USER_EXPIRING,
"title": "蓝鲸智云 - 账号即将到期提醒!",
"sender": "蓝鲸智云",
"content": (
"{{ username }}, 您好:\n"
DEFAULT_TENANT_USER_VALIDITY_PERIOD_CONFIG = ValidityPeriodConfig(
enabled_validity_period=True,
valid_time=-1,
remind_before_expire=[7],
enabled_notification_methods=[NotificationMethod.EMAIL],
notification_templates=[
NotificationTemplate(
method=NotificationMethod.EMAIL,
scene=NotificationScene.TENANT_USER_EXPIRING,
title="蓝鲸智云 - 账号即将到期提醒!",
sender="蓝鲸智云",
content=(
"{{ username }}, 您好:\n "
+ "您的蓝鲸智云平台账号将于 {{ expired_at }} 天后到期。"
+ "为避免影响使用,请尽快联系平台管理员进行续期。\n"
+ "此邮件为系统自动发送,请勿回复。\n"
+ "为避免影响使用,请尽快联系平台管理员进行续期。\n "
+ "此邮件为系统自动发送,请勿回复。\n "
),
"content_html": (
content_html=(
"<p>{{ username }}, 您好:</p>"
+ "<p>您的蓝鲸智云平台账号将于 {{ expired_at }} 天后到期。"
+ "为避免影响使用,请尽快联系平台管理员进行续期。</p>"
+ "<p>此邮件为系统自动发送,请勿回复。</p>"
),
},
{
"method": NotificationMethod.EMAIL,
"scene": NotificationScene.TENANT_USER_EXPIRED,
"title": "蓝鲸智云 - 账号到期提醒!",
"sender": "蓝鲸智云",
"content": (
"{{ username }},您好:\n"
+ "您的蓝鲸智云平台账号已过期。为避免影响使用,请尽快联系平台管理员进行续期。\n" # noqa: E501
),
NotificationTemplate(
method=NotificationMethod.EMAIL,
scene=NotificationScene.TENANT_USER_EXPIRED,
title="蓝鲸智云 - 账号到期提醒!",
sender="蓝鲸智云",
content=(
"{{ username }},您好:\n "
+ "您的蓝鲸智云平台账号已过期。为避免影响使用,请尽快联系平台管理员进行续期。\n " # noqa: E501
+ "该邮件为系统自动发送,请勿回复。"
),
"content_html": (
content_html=(
"<p>{{ username }},您好:</p>"
+ "<p>您的蓝鲸智云平台账号已过期,如需继续使用,请尽快联系平台管理员进行续期。</p>"
+ "<p>此邮件为系统自动发送,请勿回复。</p>"
),
},
{
"method": NotificationMethod.SMS,
"scene": NotificationScene.TENANT_USER_EXPIRING,
"title": None,
"sender": "蓝鲸智云",
"content": (
"{{ username }},您好:\n"
),
NotificationTemplate(
method=NotificationMethod.SMS,
scene=NotificationScene.TENANT_USER_EXPIRING,
title=None,
sender="蓝鲸智云",
content=(
"{{ username }},您好:\n "
+ "您的蓝鲸智云平台账号将于 {{ expired_at }} 天后到期。"
+ "为避免影响使用,请尽快联系平台管理员进行续期。\n"
+ "为避免影响使用,请尽快联系平台管理员进行续期。\n "
+ "该短信为系统自动发送,请勿回复。"
),
"content_html": (
content_html=(
"<p>{{ username }},您好:</p>"
+ "<p>您的蓝鲸智云平台账号将于 {{ expired_at }} 天后到期。"
+ "为避免影响使用,请尽快联系平台管理员进行续期。</p>"
+ "<p>该短信为系统自动发送,请勿回复。</p>"
),
},
{
"method": NotificationMethod.SMS,
"scene": NotificationScene.TENANT_USER_EXPIRED,
"title": None,
"sender": "蓝鲸智云",
"content": (
"{{ username }}您好:\n"
+ "您的蓝鲸智云平台账号已过期,如需继续使用,请尽快联系平台管理员进行续期。\n" # noqa: E501
),
NotificationTemplate(
method=NotificationMethod.SMS,
scene=NotificationScene.TENANT_USER_EXPIRED,
title=None,
sender="蓝鲸智云",
content=(
"{{ username }}您好:\n "
+ "您的蓝鲸智云平台账号已过期,如需继续使用,请尽快联系平台管理员进行续期。\n " # noqa: E501
+ "该短信为系统自动发送,请勿回复。" # noqa: E501
),
"content_html": (
content_html=(
"<p>{{ username }}您好:</p>"
+ "<p>您的蓝鲸智云平台账号已过期,如需继续使用,请尽快联系平台管理员进行续期。</p>"
+ "<p>该短信为系统自动发送,请勿回复。</p>"
),
},
),
],
}
)

def init_tenant_user_validity_period_config(
self,
Expand All @@ -165,13 +165,14 @@ def init_tenant_user_validity_period_config(

validity_period_config = self.DEFAULT_TENANT_USER_VALIDITY_PERIOD_CONFIG

notification_templates = [template.model_dump() for template in validity_period_config.notification_templates]
TenantUserValidityPeriodConfig.objects.create(
tenant=tenant,
enabled_validity_period=validity_period_config["enabled_validity_period"],
valid_time=validity_period_config["valid_time"],
remind_before_expire=validity_period_config["remind_before_expire"],
enabled_notification_methods=validity_period_config["enabled_notification_methods"],
notification_templates=validity_period_config["notification_templates"],
enabled_validity_period=validity_period_config.enabled_validity_period,
valid_time=validity_period_config.valid_time,
remind_before_expire=validity_period_config.remind_before_expire,
enabled_notification_methods=validity_period_config.enabled_notification_methods,
notification_templates=notification_templates,
updater=operator,
creator=operator,
)
Expand All @@ -184,7 +185,7 @@ def update_tenant_user_validity_period_config(
if not instance:
raise error_codes.OBJECT_NOT_FOUND

notification_templates = [item.model_dump() for item in validity_period_config.notification_templates]
notification_templates = [template.model_dump() for template in validity_period_config.notification_templates]
with transaction.atomic():
instance.enabled_validity_period = validity_period_config.enabled_validity_period
instance.valid_time = validity_period_config.valid_time
Expand All @@ -205,5 +206,15 @@ def get_tenant_user_validity_period_config(tenant_id: str) -> ValidityPeriodConf
valid_time=instance.valid_time,
remind_before_expire=instance.remind_before_expire,
enabled_notification_methods=instance.enabled_notification_methods,
notification_templates=instance.notification_templates,
notification_templates=[
NotificationTemplate(
method=template["method"],
scene=template["scene"],
title=template.get("title", None),
sender=template["sender"],
content=template["content"],
content_html=template["content_html"],
)
for template in instance.notification_templates
],
)

0 comments on commit c0c7602

Please sign in to comment.