@@ -4,6 +4,8 @@ from django.db.models import Q
|
||||
from django.core.paginator import Paginator
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from .models import Card, Game, Set, Cart, CartItem, CardListing, PackListing, VirtualPack, Order, OrderItem, Seller, Bounty, BountyOffer, SellerReport
|
||||
from django.db.models import Sum, Value
|
||||
from django.db.models.functions import Coalesce
|
||||
from .forms import SellerRegistrationForm, CardListingForm, PackListingForm, AddCardListingForm, SellerEditForm, BountyForm, BountyOfferForm, BulkListingForm
|
||||
from django.utils.text import slugify
|
||||
import random
|
||||
@@ -19,6 +21,14 @@ def index(request):
|
||||
def card_list(request):
|
||||
cards = Card.objects.all().select_related('set', 'set__game').prefetch_related('listings')
|
||||
|
||||
# Annotate with total quantity of listed items
|
||||
cards = cards.annotate(
|
||||
total_quantity=Coalesce(
|
||||
Sum('listings__quantity', filter=Q(listings__status='listed')),
|
||||
Value(0)
|
||||
)
|
||||
)
|
||||
|
||||
# Filtering
|
||||
game_slug = request.GET.get('game')
|
||||
if game_slug:
|
||||
@@ -43,7 +53,7 @@ def card_list(request):
|
||||
hide_oos = 'on'
|
||||
|
||||
if hide_oos == 'on':
|
||||
cards = cards.filter(listings__quantity__gt=0, listings__status='listed').distinct()
|
||||
cards = cards.filter(total_quantity__gt=0)
|
||||
|
||||
# Simple logic: only show cards that have listings or show all?
|
||||
# Let's show all for browsing, but indicate stock.
|
||||
|
||||
Reference in New Issue
Block a user