brightfunds: Add importer for new CSV format.
This commit is contained in:
parent
347203b5f4
commit
c13ddd1274
3 changed files with 86 additions and 27 deletions
|
@ -1,40 +1,68 @@
|
||||||
|
import collections
|
||||||
|
|
||||||
|
from . import _csv
|
||||||
from . import _xls
|
from . import _xls
|
||||||
from .. import strparse
|
from .. import strparse
|
||||||
|
|
||||||
class DonorReportImporter(_xls.XLSImporterBase):
|
class _BrightFundsMixin:
|
||||||
BOOK_KWARGS = {'encoding_override': 'utf-8'}
|
DATE_FMT = '%m/%d/%y'
|
||||||
ENTRY_SEED = {'currency': 'USD'}
|
ENTRY_SEED = {'currency': 'USD'}
|
||||||
NEEDED_FIELDS = frozenset(['Created', 'Amount'])
|
NAME_KEYS = ['company_name', 'donor_name', 'on_behalf_of']
|
||||||
COPIED_FIELDS = {
|
|
||||||
'Company Name': 'company_name',
|
|
||||||
'Donor Name': 'donor_name',
|
|
||||||
'Donor Email': 'donor_email',
|
|
||||||
'On Behalf Of': 'on_behalf_of',
|
|
||||||
'Designation': 'designation',
|
|
||||||
'Fund': 'fund',
|
|
||||||
'Type': 'type',
|
|
||||||
}
|
|
||||||
|
|
||||||
def _cell_is_blank(self, value):
|
DATE_FIELD = 'Date'
|
||||||
return value == '-' or not value
|
DONOR_FIELD = 'Name'
|
||||||
|
EMAIL_FIELD = 'Email'
|
||||||
|
TYPE_FIELD = 'Donation Type'
|
||||||
|
|
||||||
|
def __init_subclass__(cls):
|
||||||
|
cls.FIELDS_TO_COPY = {
|
||||||
|
'Company Name': 'company_name',
|
||||||
|
'On Behalf Of': 'on_behalf_of',
|
||||||
|
'Designation': 'designation',
|
||||||
|
'Fund': 'fund',
|
||||||
|
cls.DONOR_FIELD: 'donor_name',
|
||||||
|
cls.EMAIL_FIELD: 'donor_email',
|
||||||
|
cls.TYPE_FIELD: 'type',
|
||||||
|
}
|
||||||
|
cls.NEEDED_FIELDS = frozenset(['Amount', cls.DATE_FIELD, *cls.FIELDS_TO_COPY])
|
||||||
|
|
||||||
|
def _get_value(self, value):
|
||||||
|
return '' if (value == '-' or not value) else value
|
||||||
|
|
||||||
def _read_row(self, row):
|
def _read_row(self, row):
|
||||||
if any(self._cell_is_blank(row[key]) for key in self.NEEDED_FIELDS):
|
if not (row['Amount'] and row[self.DATE_FIELD]):
|
||||||
return None
|
return None
|
||||||
names = [row[key] for key in ['Company Name', 'Donor Name', 'On Behalf Of']
|
entry_data = {
|
||||||
if not self._cell_is_blank(row[key])]
|
entry_key: self._get_value(row[row_key])
|
||||||
|
for row_key, entry_key in self.FIELDS_TO_COPY.items()
|
||||||
|
}
|
||||||
|
print(entry_data, row['Amount'])
|
||||||
|
names = []
|
||||||
|
for key in self.NAME_KEYS:
|
||||||
|
name = entry_data[key].strip()
|
||||||
|
entry_data[key] = name
|
||||||
|
if name:
|
||||||
|
names.append(name)
|
||||||
try:
|
try:
|
||||||
corporation, payee, *_ = names
|
corporation, payee, *_ = names
|
||||||
except ValueError:
|
except ValueError:
|
||||||
corporation = names[0]
|
corporation = names[0]
|
||||||
payee = corporation
|
payee = corporation
|
||||||
entry_data = {
|
entry_data['amount'] = str(row['Amount'])
|
||||||
'amount': '{:.2f}'.format(row['Amount']),
|
entry_data['corporation'] = corporation
|
||||||
'corporation': corporation,
|
entry_data['date'] = strparse.date(row[self.DATE_FIELD], self.DATE_FMT)
|
||||||
'date': strparse.date(row['Created'], '%m/%d/%Y'),
|
entry_data['payee'] = payee
|
||||||
'payee': payee,
|
|
||||||
}
|
|
||||||
entry_data.update((entry_key, '')
|
|
||||||
for row_key, entry_key in self.COPIED_FIELDS.items()
|
|
||||||
if self._cell_is_blank(row[row_key]))
|
|
||||||
return entry_data
|
return entry_data
|
||||||
|
|
||||||
|
|
||||||
|
class DonorReportImporter(_BrightFundsMixin, _csv.CSVImporterBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class DonorReportXLSImporter(_BrightFundsMixin, _xls.XLSImporterBase):
|
||||||
|
BOOK_KWARGS = {'encoding_override': 'utf-8'}
|
||||||
|
DATE_FMT = '%m/%d/%Y'
|
||||||
|
DATE_FIELD = 'Created'
|
||||||
|
DONOR_FIELD = 'Donor Name'
|
||||||
|
EMAIL_FIELD = 'Donor Email'
|
||||||
|
TYPE_FIELD = 'Type'
|
||||||
|
|
3
tests/data/BrightFunds.csv
Normal file
3
tests/data/BrightFunds.csv
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
Date,Disbursement ID,Name,Fund,Amount,Designation,On Behalf Of,Email,Company Name,Donation Type
|
||||||
|
10/01/20,123456,Company ,,15,,Employed Person,,Company,MatchingTransaction
|
||||||
|
09/24/20,123456,Employed Person ,,15,,-,person@example.com,Company,CreditCardTransaction
|
|
|
@ -686,7 +686,7 @@
|
||||||
ledger template: benevity donations ledger entry
|
ledger template: benevity donations ledger entry
|
||||||
|
|
||||||
- source: BrightFunds.xls
|
- source: BrightFunds.xls
|
||||||
importer: brightfunds.DonorReportImporter
|
importer: brightfunds.DonorReportXLSImporter
|
||||||
expect:
|
expect:
|
||||||
- date: !!python/object/apply:datetime.date [2017, 10, 20]
|
- date: !!python/object/apply:datetime.date [2017, 10, 20]
|
||||||
currency: USD
|
currency: USD
|
||||||
|
@ -701,6 +701,34 @@
|
||||||
on_behalf_of: Dakota Smith
|
on_behalf_of: Dakota Smith
|
||||||
type: Matched Donation
|
type: Matched Donation
|
||||||
|
|
||||||
|
- source: BrightFunds.csv
|
||||||
|
importer: brightfunds.DonorReportImporter
|
||||||
|
expect:
|
||||||
|
- date: !!python/object/apply:datetime.date [2020, 10, 01]
|
||||||
|
currency: USD
|
||||||
|
amount: !!python/object/apply:decimal.Decimal [15]
|
||||||
|
payee: Company
|
||||||
|
corporation: Company
|
||||||
|
company_name: Company
|
||||||
|
designation: ""
|
||||||
|
donor_name: Company
|
||||||
|
donor_email: ""
|
||||||
|
fund: ""
|
||||||
|
on_behalf_of: Employed Person
|
||||||
|
type: MatchingTransaction
|
||||||
|
- date: !!python/object/apply:datetime.date [2020, 9, 24]
|
||||||
|
currency: USD
|
||||||
|
amount: !!python/object/apply:decimal.Decimal [15]
|
||||||
|
payee: Employed Person
|
||||||
|
corporation: Company
|
||||||
|
company_name: Company
|
||||||
|
designation: ""
|
||||||
|
donor_name: Employed Person
|
||||||
|
donor_email: person@example.com
|
||||||
|
fund: ""
|
||||||
|
on_behalf_of: ""
|
||||||
|
type: CreditCardTransaction
|
||||||
|
|
||||||
- source: OReillyRoyalties.csv
|
- source: OReillyRoyalties.csv
|
||||||
importer: oreilly.RoyaltiesImporter
|
importer: oreilly.RoyaltiesImporter
|
||||||
expect:
|
expect:
|
||||||
|
|
Loading…
Reference in a new issue