From 496467ba751cff06a2aabf3b04cbf38c50e96d21 Mon Sep 17 00:00:00 2001 From: "Crane.z" <1481445951@qq.com> Date: Tue, 28 Jul 2026 17:23:52 +0800 Subject: [PATCH] feat:simple --- apps/common/utils/timezone.py | 14 -------------- apps/perms/tasks.py | 17 +++++++++-------- apps/users/tasks.py | 19 +++++++++---------- 3 files changed, 18 insertions(+), 32 deletions(-) diff --git a/apps/common/utils/timezone.py b/apps/common/utils/timezone.py index 2b328b11e..fd168c12c 100644 --- a/apps/common/utils/timezone.py +++ b/apps/common/utils/timezone.py @@ -16,20 +16,6 @@ def local_now(): return dj_timezone.localtime(dj_timezone.now()) -def expiration_remain_days(date_expired, today=None): - if today is None: - today = local_now().date() - date_expired = dj_timezone.localtime(date_expired).date() - return (date_expired - today).days - - -def should_notify_expiration(remain_days, first_notice_days, daily_notice_days): - return ( - remain_days == first_notice_days - or 0 <= remain_days <= daily_notice_days - ) - - def local_now_display(fmt='%Y-%m-%d %H:%M:%S'): return local_now().strftime(fmt) diff --git a/apps/perms/tasks.py b/apps/perms/tasks.py index 7592faeb0..1af6276e7 100644 --- a/apps/perms/tasks.py +++ b/apps/perms/tasks.py @@ -11,9 +11,7 @@ from django.utils.translation import gettext_lazy as _ from common.const.crontab import CRONTAB_AT_AM_TEN from common.utils import get_logger -from common.utils.timezone import ( - dt_parser, expiration_remain_days, local_now, should_notify_expiration, -) +from common.utils.timezone import dt_parser, local_now from ops.celery.decorator import register_as_period_task from orgs.utils import tmp_to_root_org from perms.models import AssetPermission @@ -62,8 +60,8 @@ def check_asset_permission_expired(): def check_asset_permission_will_expired(): first_notice_days = settings.PERM_EXPIRED_FIRST_NOTICE_DAYS daily_notice_days = settings.PERM_EXPIRED_DAILY_NOTICE_DAYS - start = local_now().replace(hour=0, minute=0, second=0, microsecond=0) - end = start + timedelta(days=max(first_notice_days, daily_notice_days) + 1) + start = local_now() + end = start + timedelta(days=first_notice_days + 1) user_asset_remain_day_mapper = defaultdict(dict) org_perm_remain_day_mapper = defaultdict(dict) @@ -76,9 +74,12 @@ def check_asset_permission_will_expired(): for asset_perm in asset_perms: date_expired = dt_parser(asset_perm.date_expired) - remain_days = expiration_remain_days(date_expired, start.date()) - if not should_notify_expiration( - remain_days, first_notice_days, daily_notice_days): + remain_days = (date_expired - start).days + should_notify = ( + remain_days == first_notice_days + or 0 <= remain_days <= daily_notice_days + ) + if not should_notify: continue org = asset_perm.org diff --git a/apps/users/tasks.py b/apps/users/tasks.py index c4f32e10c..0232d875b 100644 --- a/apps/users/tasks.py +++ b/apps/users/tasks.py @@ -12,9 +12,6 @@ from django.utils.translation import gettext_lazy as _, gettext_noop from audits.const import ActivityChoices from common.const.crontab import CRONTAB_AT_AM_TEN, CRONTAB_AT_PM_TWO from common.utils import get_logger -from common.utils.timezone import ( - expiration_remain_days, local_now, should_notify_expiration, -) from ops.celery.decorator import after_app_ready_start, register_as_period_task from ops.celery.utils import create_or_update_celery_periodic_tasks from orgs.utils import tmp_to_root_org @@ -76,18 +73,20 @@ def check_password_expired_periodic(): def check_user_expired(): first_notice_days = settings.USER_EXPIRED_FIRST_NOTICE_DAYS daily_notice_days = settings.USER_EXPIRED_DAILY_NOTICE_DAYS - start = local_now().replace(hour=0, minute=0, second=0, microsecond=0) - end = start + timedelta(days=max(first_notice_days, daily_notice_days) + 1) + date_expired_lt = timezone.now() + timezone.timedelta(days=first_notice_days + 1) users = User.get_nature_users() \ .filter(source=User.Source.local) \ - .filter(date_expired__gte=start, date_expired__lt=end) + .filter(date_expired__lt=date_expired_lt) for user in users: - if not user.is_active: + if not user.is_valid: continue - remain_days = expiration_remain_days(user.date_expired, start.date()) - if not should_notify_expiration( - remain_days, first_notice_days, daily_notice_days): + remain_days = user.expired_remain_days + should_notify = ( + remain_days == first_notice_days + or 0 <= remain_days <= daily_notice_days + ) + if not should_notify: continue msg = "The user {} will expires in {} days" logger.info(msg.format(user, remain_days))