fix card_multiselect filtering and quantity controls

This commit is contained in:
badblocks 2025-03-13 15:48:26 -07:00
parent 6e4c6040bd
commit b97ddde71c
52 changed files with 1689 additions and 2268 deletions

View file

@ -90,23 +90,11 @@ class TradeOfferAllListView(ListView):
)
)
.order_by("-updated_at")
.annotate(
is_active=Case(
When(
manually_closed=False,
total_have_quantity__gt=F('total_have_accepted'),
total_want_quantity__gt=F('total_want_accepted'),
then=Value(True)
),
default=Value(False),
output_field=BooleanField()
)
)
)
if show_closed:
queryset = queryset.filter(is_active=False)
queryset = queryset.filter(is_closed=True)
else:
queryset = queryset.filter(is_active=True)
queryset = queryset.filter(is_closed=False)
offers_page = request.GET.get("offers_page")
offers_paginator = Paginator(queryset, 10)
@ -135,18 +123,6 @@ class TradeOfferMyListView(LoginRequiredMixin, ListView):
)
)
.order_by("-updated_at")
.annotate(
is_active=Case(
When(
manually_closed=False,
total_have_quantity__gt=F('total_have_accepted'),
total_want_quantity__gt=F('total_want_accepted'),
then=Value(True)
),
default=Value(False),
output_field=BooleanField()
)
)
)
def get_context_data(self, **kwargs):
@ -173,9 +149,9 @@ class TradeOfferMyListView(LoginRequiredMixin, ListView):
queryset = self.get_queryset().filter(initiated_by=selected_friend_code)
if show_closed:
queryset = queryset.filter(is_active=False)
queryset = queryset.filter(is_closed=True)
else:
queryset = queryset.filter(is_active=True)
queryset = queryset.filter(is_closed=False)
offers_page = request.GET.get("offers_page")
offers_paginator = Paginator(queryset, 10)
@ -267,8 +243,8 @@ class TradeOfferDeleteView(LoginRequiredMixin, DeleteView):
return self.render_to_response(context)
else:
if trade_offer.acceptances.count() > 0:
trade_offer.manually_closed = True
trade_offer.save(update_fields=["manually_closed"])
trade_offer.is_closed = True
trade_offer.save(update_fields=["is_closed"])
messages.success(request, "Trade offer has been marked as closed.")
return HttpResponseRedirect(self.get_success_url())
else:
@ -330,9 +306,7 @@ class TradeOfferSearchView(LoginRequiredMixin, ListView):
return TradeOffer.objects.none()
qs = TradeOffer.objects.filter(
manually_closed=False,
total_have_accepted__lt=F("total_have_quantity"),
total_want_accepted__lt=F("total_want_quantity")
is_closed=False,
).exclude(initiated_by__in=self.request.user.friend_codes.all())
# Chain filters for offered selections (i.e. the user "has" cards).
@ -408,13 +382,6 @@ class TradeOfferDetailView(LoginRequiredMixin, DetailView):
)
)
)
.annotate(
is_active=Case(
When(manually_closed=False, then=Value(True)),
default=Value(False),
output_field=BooleanField()
)
)
)
def get_context_data(self, **kwargs):
@ -436,9 +403,7 @@ class TradeOfferDetailView(LoginRequiredMixin, DetailView):
# Option 1: Filter active acceptances using the queryset lookup.
context["active_acceptances"] = trade_offer.acceptances.exclude(state__in=terminal_states)
# Option 2: Or filter using the computed property (if you prefer to work with Python iterables):
# context["active_acceptances"] = [acc for acc in trade_offer.acceptances.all() if acc.is_active]
user_friend_codes = self.request.user.friend_codes.all()
# Add context flag and deletion URL if the current user is the initiator
@ -466,7 +431,7 @@ class TradeAcceptanceCreateView(LoginRequiredMixin, CreateView):
def dispatch(self, request, *args, **kwargs):
self.trade_offer = self.get_trade_offer()
if self.trade_offer.initiated_by_id in request.user.friend_codes.values_list("id", flat=True) or not self.trade_offer.is_active:
if self.trade_offer.initiated_by_id in request.user.friend_codes.values_list("id", flat=True) or self.trade_offer.is_closed:
raise PermissionDenied("You cannot accept this trade offer.")
if not request.user.friend_codes.exists():
raise PermissionDenied("No friend codes available for your account.")
@ -485,13 +450,6 @@ class TradeAcceptanceCreateView(LoginRequiredMixin, CreateView):
)
)
)
.annotate(
is_active=Case(
When(manually_closed=False, then=Value(True)),
default=Value(False),
output_field=BooleanField()
)
)
.get(pk=self.kwargs['offer_pk'])
)
@ -520,10 +478,11 @@ class TradeAcceptanceUpdateView(LoginRequiredMixin, UpdateView):
def dispatch(self, request, *args, **kwargs):
self.object = self.get_object()
if self.object.accepted_by_id not in request.user.friend_codes.values_list("id", flat=True):
raise PermissionDenied("You are not authorized to update this acceptance.")
if not request.user.friend_codes.exists():
raise PermissionDenied("No friend codes available for your account.")
friend_codes = request.user.friend_codes.values_list("id", flat=True)
if self.object.accepted_by_id not in friend_codes and self.object.trade_offer.initiated_by_id not in friend_codes:
raise PermissionDenied("You are not authorized to update this acceptance.")
return super().dispatch(request, *args, **kwargs)
def get_form_kwargs(self):