Add cache table, importer for NationBuilder donations
This commit is contained in:
parent
719de78413
commit
63286679ad
6 changed files with 182 additions and 9 deletions
|
@ -34,3 +34,6 @@ from rattail_nationbuilder.db import model
|
|||
|
||||
class NationBuilderCachePersonImporter(ToRattail):
|
||||
model_class = model.NationBuilderCachePerson
|
||||
|
||||
class NationBuilderCacheDonationImporter(ToRattail):
|
||||
model_class = model.NationBuilderCacheDonation
|
||||
|
|
|
@ -25,6 +25,7 @@ NationBuilder -> Rattail importing
|
|||
"""
|
||||
|
||||
import datetime
|
||||
import decimal
|
||||
from collections import OrderedDict
|
||||
|
||||
from rattail import importing
|
||||
|
@ -43,6 +44,7 @@ class FromNationBuilderToRattail(importing.ToRattailHandler):
|
|||
def get_importers(self):
|
||||
importers = OrderedDict()
|
||||
importers['NationBuilderCachePerson'] = NationBuilderCachePersonImporter
|
||||
importers['NationBuilderCacheDonation'] = NationBuilderCacheDonationImporter
|
||||
return importers
|
||||
|
||||
|
||||
|
@ -58,6 +60,14 @@ class FromNationBuilder(importing.Importer):
|
|||
def setup_api(self):
|
||||
self.nationbuilder = NationBuilderWebAPI(self.config)
|
||||
|
||||
def normalize_timestamp(self, value):
|
||||
if not value:
|
||||
return
|
||||
|
||||
dt = datetime.datetime.strptime(value, '%Y-%m-%dT%H:%M:%S%z')
|
||||
dt = self.app.localtime(dt)
|
||||
return self.app.make_utc(dt)
|
||||
|
||||
|
||||
class NationBuilderCachePersonImporter(FromNationBuilder, nationbuilder_importing.model.NationBuilderCachePersonImporter):
|
||||
"""
|
||||
|
@ -95,14 +105,6 @@ class NationBuilderCachePersonImporter(FromNationBuilder, nationbuilder_importin
|
|||
def get_host_objects(self):
|
||||
return self.nationbuilder.get_people(page_size=500)
|
||||
|
||||
def normalize_timestamp(self, value):
|
||||
if not value:
|
||||
return
|
||||
|
||||
dt = datetime.datetime.strptime(value, '%Y-%m-%dT%H:%M:%S%z')
|
||||
dt = self.app.localtime(dt)
|
||||
return self.app.make_utc(dt)
|
||||
|
||||
def normalize_host_object(self, person):
|
||||
|
||||
# nb. some fields may not be present in person dict
|
||||
|
@ -130,3 +132,48 @@ class NationBuilderCachePersonImporter(FromNationBuilder, nationbuilder_importin
|
|||
})
|
||||
|
||||
return data
|
||||
|
||||
|
||||
class NationBuilderCacheDonationImporter(FromNationBuilder, nationbuilder_importing.model.NationBuilderCacheDonationImporter):
|
||||
"""
|
||||
Importer for NB Donation cache
|
||||
"""
|
||||
key = 'id'
|
||||
supported_fields = [
|
||||
'id',
|
||||
'author_id',
|
||||
'membership_id',
|
||||
'donor_id',
|
||||
'donor_external_id',
|
||||
'email',
|
||||
'amount',
|
||||
'payment_type_name',
|
||||
'check_number',
|
||||
'tracking_code_slug',
|
||||
'note',
|
||||
'created_at',
|
||||
'succeeded_at',
|
||||
'failed_at',
|
||||
'canceled_at',
|
||||
'updated_at',
|
||||
]
|
||||
|
||||
def get_host_objects(self):
|
||||
return self.nationbuilder.get_donations(page_size=500)
|
||||
|
||||
def normalize_host_object(self, donation):
|
||||
|
||||
# nb. some fields may not be present in donation dict
|
||||
data = dict([(field, donation.get(field))
|
||||
for field in self.fields])
|
||||
if data:
|
||||
|
||||
donor = donation.get('donor')
|
||||
data['donor_external_id'] = donor.get('external_id') if donor else None
|
||||
|
||||
data['amount'] = decimal.Decimal('{:0.2f}'.format(donation['amount_in_cents'] / 100))
|
||||
|
||||
for field in ('created_at', 'succeeded_at', 'failed_at', 'canceled_at', 'updated_at'):
|
||||
data[field] = self.normalize_timestamp(data[field])
|
||||
|
||||
return data
|
||||
|
|
|
@ -31,6 +31,7 @@ class NationBuilderVersionMixin(object):
|
|||
|
||||
def add_nationbuilder_importers(self, importers):
|
||||
importers['NationBuilderCachePerson'] = NationBuilderCachePersonImporter
|
||||
importers['NationBuilderCacheDonation'] = NationBuilderCacheDonationImporter
|
||||
return importers
|
||||
|
||||
|
||||
|
@ -39,3 +40,10 @@ class NationBuilderCachePersonImporter(base.VersionImporter):
|
|||
@property
|
||||
def host_model_class(self):
|
||||
return self.model.NationBuilderCachePerson
|
||||
|
||||
|
||||
class NationBuilderCacheDonationImporter(base.VersionImporter):
|
||||
|
||||
@property
|
||||
def host_model_class(self):
|
||||
return self.model.NationBuilderCacheDonation
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue