Fix create trade offer flow and other related bugs
This commit is contained in:
parent
f3a1366269
commit
65ca344582
40 changed files with 867 additions and 278 deletions
|
|
@ -1,45 +1,9 @@
|
|||
from django.core.exceptions import ValidationError
|
||||
from django.db.models.signals import m2m_changed, post_save, post_delete, pre_save
|
||||
from django.db.models.signals import post_save, post_delete, pre_save
|
||||
from django.dispatch import receiver
|
||||
from .models import TradeOffer
|
||||
from cards.models import Card
|
||||
from django.db.models import F
|
||||
from trades.models import TradeOfferHaveCard, TradeOfferWantCard, TradeAcceptance
|
||||
from django.db import transaction
|
||||
|
||||
def validate_and_set_trade_offer_rarity(instance):
|
||||
"""
|
||||
Ensures all cards on both sides share the same rarity and sets the TradeOffer's
|
||||
rarity_level and rarity_icon if they haven't been set already.
|
||||
"""
|
||||
combined_cards = list(instance.have_cards.all()) + list(instance.want_cards.all())
|
||||
if not combined_cards:
|
||||
return
|
||||
|
||||
rarities = {card.rarity_level for card in combined_cards}
|
||||
if len(rarities) > 1:
|
||||
raise ValidationError("All cards in a trade offer must have the same rarity.")
|
||||
|
||||
updated_fields = []
|
||||
if instance.rarity_level is None:
|
||||
instance.rarity_level = combined_cards[0].rarity_level
|
||||
updated_fields.append("rarity_level")
|
||||
if instance.rarity_icon is None:
|
||||
instance.rarity_icon = combined_cards[0].rarity_icon
|
||||
updated_fields.append("rarity_icon")
|
||||
if updated_fields:
|
||||
instance.save(update_fields=updated_fields)
|
||||
|
||||
@receiver(m2m_changed, sender=TradeOffer.have_cards.through)
|
||||
def validate_have_cards_rarity(sender, instance, action, **kwargs):
|
||||
if action == "post_add":
|
||||
transaction.on_commit(lambda: validate_and_set_trade_offer_rarity(instance))
|
||||
|
||||
@receiver(m2m_changed, sender=TradeOffer.want_cards.through)
|
||||
def validate_want_cards_rarity(sender, instance, action, **kwargs):
|
||||
if action == "post_add":
|
||||
transaction.on_commit(lambda: validate_and_set_trade_offer_rarity(instance))
|
||||
|
||||
ACTIVE_STATES = [
|
||||
TradeAcceptance.AcceptanceState.ACCEPTED,
|
||||
TradeAcceptance.AcceptanceState.SENT,
|
||||
|
|
@ -83,40 +47,32 @@ def update_trade_offer_closed_status(trade_offer):
|
|||
trade_offer.is_closed = closed
|
||||
trade_offer.save(update_fields=["is_closed"])
|
||||
|
||||
# Pre-save signal to capture the original state before any changes.
|
||||
@receiver(pre_save, sender=TradeAcceptance)
|
||||
def trade_acceptance_pre_save(sender, instance, **kwargs):
|
||||
if instance.pk:
|
||||
old_instance = TradeAcceptance.objects.get(pk=instance.pk)
|
||||
instance._old_state = old_instance.state
|
||||
|
||||
# Post-save signal to adjust qty_accepted incrementally.
|
||||
@receiver(post_save, sender=TradeAcceptance)
|
||||
def trade_acceptance_post_save(sender, instance, created, **kwargs):
|
||||
delta = 0
|
||||
if created:
|
||||
# For a new acceptance, increment only if the state is active.
|
||||
if instance.state in ACTIVE_STATES:
|
||||
delta = 1
|
||||
else:
|
||||
old_state = getattr(instance, '_old_state', None)
|
||||
if old_state is not None:
|
||||
# Transition from active to non-active (e.g. a rejection)
|
||||
if old_state in ACTIVE_STATES and instance.state not in ACTIVE_STATES:
|
||||
delta = -1
|
||||
# Transition from non-active to active
|
||||
elif old_state not in ACTIVE_STATES and instance.state in ACTIVE_STATES:
|
||||
delta = 1
|
||||
|
||||
if delta != 0:
|
||||
trade_offer = instance.trade_offer
|
||||
# Update the "have" side using the requested_card.
|
||||
adjust_qty_for_trade_offer(trade_offer, instance.requested_card, side='have', delta=delta)
|
||||
# Update the "want" side using the offered_card.
|
||||
adjust_qty_for_trade_offer(trade_offer, instance.offered_card, side='want', delta=delta)
|
||||
update_trade_offer_closed_status(trade_offer)
|
||||
|
||||
# Post-delete signal to decrement qty_accepted if the deleted acceptance was active.
|
||||
@receiver(post_delete, sender=TradeAcceptance)
|
||||
def trade_acceptance_post_delete(sender, instance, **kwargs):
|
||||
if instance.state in ACTIVE_STATES:
|
||||
|
|
@ -124,4 +80,4 @@ def trade_acceptance_post_delete(sender, instance, **kwargs):
|
|||
trade_offer = instance.trade_offer
|
||||
adjust_qty_for_trade_offer(trade_offer, instance.requested_card, side='have', delta=delta)
|
||||
adjust_qty_for_trade_offer(trade_offer, instance.offered_card, side='want', delta=delta)
|
||||
update_trade_offer_closed_status(trade_offer)
|
||||
update_trade_offer_closed_status(trade_offer)
|
||||
Loading…
Add table
Add a link
Reference in a new issue