diff --git a/jituan/services/huiyuan_bundle.py b/jituan/services/huiyuan_bundle.py index a5cd049..86abbe4 100644 --- a/jituan/services/huiyuan_bundle.py +++ b/jituan/services/huiyuan_bundle.py @@ -51,6 +51,21 @@ def format_legacy_daoqi(dt): return dt.strftime('%Y-%m-%d %H:%M:%S') +def _normalize_datetime_for_compare(dt): + """统一 naive/aware,避免 daoqi 与 now 比较时报 TypeError。""" + from django.conf import settings + + if dt is None: + return timezone.now() + if settings.USE_TZ: + if timezone.is_naive(dt): + return timezone.make_aware(dt, timezone.get_current_timezone()) + return dt + if timezone.is_aware(dt): + return timezone.make_naive(dt, timezone.get_current_timezone()) + return dt + + def get_bundle_included_ids(club_id, bundle_huiyuan_id): from jituan.models import ClubHuiyuanBundleInclude @@ -128,7 +143,7 @@ def _czjilu_grants_huiyuan_access(yonghu_id, required_huiyuan_id, club_id=None): req = normalize_huiyuan_id(required_huiyuan_id) if not req: return False - now = timezone.now() + now = _normalize_datetime_for_compare(timezone.now()) for order in Czjilu.query.filter( yonghuid=yonghu_id, leixing=MEMBER_LEIXING, @@ -142,9 +157,7 @@ def _czjilu_grants_huiyuan_access(yonghu_id, required_huiyuan_id, club_id=None): if days <= 0: _, _, days = club_huiyuan_sellable(cid, order.huiyuan_id, is_trial=is_trial) days = max(int(days or 0), 1) - start = order.CreateTime or now - if timezone.is_naive(start): - start = timezone.make_aware(start, timezone.get_current_timezone()) + start = _normalize_datetime_for_compare(order.CreateTime or now) if start + timedelta(days=days) > now: return True return False @@ -305,7 +318,7 @@ def _build_clumber_from_paid_czjilu(yonghu_id): items = [] seen = set() - now = timezone.now() + now = _normalize_datetime_for_compare(timezone.now()) for order in Czjilu.query.filter( yonghuid=yonghu_id, leixing=MEMBER_LEIXING, @@ -320,9 +333,7 @@ def _build_clumber_from_paid_czjilu(yonghu_id): if days <= 0: _, _, days = club_huiyuan_sellable(cid, hid, is_trial=is_trial) days = max(int(days or 0), 1) - start = order.CreateTime or now - if timezone.is_naive(start): - start = timezone.make_aware(start, timezone.get_current_timezone()) + start = _normalize_datetime_for_compare(order.CreateTime or now) daoqi = start + timedelta(days=days) if daoqi <= now: continue