Make PartialTXN flag a string

This commit is contained in:
2026-01-05 19:39:39 +10:00
parent c7d32520c4
commit 3b37c62f17
4 changed files with 76 additions and 59 deletions

View File

@@ -16,6 +16,7 @@ class PartialPosting(ABCPosting):
"""
Partial posting object, potentially without an account name.
"""
account: Optional[str] = None
def upgrade(self, default_account: Optional[str] = None) -> TXNPosting:
@@ -30,7 +31,7 @@ class PartialPosting(ABCPosting):
total_cost=self.total_cost,
price=self.price,
flag=self.flag,
comment=self.comment
comment=self.comment,
)
@property
@@ -54,14 +55,15 @@ class PartialTXN:
TODO: REPR
"""
date: dt.date
flag: TXNFlag = TXNFlag.INCOMPLETE
payee: str = ''
narration: str = ''
flag: str = TXNFlag.INCOMPLETE.value
payee: str = ""
narration: str = ""
comment: Optional[str] = None
document: Optional[str] = None
tags: str = ''
links: str = ''
tags: str = ""
links: str = ""
source_posting: PartialPosting
source_fee_asset_posting: Optional[PartialPosting] = None
source_fee_expense_posting: Optional[PartialPosting] = None
@@ -71,25 +73,25 @@ class PartialTXN:
# Exposing set of fields which may be updated (e.g. from rules)
# Map field name -> display name
fields = {
'flag': 'Flag',
'payee': 'Payee',
'narration': 'Narration',
'comment': "Comment",
'document': "Document",
'tags': "Tags",
'links': "Links",
'source_account': "Source Account",
'source_fee_asset_account': "Source Fee Asset Account",
'source_fee_expense_account': "Source Fee Expense Account",
'target_account': "Target Account",
'target_fee_expense_account': "Target Fee Expense Account",
"flag": "Flag",
"payee": "Payee",
"narration": "Narration",
"comment": "Comment",
"document": "Document",
"tags": "Tags",
"links": "Links",
"source_account": "Source Account",
"source_fee_asset_account": "Source Fee Asset Account",
"source_fee_expense_account": "Source Fee Expense Account",
"target_account": "Target Account",
"target_fee_expense_account": "Target Fee Expense Account",
}
posting_fields = {
'source_posting': 'source_account',
'source_fee_asset_posting': 'source_fee_asset_account',
'source_fee_expense_posting': 'source_fee_expense_account',
'target_posting': 'target_account',
'target_fee_expense_posting': 'target_fee_expense_account',
"source_posting": "source_account",
"source_fee_asset_posting": "source_fee_asset_account",
"source_fee_expense_posting": "source_fee_expense_account",
"target_posting": "target_account",
"target_fee_expense_posting": "target_fee_expense_account",
}
@property
@@ -118,7 +120,9 @@ class PartialTXN:
if (posting := self.source_fee_asset_posting) is not None:
posting.account = value
else:
raise ValueError("This TXN does not have a source fee asset posting to set.")
raise ValueError(
"This TXN does not have a source fee asset posting to set."
)
@property
def source_fee_expense_account(self):
@@ -130,7 +134,9 @@ class PartialTXN:
if (posting := self.source_fee_expense_posting) is not None:
posting.account = value
else:
raise ValueError("This TXN does not have a source fee expense posting to set.")
raise ValueError(
"This TXN does not have a source fee expense posting to set."
)
@property
def target_fee_expense_account(self):
@@ -142,7 +148,9 @@ class PartialTXN:
if (posting := self.target_fee_expense_posting) is not None:
posting.account = value
else:
raise ValueError("This TXN does not have a target fee expense posting to set.")
raise ValueError(
"This TXN does not have a target fee expense posting to set."
)
@property
def postings(self):
@@ -180,7 +188,7 @@ class PartialTXN:
we_dont_have = set(self.posting_fields.keys()).difference(we_have)
with_defaults.update(
overwrite=False,
**{k: v for k, v in defaults.items() if k not in we_dont_have}
**{k: v for k, v in defaults.items() if k not in we_dont_have},
)
upgraded = with_defaults.upgrade()
elif self.partial:
@@ -188,14 +196,14 @@ class PartialTXN:
else:
upgraded = Transaction(
date=self.date,
flag=self.flag,
flag=TXNFlag(self.flag),
payee=self.payee,
narration=self.narration,
comments=[self.comment] if self.comment else [],
document=[self.document] if self.document else [],
tags=self.tags.split(),
links=self.links.split(),
postings=[p.upgrade() for p in self.postings.values()]
postings=[p.upgrade() for p in self.postings.values()],
)
return upgraded
@@ -219,17 +227,16 @@ class PartialTXN:
# Don't include posting accounts which aren't there
continue
match name:
case 'flag':
value = self.flag.value
case "flag":
value = self.flag
case _:
value = getattr(self, name)
value = str(value) if value is not None else ''
fields.append(TXNField(
name=name,
display_name=display_name,
value=value,
matchable=True
))
value = str(value) if value is not None else ""
fields.append(
TXNField(
name=name, display_name=display_name, value=value, matchable=True
)
)
return fields
@@ -243,26 +250,26 @@ class PartialTXN:
userstr = userstr.strip()
# TODO: Each of these cases needs custom validation
match name:
case 'flag':
if userstr == '!':
updater['flag'] = TXNFlag.INCOMPLETE
elif userstr == '*':
updater['flag'] = TXNFlag.COMPLETE
case "flag":
if userstr == "!":
updater["flag"] = TXNFlag.INCOMPLETE.value
elif userstr == "*":
updater["flag"] = TXNFlag.COMPLETE.value
else:
raise UserInputError(
"Transaction flag must be either '*' or '!'"
)
case 'payee' | 'narration' | 'tags' | 'links':
case "payee" | "narration" | "tags" | "links":
updater[name] = userstr
case 'comment' | 'document':
updater[name] = userstr or None
case 'source_account' | 'target_account':
case "comment" | "document":
updater[name] = userstr or None
case "source_account" | "target_account":
updater[name] = userstr
case 'source_fee_asset_account':
case "source_fee_asset_account":
updater[name] = userstr
case 'source_fee_expense_account':
case "source_fee_expense_account":
updater[name] = userstr
case 'target_fee_expense_account':
case "target_fee_expense_account":
updater[name] = userstr
case _:
raise ValueError(f"Unknown field {name} passed to TXN parser.")

View File

@@ -110,10 +110,16 @@ class CBAConverter(Converter[CBARecord, CBAConfig]):
args = {}
args["date"] = record.date
if "flag" in fields:
args["flag"] = TXNFlag(fields["flag"])
for name in {"payee", "narration", "comment", "document", "tags", "links"}:
for name in {
"payee",
"narration",
"comment",
"document",
"tags",
"links",
"flag",
}:
if name in fields:
args[name] = fields[name]

View File

@@ -254,12 +254,16 @@ class WiseConverter(Converter[WiseRecord, WiseConfig]):
args = {}
args["date"] = record.date
# Convert string flag if it exists
if "flag" in fields:
args["flag"] = TXNFlag(fields["flag"])
# Copy string fields over directly
for name in {"payee", "narration", "comment", "document", "tags", "links"}:
for name in {
"payee",
"narration",
"comment",
"document",
"tags",
"links",
"flag",
}:
if name in fields:
args[name] = fields[name]

View File

@@ -266,7 +266,7 @@ class RowTree(ttk.Frame):
for col in self.columns:
match col.split(".", maxsplit=1):
case ["txn", "flag"]:
value = txn.flag.value
value = txn.flag
case ["txn", field]:
value = getattr(txn, field)
case ["record", "date"]: