Skip to content

Commit

Permalink
Deck creation from import now merges the same card if encountered mul…
Browse files Browse the repository at this point in the history
…tiple times
  • Loading branch information
Ajordat committed Jun 15, 2024
1 parent 1409730 commit 3817d63
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions alteredbuilder/decks/deck_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django.contrib.auth.models import User
from django.db import transaction
from django.db.models import F
from django.utils.translation import gettext_lazy as _

from .game_modes import DraftGameMode, GameMode, StandardGameMode, update_deck_legality
Expand Down Expand Up @@ -74,8 +75,14 @@ def create_new_deck(user: User, deck_form: dict) -> Deck:
_("Multiple heroes present in the decklist")
)
else:
# Link the Card with the Deck
CardInDeck.objects.create(deck=deck, card=card, quantity=count)
if CardInDeck.objects.filter(deck=deck, card=card).exists():
# If the card is already linked to the deck, increase its quantity
cid = CardInDeck.objects.get(deck=deck, card=card)
cid.quantity = F("quantity") + count
cid.save(update_fields=["quantity"])
else:
# Link the Card with the Deck
CardInDeck.objects.create(deck=deck, card=card, quantity=count)

update_deck_legality(deck)
deck.save()
Expand Down

0 comments on commit 3817d63

Please sign in to comment.