Initial working version with minor bugs

This commit is contained in:
badblocks 2025-02-26 00:06:42 -08:00
parent f946e4933a
commit 71b3993326
83 changed files with 34485 additions and 173 deletions

25
trades/signals.py Normal file
View file

@ -0,0 +1,25 @@
from django.core.exceptions import ValidationError
from django.db.models.signals import m2m_changed
from django.dispatch import receiver
from .models import TradeOffer
from cards.models import Card
def check_trade_offer_rarity(instance):
combined_cards = list(instance.have_cards.all()) + list(instance.want_cards.all())
# Map rarities 6 (Super Rare) and 7 (Special Art Rare) to a single value (here, 6)
rarities = {
card.rarity_id if card.rarity_id not in (6, 7) else 6
for card in combined_cards
}
if len(rarities) > 1:
raise ValidationError("All cards in a trade offer must have the same rarity.")
@receiver(m2m_changed, sender=TradeOffer.have_cards.through)
def validate_have_cards_rarity(sender, instance, action, **kwargs):
if action == "post_add":
check_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":
check_trade_offer_rarity(instance)