We've long supported skipping documentation checks by flagging the transaction. We haven't done the same for enumerated metadata because we need it less often, and bad values tend to do more damage to reports. However, occasionally when something very off-process happens, we do need it as a matter of expediency. So support it. In order to skip validation of these fields, the plugin requires that the value start with the string "FIXME". This helps ensure that reports have a consistent way to detect and warn about unfilled values in flagged transactions.
43 lines
1.5 KiB
Python
43 lines
1.5 KiB
Python
"""meta_paypal_id - Validate paypal-id metadata"""
|
|
# Copyright © 2020 Brett Smith
|
|
# License: AGPLv3-or-later WITH Beancount-Plugin-Additional-Permission-1.0
|
|
#
|
|
# Full copyright and licensing details can be found at toplevel file
|
|
# LICENSE.txt in the repository.
|
|
|
|
import re
|
|
|
|
from . import core
|
|
from .. import data
|
|
from .. import errors as errormod
|
|
from ..beancount_types import (
|
|
Transaction,
|
|
)
|
|
|
|
class MetaPayPalID(core._PostingHook):
|
|
METADATA_KEY = 'paypal-id'
|
|
HOOK_GROUPS = frozenset(['metadata', METADATA_KEY])
|
|
TXN_ID_RE = re.compile(r'^[A-Z0-9]{17}$')
|
|
INVOICE_ID_RE = re.compile(r'^INV2(?:-[A-Z0-9]{4}){4}$')
|
|
|
|
def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool:
|
|
if post.account.is_under('Assets:PayPal'):
|
|
return True
|
|
elif post.account.is_under('Assets:Receivable'):
|
|
return self.METADATA_KEY in post.meta
|
|
else:
|
|
return False
|
|
|
|
def post_run(self, txn: Transaction, post: data.Posting) -> errormod.Iter:
|
|
if post.account.is_under('Assets:Receivable'):
|
|
regexp = self.INVOICE_ID_RE
|
|
else:
|
|
regexp = self.TXN_ID_RE
|
|
value = post.meta.get(self.METADATA_KEY)
|
|
try:
|
|
# A bad argument type is okay because we catch the TypeError.
|
|
match = regexp.match(value) # type:ignore[arg-type]
|
|
except TypeError:
|
|
match = None
|
|
if match is None and not self._is_flagged_fixme(post, value):
|
|
yield errormod.InvalidMetadataError(txn, self.METADATA_KEY, value, post)
|