Some optimizations to trade_offers to reduce loading times
This commit is contained in:
parent
0ac8ac8d5c
commit
9ce5d525b3
13 changed files with 255 additions and 222 deletions
|
|
@ -29,31 +29,38 @@ class TradeAcceptanceCreateForm(forms.ModelForm):
|
|||
Expects the caller to pass:
|
||||
- trade_offer: the instance of TradeOffer this acceptance is for.
|
||||
- friend_codes: a queryset of FriendCode objects for the current user.
|
||||
- default_friend_code (optional): the user's default FriendCode.
|
||||
It filters available requested and offered cards based on what's still available.
|
||||
"""
|
||||
class Meta:
|
||||
model = TradeAcceptance
|
||||
fields = ["accepted_by", "requested_card", "offered_card"]
|
||||
|
||||
def __init__(self, *args, trade_offer=None, friend_codes=None, **kwargs):
|
||||
def __init__(self, *args, trade_offer=None, friend_codes=None, default_friend_code=None, **kwargs):
|
||||
if trade_offer is None:
|
||||
raise ValueError("trade_offer must be provided to filter choices.")
|
||||
super().__init__(*args, **kwargs)
|
||||
self.trade_offer = trade_offer
|
||||
|
||||
# Filter accepted_by to those friend codes that belong to the current user.
|
||||
if friend_codes is None:
|
||||
raise ValueError("friend_codes must be provided")
|
||||
# Set the accepted_by queryset to the user's friend codes.
|
||||
self.fields["accepted_by"].queryset = friend_codes
|
||||
|
||||
# Update active_states to include only states that mean the acceptance is still "open".
|
||||
# If the user only has one friend code, preset the field and use a HiddenInput.
|
||||
if friend_codes.count() == 1:
|
||||
self.initial["accepted_by"] = friend_codes.first().pk
|
||||
self.fields["accepted_by"].widget = forms.HiddenInput()
|
||||
# Otherwise, if a default friend code is provided and it is in the queryset, preselect it.
|
||||
elif default_friend_code and friend_codes.filter(pk=default_friend_code.pk).exists():
|
||||
self.initial["accepted_by"] = default_friend_code.pk
|
||||
|
||||
# Update available requested_card choices from the TradeOffer's "have" side.
|
||||
active_states = [
|
||||
TradeAcceptance.AcceptanceState.ACCEPTED,
|
||||
TradeAcceptance.AcceptanceState.SENT,
|
||||
TradeAcceptance.AcceptanceState.RECEIVED,
|
||||
]
|
||||
|
||||
# Build available requested_card choices from the TradeOffer's "have" side.
|
||||
available_requested_ids = []
|
||||
for through_obj in trade_offer.trade_offer_have_cards.all():
|
||||
active_count = trade_offer.acceptances.filter(
|
||||
|
|
@ -64,7 +71,7 @@ class TradeAcceptanceCreateForm(forms.ModelForm):
|
|||
available_requested_ids.append(through_obj.card.id)
|
||||
self.fields["requested_card"].queryset = Card.objects.filter(id__in=available_requested_ids)
|
||||
|
||||
# Similarly, build available offered_card choices from the TradeOffer's "want" side.
|
||||
# Update available offered_card choices from the TradeOffer's "want" side.
|
||||
available_offered_ids = []
|
||||
for through_obj in trade_offer.trade_offer_want_cards.all():
|
||||
active_count = trade_offer.acceptances.filter(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue