Skip to content

Commit

Permalink
fix: 'Bill Type: Invalid Choice: could not coerce' error
Browse files Browse the repository at this point in the history
Error introduced in #1290. Fixes #1293.
  • Loading branch information
rriski committed Mar 23, 2024
1 parent 4af4c10 commit 483b94c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ihatemoney/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ class BillForm(FlaskForm):
payed_for = SelectMultipleField(
_("For whom?"), validators=[DataRequired()], coerce=int
)
bill_type = SelectField(_("Bill Type"), choices=BillType.choices(), coerce=BillType, default=BillType.EXPENSE)
bill_type = SelectField(_("Bill Type"), choices=BillType.choices(), coerce=BillType.coerce, default=BillType.EXPENSE)
submit = SubmitField(_("Submit"))
submit2 = SubmitField(_("Submit and add a new one"))

Expand Down
11 changes: 11 additions & 0 deletions ihatemoney/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,21 @@ class BillType(Enum):
EXPENSE = "Expense"
REIMBURSEMENT = "Reimbursement"

def __str__(self):
return self.value

@classmethod
def choices(cls):
return [(choice, choice.value) for choice in cls]

@classmethod
def coerce(cls, item):
try:
return item if isinstance(item, BillType) else BillType[item.upper()]
except KeyError:
raise ValueError(f"Invalid bill type: {item}")



db = SQLAlchemy()

Expand Down

0 comments on commit 483b94c

Please sign in to comment.