Skip to content

Commit

Permalink
refactor: cr 调整
Browse files Browse the repository at this point in the history
  • Loading branch information
neronkl committed Nov 10, 2023
1 parent 13f3739 commit 55c714c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
3 changes: 0 additions & 3 deletions src/bk-user/bkuser/apis/web/tenant_setting/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@
TenantUserValidityPeriodConfig,
UserBuiltinField,
)
from bkuser.apps.tenant.periodic_tasks import send_tenant_user_expiring_notification
from bkuser.common.error_codes import error_codes
from bkuser.common.views import ExcludePatchAPIViewMixin, ExcludePutAPIViewMixin

send_tenant_user_expiring_notification()


class TenantUserFieldListApi(CurrentUserTenantMixin, generics.ListAPIView):
pagination_class = None
Expand Down
31 changes: 17 additions & 14 deletions src/bk-user/bkuser/apps/tenant/periodic_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from bkuser.apps.tenant.constants import NotificationScene
from bkuser.apps.tenant.models import Tenant, TenantUser, TenantUserValidityPeriodConfig
from bkuser.apps.tenant.notifier import TenantUserValidityPeriodNotifier
from bkuser.apps.tenant.tasks import send_notifications
from bkuser.celery import app
from bkuser.common.task import BaseTask

Expand Down Expand Up @@ -49,22 +49,24 @@ def send_tenant_user_expiring_notification():
should_notify_users = []
for remain_days in remind_before_expire:
account_expired_date = now + datetime.timedelta(days=int(remain_days))
account_expired_date_start = account_expired_date.replace(hour=0, minute=0, second=0)

should_notify_users += list(
tenant_users.filter(
account_expired_at__range=[
account_expired_date.replace(hour=0, minute=0, second=0),
account_expired_date.replace(hour=23, minute=59, second=59, microsecond=999999),
account_expired_date_start,
account_expired_date_start + datetime.timedelta(days=1),
]
)
).values_list("id", flat=True)
)
# 发送通知
logger.info("going to notify expiring users in tenant{%s}, count: %s", tenant_id, len(should_notify_users))

if not should_notify_users:
return
continue

TenantUserValidityPeriodNotifier(tenant_id=tenant_id, scene=NotificationScene.TENANT_USER_EXPIRING).send(
should_notify_users
send_notifications.delay(
tenant_id=tenant_id, scene=NotificationScene.TENANT_USER_EXPIRING, tenant_user_ids=should_notify_users
)


Expand All @@ -77,24 +79,25 @@ def send_tenant_user_expired_notification():
now = timezone.now()
# 获取 租户-过期用户
tenant_ids = Tenant.objects.filter().values_list("id", flat=True)

tenant_user_map = {
tenant_id: TenantUser.objects.filter(
account_expired_at__range=[
now.replace(hour=0, minute=0, second=0),
now.replace(hour=23, minute=59, second=59, microsecond=999999),
now + datetime.timedelta(days=1),
],
tenant_id=tenant_id,
)
).values_list("id", flat=True)
for tenant_id in tenant_ids
}

for tenant_id, tenant_users in tenant_user_map.items():
for tenant_id, tenant_user_ids in tenant_user_map.items():
# 发送过期通知
should_notify_users = list(tenant_users)
should_notify_users = list(tenant_user_ids)
logger.info("going to notify expired users in tenant{%s}, count: %s", tenant_id, len(should_notify_users))
if not should_notify_users:
return
continue

TenantUserValidityPeriodNotifier(tenant_id=tenant_id, scene=NotificationScene.TENANT_USER_EXPIRED).send(
should_notify_users
send_notifications.delay(
tenant_id=tenant_id, scene=NotificationScene.TENANT_USER_EXPIRED, tenant_user_ids=should_notify_users
)
32 changes: 32 additions & 0 deletions src/bk-user/bkuser/apps/tenant/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available.
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
import logging
from typing import List

from bkuser.apps.tenant.constants import NotificationScene
from bkuser.apps.tenant.models import TenantUser
from bkuser.apps.tenant.notifier import TenantUserValidityPeriodNotifier
from bkuser.celery import app
from bkuser.common.task import BaseTask

logger = logging.getLogger(__name__)


@app.task(base=BaseTask, ignore_result=True)
def send_notifications(tenant_id: str, scene: NotificationScene, tenant_user_ids: List[str]):
users = TenantUser.objects.filter(id__in=tenant_user_ids)
logger.info(
"going to send notification for users. user_count=%s tenant=%s, scene=%s", len(users), tenant_id, scene
)
try:
TenantUserValidityPeriodNotifier(tenant_id=tenant_id, scene=scene).send(users)
except Exception:
logger.exception("send notification failed, please check!")

0 comments on commit 55c714c

Please sign in to comment.