From 483b94cd1afd72f1a34e8db411d7afa2ab297d26 Mon Sep 17 00:00:00 2001 From: Timo Riski Date: Sat, 23 Mar 2024 16:49:42 +0200 Subject: [PATCH] fix: 'Bill Type: Invalid Choice: could not coerce' error Error introduced in #1290. Fixes #1293. --- ihatemoney/forms.py | 2 +- ihatemoney/models.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/ihatemoney/forms.py b/ihatemoney/forms.py index 6a787e214..dd7c85463 100644 --- a/ihatemoney/forms.py +++ b/ihatemoney/forms.py @@ -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")) diff --git a/ihatemoney/models.py b/ihatemoney/models.py index fcc172647..f3e283a82 100644 --- a/ihatemoney/models.py +++ b/ihatemoney/models.py @@ -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()