这是我想使用 update_or_create 逻辑编写的代码,并希望删除 transaction.atomic 并选择更新。记住 async_handle_invoice_payment_failed 和 async_handle_invoice_payment_action_required 将被多次触发,但对于这些特定值,我只希望 failed_payment.first_payment_action_required_triggered_at 和 failed_payment.first_Payment_failure_triggered_at 的值仅在它们首次创建时或代码中使用的逻辑。
这是我的模特
class FailedPaymentLog(TimeStampedModel):
account = models.ForeignKey(Account, on_delete=models.CASCADE)
invoice_id = models.CharField(max_length=255)
retry_count = models.PositiveIntegerField(default=0)
last_retry_time = models.DateTimeField(null=True, blank=True)
first_Payment_failure_triggered_at = models.DateTimeField(null=True, blank=True) #this is the time when the payment of invoice failed for the first time
first_payment_action_required_triggered_at = models.DateTimeField(null=True, blank=True)
class Meta:
indexes = [
models.Index(fields=['account', 'invoice_id'], name='account_invoice_idx'),
]
这是我要更改的代码-:
@transaction.atomic()
def update_failed_payment_log(event, action_required=False):
invoice = event['data']['object']
stripe_customer_id = invoice['customer']
account_billing = AccountBilling.objects.get(stripe_customer_id=stripe_customer_id)
account = account_billing.account
invoice_id = invoice['id']
try:
subscription = Subscription.objects.filter(payment_identifier=event['data']['object']['subscription']).first()
subscription.payment_status = db_choices.BillingPaymentStatusChoices.declined
subscription.save()
except Subscription.DoesNotExist:
return
failed_payment, created = FailedPaymentLog.objects.select_for_update().get_or_create(account=account, invoice_id=invoice_id)
failed_payment.retry_count = invoice['attempt_count']
failed_payment.last_retry_time = timezone.make_aware(timezone.datetime.fromtimestamp(event['created']))
if created:
if action_required:
failed_payment.first_payment_action_required_triggered_at = timezone.make_aware(timezone.datetime.fromtimestamp(event['created']))
else:
failed_payment.first_Payment_failure_triggered_at = timezone.make_aware(timezone.datetime.fromtimestamp(event['created']))
else:
if action_required and not failed_payment.first_payment_action_required_triggered_at:
failed_payment.first_payment_action_required_triggered_at = timezone.make_aware(timezone.datetime.fromtimestamp(event['created']))
elif not action_required and not failed_payment.first_Payment_failure_triggered_at :
failed_payment.first_Payment_failure_triggered_at = timezone.make_aware(timezone.datetime.fromtimestamp(event['created']))
failed_payment.save()
@shared_task
def async_handle_invoice_payment_failed(event):
update_failed_payment_log(event)
@shared_task
def async_handle_invoice_payment_action_required(event):
update_failed_payment_log(event, action_required=True)
期待你按照这个写逻辑 我想使用 update_or_create 逻辑编写它,并想删除 transaction.atomic() 并选择更新。记住 async_handle_invoice_payment_failed 和 async_handle_invoice_payment_action_required 将被多次触发,但对于这些特定值,我只希望 failed_payment.first_payment_action_required_triggered_at 和 failed_payment.first_Payment_failure_triggered_at 的值仅在它们首次创建时或代码中使用的逻辑时出现。很多代码是重复的,你能重构它吗?