fix: 迁移会员过户统一 naive/aware 时间比较,避免 confirm 500

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
XingQue
2026-07-29 03:58:08 +08:00
parent b324e3ee0f
commit 47f69dc8ab

View File

@@ -307,7 +307,12 @@ def _preview_huiyuan(uid: str, club_id: str, role: str) -> List[dict]:
for r in rows:
name = (r.jieshao or '').strip() or name_map.get(r.huiyuan_id) or '会员'
exp = _aware(r.daoqi_time)
exp_text = timezone.localtime(exp).strftime('%Y-%m-%d %H:%M') if exp else ''
if exp is None:
exp_text = ''
elif timezone.is_aware(exp):
exp_text = timezone.localtime(exp).strftime('%Y-%m-%d %H:%M')
else:
exp_text = exp.strftime('%Y-%m-%d %H:%M')
out.append({
'key': f'{role}.huiyuan:{r.huiyuan_id}',
'role': role,
@@ -383,7 +388,7 @@ def _add_huiyuan_duration(*, to_uid: str, to_club: str, huiyuan_id: str, remain,
"""给目标用户对应会员加时长(已有则累加剩余,否则从现在起算)。"""
from products.models import Huiyuangoumai
now = _now()
now = _now_aware()
remain = remain if remain and remain.total_seconds() > 0 else None
if remain is None:
return None, {}
@@ -427,13 +432,13 @@ def _add_huiyuan_duration(*, to_uid: str, to_club: str, huiyuan_id: str, remain,
def _transfer_huiyuan(*, from_uid, to_uid, from_club, to_club, role) -> List[dict]:
from products.models import Huiyuan, Huiyuangoumai
now = _now()
now = _now_aware()
rows = list(
Huiyuangoumai.objects.select_for_update().filter(
yonghu_id=str(from_uid),
club_id=from_club,
huiyuan_zhuangtai=1,
daoqi_time__gt=now,
daoqi_time__gt=_now(),
)
)
out = []
@@ -525,13 +530,25 @@ def _transfer_huiyuan(*, from_uid, to_uid, from_club, to_club, role) -> List[dic
def _aware(dt):
"""与 timezone.now() 对齐 aware/naive避免混比炸 500。"""
if dt is None:
return None
if timezone.is_naive(dt):
return timezone.make_aware(dt, timezone.get_current_timezone())
now_sample = timezone.now()
if timezone.is_aware(now_sample):
if timezone.is_naive(dt):
return timezone.make_aware(dt, timezone.get_current_timezone())
return dt
# 项目未开 USE_TZ一律 naive
if timezone.is_aware(dt):
return timezone.make_naive(dt, timezone.get_current_timezone())
return dt
def _now_aware():
"""业务比较用的当前时间(与 _aware 同一套时区规则)。"""
return _aware(_now()) or _now()
def check_conditions(plan: ClubMigratePlan, from_uid: str) -> Tuple[bool, str]:
"""返回 (ok, msg)。"""
cond = plan.condition_json or {}