further fixes for card_multiselect breaking when removing all items; values are now static and quanity is stored in data-quantity. Also fix searching via label and change card_multiselect to accept list of cards instead of card_filter
This commit is contained in:
parent
b97ddde71c
commit
3df4b41750
7 changed files with 76 additions and 59 deletions
|
|
@ -4,7 +4,7 @@ from cards.models import Card
|
|||
register = template.Library()
|
||||
|
||||
@register.inclusion_tag('templatetags/card_multiselect.html')
|
||||
def card_multiselect(field_name, label, placeholder, card_filter=None, selected_values=None, cache_timeout=86400, cache_key="available_cards_options"):
|
||||
def card_multiselect(field_name, label, placeholder, cards=None, selected_values=None, cache_timeout=86400, cache_key="cards_multiselect"):
|
||||
"""
|
||||
Renders a multiselect field for choosing cards while supporting quantity data.
|
||||
|
||||
|
|
@ -16,7 +16,6 @@ def card_multiselect(field_name, label, placeholder, card_filter=None, selected_
|
|||
- field_name: The name attribute for the select tag.
|
||||
- label: Label text to show above the selector.
|
||||
- placeholder: Placeholder text to show in the select.
|
||||
- card_filter: (Optional) A dictionary of filter parameters or a QuerySet to obtain the available Card objects.
|
||||
- selected_values: (Optional) A list of selected values; if a value includes a quantity it should be in the format "card_id:quantity".
|
||||
- cache_timeout: (Optional) Cache timeout (in seconds) for the options block.
|
||||
- cache_key: (Optional) Cache key.
|
||||
|
|
@ -28,41 +27,28 @@ def card_multiselect(field_name, label, placeholder, card_filter=None, selected_
|
|||
for val in selected_values:
|
||||
parts = str(val).split(':')
|
||||
card_id = parts[0]
|
||||
quantity = parts[1] if len(parts) > 1 else "1"
|
||||
quantity = parts[1] if len(parts) > 1 else 1
|
||||
selected_cards[card_id] = quantity
|
||||
|
||||
# Determine how to obtain the available cards.
|
||||
# If card_filter is not provided, or is None, fall back to all cards.
|
||||
if card_filter is None:
|
||||
available_cards_qs = Card.objects.all()
|
||||
# If card_filter is a dict, treat it as mapping lookup parameters.
|
||||
elif isinstance(card_filter, dict):
|
||||
available_cards_qs = Card.objects.filter(**card_filter)
|
||||
# Otherwise assume it's already a QuerySet.
|
||||
else:
|
||||
available_cards_qs = card_filter
|
||||
|
||||
available_cards = list(
|
||||
available_cards_qs.order_by("name", "rarity__pk")
|
||||
.select_related("rarity", "cardset")
|
||||
.prefetch_related("decks")
|
||||
)
|
||||
if cards is None:
|
||||
cards = Card.objects.all()
|
||||
|
||||
# Loop through available cards and attach pre‑selected quantity
|
||||
for card in available_cards:
|
||||
for card in cards:
|
||||
pk_str = str(card.pk)
|
||||
if pk_str in selected_cards:
|
||||
card.selected_quantity = selected_cards[pk_str]
|
||||
card.selected = True
|
||||
else:
|
||||
card.selected_quantity = 1
|
||||
card.selected = False
|
||||
|
||||
return {
|
||||
'field_name': field_name,
|
||||
'field_id': field_name, # using the name as id for simplicity
|
||||
'label': label,
|
||||
'available_cards': available_cards,
|
||||
'cards': cards,
|
||||
'placeholder': placeholder,
|
||||
# Pass just the list of pre‑selected card IDs for caching/selection logic in the template.
|
||||
'selected_values': list(selected_cards.keys()),
|
||||
'cache_timeout': cache_timeout,
|
||||
'cache_key': cache_key,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue