finished conversion to tailwind
This commit is contained in:
parent
6e2843c60e
commit
d62956d465
50 changed files with 2490 additions and 1273 deletions
|
|
@ -46,11 +46,11 @@ class TradeAcceptanceCreateForm(forms.ModelForm):
|
|||
raise ValueError("friend_codes must be provided")
|
||||
self.fields["accepted_by"].queryset = friend_codes
|
||||
|
||||
# Update active_states to include only states that mean the acceptance is still "open".
|
||||
active_states = [
|
||||
TradeAcceptance.AcceptanceState.ACCEPTED,
|
||||
TradeAcceptance.AcceptanceState.SENT,
|
||||
TradeAcceptance.AcceptanceState.RECEIVED,
|
||||
TradeAcceptance.AcceptanceState.COMPLETED,
|
||||
]
|
||||
|
||||
# Build available requested_card choices from the TradeOffer's "have" side.
|
||||
|
|
@ -83,67 +83,24 @@ class TradeAcceptanceCreateForm(forms.ModelForm):
|
|||
self.instance.trade_offer = self.trade_offer
|
||||
return super().clean()
|
||||
|
||||
class TradeAcceptanceUpdateForm(forms.ModelForm):
|
||||
"""
|
||||
Form for updating the state of an existing TradeAcceptance.
|
||||
Based on the current state and which party is acting (initiator vs. acceptor),
|
||||
this form limits available state transitions.
|
||||
"""
|
||||
class Meta:
|
||||
model = TradeAcceptance
|
||||
fields = ["state"]
|
||||
class ButtonRadioSelect(forms.RadioSelect):
|
||||
template_name = "widgets/button_radio_select.html"
|
||||
|
||||
def __init__(self, *args, friend_codes=None, **kwargs):
|
||||
class TradeAcceptanceTransitionForm(forms.Form):
|
||||
state = forms.ChoiceField(widget=forms.HiddenInput())
|
||||
|
||||
def __init__(self, *args, instance=None, user=None, **kwargs):
|
||||
"""
|
||||
Initializes the form with allowed transitions from the provided instance.
|
||||
:param instance: A TradeAcceptance instance.
|
||||
"""
|
||||
super().__init__(*args, **kwargs)
|
||||
instance = self.instance
|
||||
allowed_choices = []
|
||||
|
||||
# Allowed transitions for a TradeAcceptance:
|
||||
# - From ACCEPTED:
|
||||
# • If the initiator is acting, allow SENT and REJECTED_BY_INITIATOR.
|
||||
# • If the acceptor is acting, allow REJECTED_BY_ACCEPTOR.
|
||||
# - From SENT:
|
||||
# • If the acceptor is acting, allow RECEIVED and REJECTED_BY_ACCEPTOR.
|
||||
# • If the initiator is acting, allow REJECTED_BY_INITIATOR.
|
||||
# - From RECEIVED:
|
||||
# • If the initiator is acting, allow COMPLETED and REJECTED_BY_INITIATOR.
|
||||
# • If the acceptor is acting, allow REJECTED_BY_ACCEPTOR.
|
||||
if friend_codes is None:
|
||||
raise ValueError("friend_codes must be provided")
|
||||
if instance.state == TradeAcceptance.AcceptanceState.ACCEPTED:
|
||||
if instance.trade_offer.initiated_by in friend_codes:
|
||||
allowed_choices = [
|
||||
(TradeAcceptance.AcceptanceState.SENT, "Sent"),
|
||||
(TradeAcceptance.AcceptanceState.REJECTED_BY_INITIATOR, "Rejected by Initiator"),
|
||||
]
|
||||
elif instance.accepted_by in friend_codes:
|
||||
allowed_choices = [
|
||||
(TradeAcceptance.AcceptanceState.REJECTED_BY_ACCEPTOR, "Rejected by Acceptor"),
|
||||
]
|
||||
elif instance.state == TradeAcceptance.AcceptanceState.SENT:
|
||||
if instance.accepted_by in friend_codes:
|
||||
allowed_choices = [
|
||||
(TradeAcceptance.AcceptanceState.RECEIVED, "Received"),
|
||||
(TradeAcceptance.AcceptanceState.REJECTED_BY_ACCEPTOR, "Rejected by Acceptor"),
|
||||
]
|
||||
elif instance.trade_offer.initiated_by in friend_codes:
|
||||
allowed_choices = [
|
||||
(TradeAcceptance.AcceptanceState.REJECTED_BY_INITIATOR, "Rejected by Initiator"),
|
||||
]
|
||||
elif instance.state == TradeAcceptance.AcceptanceState.RECEIVED:
|
||||
if instance.trade_offer.initiated_by in friend_codes:
|
||||
allowed_choices = [
|
||||
(TradeAcceptance.AcceptanceState.COMPLETED, "Completed"),
|
||||
(TradeAcceptance.AcceptanceState.REJECTED_BY_INITIATOR, "Rejected by Initiator"),
|
||||
]
|
||||
elif instance.accepted_by in friend_codes:
|
||||
allowed_choices = [
|
||||
(TradeAcceptance.AcceptanceState.REJECTED_BY_ACCEPTOR, "Rejected by Acceptor"),
|
||||
]
|
||||
if allowed_choices:
|
||||
self.fields["state"].choices = allowed_choices
|
||||
else:
|
||||
self.fields.pop("state")
|
||||
if instance is None:
|
||||
raise ValueError("A TradeAcceptance instance must be provided")
|
||||
self.instance = instance
|
||||
self.user = user
|
||||
|
||||
self.fields["state"].choices = instance.get_allowed_state_transitions(user)
|
||||
|
||||
class TradeOfferCreateForm(ModelForm):
|
||||
# Override the default fields to capture quantity info in the format 'card_id:quantity'
|
||||
|
|
@ -166,10 +123,14 @@ class TradeOfferCreateForm(ModelForm):
|
|||
data = self.data.getlist("have_cards")
|
||||
parsed = {}
|
||||
for item in data:
|
||||
if ':' not in item:
|
||||
# Ignore any input without a colon.
|
||||
continue
|
||||
parts = item.split(':')
|
||||
card_id = parts[0]
|
||||
try:
|
||||
quantity = int(parts[1]) if len(parts) > 1 else 1
|
||||
# Only parse quantity when a colon is present.
|
||||
quantity = int(parts[1])
|
||||
except ValueError:
|
||||
raise forms.ValidationError(f"Invalid quantity provided in {item}")
|
||||
parsed[card_id] = parsed.get(card_id, 0) + quantity
|
||||
|
|
@ -179,10 +140,12 @@ class TradeOfferCreateForm(ModelForm):
|
|||
data = self.data.getlist("want_cards")
|
||||
parsed = {}
|
||||
for item in data:
|
||||
if ':' not in item:
|
||||
continue
|
||||
parts = item.split(':')
|
||||
card_id = parts[0]
|
||||
try:
|
||||
quantity = int(parts[1]) if len(parts) > 1 else 1
|
||||
quantity = int(parts[1])
|
||||
except ValueError:
|
||||
raise forms.ValidationError(f"Invalid quantity provided in {item}")
|
||||
parsed[card_id] = parsed.get(card_id, 0) + quantity
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue