This commit is contained in:
@@ -98,9 +98,14 @@ class ActivitySignupAdmin(ModelAdmin):
|
||||
"pending": "warning",
|
||||
"confirmed": "success",
|
||||
"cancelled": "danger",
|
||||
"unpaid": "secondary",
|
||||
}
|
||||
)
|
||||
def status_label(self, obj):
|
||||
# Auto sync with order status on display
|
||||
if obj.check_payment_status():
|
||||
# If status changed, return new status
|
||||
return obj.status
|
||||
return obj.status
|
||||
|
||||
@display(description="关联订单")
|
||||
|
||||
@@ -95,6 +95,17 @@ class ActivitySignup(models.Model):
|
||||
def __str__(self):
|
||||
return f"{self.user.nickname} - {self.activity.title}"
|
||||
|
||||
def check_payment_status(self):
|
||||
"""
|
||||
检查并同步关联订单的支付状态
|
||||
"""
|
||||
if self.status == 'unpaid' and self.order:
|
||||
if self.order.status == 'paid':
|
||||
self.status = 'confirmed' if self.activity.auto_confirm else 'pending'
|
||||
self.save()
|
||||
return True
|
||||
return False
|
||||
|
||||
class Meta:
|
||||
verbose_name = "活动报名"
|
||||
verbose_name_plural = "活动报名管理"
|
||||
|
||||
@@ -197,6 +197,11 @@ class ActivityViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
if not user:
|
||||
return Response({'error': '请先登录'}, status=401)
|
||||
signups = ActivitySignup.objects.filter(user=user).order_by('-signup_time')
|
||||
|
||||
# Sync payment status
|
||||
for signup in signups:
|
||||
signup.check_payment_status()
|
||||
|
||||
serializer = ActivitySignupSerializer(signups, many=True)
|
||||
return Response(serializer.data)
|
||||
|
||||
|
||||
@@ -532,10 +532,15 @@ def payment_finish(request):
|
||||
|
||||
if signup:
|
||||
# Determine status based on activity setting
|
||||
new_status = 'confirmed' if signup.activity.auto_confirm else 'pending'
|
||||
signup.status = new_status
|
||||
signup.save()
|
||||
print(f"活动报名状态已更新: {signup.id} -> {new_status}")
|
||||
# Use the model method if available, otherwise manual logic
|
||||
if hasattr(signup, 'check_payment_status'):
|
||||
signup.check_payment_status()
|
||||
print(f"活动报名状态已更新(check_payment_status): {signup.id} -> {signup.status}")
|
||||
else:
|
||||
new_status = 'confirmed' if signup.activity.auto_confirm else 'pending'
|
||||
signup.status = new_status
|
||||
signup.save()
|
||||
print(f"活动报名状态已更新: {signup.id} -> {new_status}")
|
||||
else:
|
||||
print(f"Error: No ActivitySignup found for paid order {order.id}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user