From 3df4b417503597fe6f34739abae725f66a8cc04f Mon Sep 17 00:00:00 2001
From: badbl0cks <4161747+badbl0cks@users.noreply.github.com>
Date: Thu, 13 Mar 2025 19:57:32 -0700
Subject: [PATCH] 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
---
cards/templatetags/card_multiselect.py | 30 ++-----
home/views.py | 4 +-
theme/templates/home/home.html | 4 +-
.../templates/trades/trade_offer_create.html | 4 +-
.../templates/trades/trade_offer_search.html | 4 +-
theme/templatetags/card_multiselect.html | 85 +++++++++++++------
trades/views.py | 4 +-
7 files changed, 76 insertions(+), 59 deletions(-)
diff --git a/cards/templatetags/card_multiselect.py b/cards/templatetags/card_multiselect.py
index 854b018..581716d 100644
--- a/cards/templatetags/card_multiselect.py
+++ b/cards/templatetags/card_multiselect.py
@@ -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,
diff --git a/home/views.py b/home/views.py
index 0a3d4c9..a5dd201 100644
--- a/home/views.py
+++ b/home/views.py
@@ -59,8 +59,8 @@ class HomePageView(TemplateView):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
- # Add available_cards QuerySet so card_multiselect works properly.
- context["available_cards"] = Card.objects.all() \
+
+ context["cards"] = Card.objects.all() \
.order_by("name", "rarity__pk") \
.select_related("rarity", "cardset") \
.prefetch_related("decks")
diff --git a/theme/templates/home/home.html b/theme/templates/home/home.html
index 76ad8d1..be9b3bd 100644
--- a/theme/templates/home/home.html
+++ b/theme/templates/home/home.html
@@ -17,10 +17,10 @@
{% csrf_token %}
- {% card_multiselect "have_cards" "I Have:" "Select some cards..." available_cards have_cards %}
+ {% card_multiselect "have_cards" "I Have:" "Select some cards..." cards have_cards %}
- {% card_multiselect "want_cards" "I Want:" "Select some cards..." available_cards want_cards %}
+ {% card_multiselect "want_cards" "I Want:" "Select some cards..." cards want_cards %}
- {% card_multiselect "offered_cards" "Have:" "Select zero or more cards..." available_cards offered_cards %}
+ {% card_multiselect "offered_cards" "Have:" "Select zero or more cards..." cards offered_cards %}
- {% card_multiselect "wanted_cards" "Want:" "Select zero or more cards..." available_cards wanted_cards %}
+ {% card_multiselect "wanted_cards" "Want:" "Select zero or more cards..." cards wanted_cards %}