From 17d8d5d81347b41220b771ae71b411ccc621c85b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Murta?= Date: Sat, 10 Dec 2022 18:58:03 +0000 Subject: [PATCH] [Fix] Changes rule's money type to Decimal Also fixes comparison w/ max amount. --- .../e36e6321568e_rules_min_max_money.py | 58 +++++++++++++++++++ pfbudget/cli/runnable.py | 5 +- pfbudget/core/categorizer.py | 2 +- pfbudget/db/model.py | 6 +- 4 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 alembic/versions/e36e6321568e_rules_min_max_money.py diff --git a/alembic/versions/e36e6321568e_rules_min_max_money.py b/alembic/versions/e36e6321568e_rules_min_max_money.py new file mode 100644 index 0000000..e69c89e --- /dev/null +++ b/alembic/versions/e36e6321568e_rules_min_max_money.py @@ -0,0 +1,58 @@ +"""Rules min/max money + +Revision ID: e36e6321568e +Revises: 0ce89e987770 +Create Date: 2022-12-10 18:55:07.149010+00:00 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = "e36e6321568e" +down_revision = "0ce89e987770" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.alter_column( + "categories_rules", + "min_amount", + existing_type=sa.DOUBLE_PRECISION(precision=53), + type_=sa.Numeric(precision=16, scale=2), + existing_nullable=True, + schema="transactions", + ) + op.alter_column( + "categories_rules", + "max_amount", + existing_type=sa.DOUBLE_PRECISION(precision=53), + type_=sa.Numeric(precision=16, scale=2), + existing_nullable=True, + schema="transactions", + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.alter_column( + "categories_rules", + "max_amount", + existing_type=sa.Numeric(precision=16, scale=2), + type_=sa.DOUBLE_PRECISION(precision=53), + existing_nullable=True, + schema="transactions", + ) + op.alter_column( + "categories_rules", + "min_amount", + existing_type=sa.Numeric(precision=16, scale=2), + type_=sa.DOUBLE_PRECISION(precision=53), + existing_nullable=True, + schema="transactions", + ) + # ### end Alembic commands ### diff --git a/pfbudget/cli/runnable.py b/pfbudget/cli/runnable.py index cdf2a94..ebe2504 100644 --- a/pfbudget/cli/runnable.py +++ b/pfbudget/cli/runnable.py @@ -1,6 +1,7 @@ from pathlib import Path import argparse import datetime as dt +import decimal import re from pfbudget.common.types import Operation @@ -369,8 +370,8 @@ def category_rule(parser: argparse.ArgumentParser, universal: argparse.ArgumentP add.add_argument("--description", nargs=1, type=str) add.add_argument("--regex", nargs=1, type=str) add.add_argument("--bank", nargs=1, type=str) - add.add_argument("--min", nargs=1, type=float) - add.add_argument("--max", nargs=1, type=float) + add.add_argument("--min", nargs=1, type=decimal.Decimal) + add.add_argument("--max", nargs=1, type=decimal.Decimal) remove = commands.add_parser("remove", parents=[universal]) remove.set_defaults(op=Operation.RuleRemove) diff --git a/pfbudget/core/categorizer.py b/pfbudget/core/categorizer.py index 15ac74d..674c1aa 100644 --- a/pfbudget/core/categorizer.py +++ b/pfbudget/core/categorizer.py @@ -79,7 +79,7 @@ class Categorizer: if rule.min_amount > transaction.amount: continue if rule.max_amount: - if rule.max_amount <= transaction.amount: + if rule.max_amount < transaction.amount: continue # passed all conditions, assign category diff --git a/pfbudget/db/model.py b/pfbudget/db/model.py index e27521b..0a76846 100644 --- a/pfbudget/db/model.py +++ b/pfbudget/db/model.py @@ -69,7 +69,7 @@ class Bank(Base): bankfk = Annotated[str, mapped_column(Text, ForeignKey(Bank.name))] idpk = Annotated[int, mapped_column(BigInteger, primary_key=True)] -money = Annotated[Decimal, mapped_column(Numeric(16, 2), nullable=False)] +money = Annotated[Decimal, mapped_column(Numeric(16, 2))] class Transaction(Base): @@ -183,8 +183,8 @@ class CategoryRule(Base): description: Mapped[Optional[str]] regex: Mapped[Optional[str]] bank: Mapped[Optional[str]] - min_amount: Mapped[Optional[float]] - max_amount: Mapped[Optional[float]] + min_amount: Mapped[Optional[money]] + max_amount: Mapped[Optional[money]] def __hash__(self): return hash(self.id)