accrual: Be case-insensitive when gathering payment-method.

This is a little nicer for the user, and lets us simplify surrounding
case-handling code.
This commit is contained in:
Brett Smith 2020-06-23 16:46:31 -04:00
parent 5085d4d8ef
commit fd548f35f4
2 changed files with 18 additions and 15 deletions

View file

@ -482,10 +482,7 @@ class OutgoingReport(BaseReport):
'uswire': 'Domestic Wire', 'uswire': 'Domestic Wire',
'vendorportal': 'Vendor Portal', 'vendorportal': 'Vendor Portal',
} }
PAYMENT_METHOD_RE = re.compile( PAYMENT_METHOD_RE = re.compile(rf'^([a-z]{{3}})\s+({"|".join(PAYMENT_METHODS)})$')
rf'^([A-Z]{{3}})\s+({"|".join(PAYMENT_METHODS)})$',
re.IGNORECASE,
)
def __init__(self, rt_wrapper: rtutil.RT, out_file: TextIO) -> None: def __init__(self, rt_wrapper: rtutil.RT, out_file: TextIO) -> None:
super().__init__(out_file) super().__init__(out_file)
@ -579,20 +576,26 @@ class OutgoingReport(BaseReport):
'payment-amount': payment_amount, 'payment-amount': payment_amount,
'payment-to': payment_to, 'payment-to': payment_to,
} }
payment_methods = posts.meta_values('payment-method') payment_methods = filters.iter_unique(
payment_methods.discard(None) post.meta['payment-method'].lower()
payment_method_count = len(payment_methods) for post in posts
if payment_method_count != 1: if isinstance(post.meta.get('payment-method'), str)
)
payment_method: Optional[str] = next(payment_methods, None)
if payment_method is None:
payment_method_count = "no"
elif next(payment_methods, None) is None:
pass
else:
payment_method_count = "multiple"
payment_method = None
if payment_method is None:
self.logger.warning( self.logger.warning(
"cannot set payment-method for rt:%s: %s metadata values found", "cannot set payment-method for rt:%s: %s metadata values found",
ticket_id, payment_method_count, ticket_id, payment_method_count,
) )
else: else:
payment_method = payment_methods.pop()
if isinstance(payment_method, str):
match = self.PAYMENT_METHOD_RE.fullmatch(payment_method) match = self.PAYMENT_METHOD_RE.fullmatch(payment_method)
else:
match = None
if match is None: if match is None:
self.logger.warning( self.logger.warning(
"cannot set payment-method for rt:%s: invalid value %r", "cannot set payment-method for rt:%s: invalid value %r",
@ -601,7 +604,7 @@ class OutgoingReport(BaseReport):
else: else:
cf_targets['payment-method'] = '{} {}'.format( cf_targets['payment-method'] = '{} {}'.format(
match.group(1).upper(), match.group(1).upper(),
self.PAYMENT_METHODS[match.group(2).lower()], self.PAYMENT_METHODS[match.group(2)],
) )
cf_updates = { cf_updates = {

View file

@ -5,7 +5,7 @@ from setuptools import setup
setup( setup(
name='conservancy_beancount', name='conservancy_beancount',
description="Plugin, library, and reports for reading Conservancy's books", description="Plugin, library, and reports for reading Conservancy's books",
version='1.4.0', version='1.4.1',
author='Software Freedom Conservancy', author='Software Freedom Conservancy',
author_email='info@sfconservancy.org', author_email='info@sfconservancy.org',
license='GNU AGPLv3+', license='GNU AGPLv3+',