def consume_csv(self, file_handle): data = DictReader(file_handle) for t in data: transaction = Transaction(id=t[' Transaction ID']) timestamp_string = "{} {} {}".format(t['Date'], t[' Time'], t[' Time Zone']) timestamp = parse(timestamp_string) #timestamp = datetime.strptime(timestamp_string, "%m/%d/%Y %H:%M:%S").replace(tzinfo=pytz.timezone(t[' Time Zone'])) transaction.timestamp = timestamp transaction.type = t[' Type'] transaction.from_email = t[' From Email Address'] transaction.to_email = t[' To Email Address'] transaction.name = t[' Name'] transaction.status = t[' Status'] if t.has_key(' Gross'): transaction.amount = Money(t[' Gross'].replace(',', ''), 'USD') if t.has_key(' Fee') and t[' Fee'] != '...': transaction.fee_amount = Money(t[' Fee'], 'USD') if t.has_key(' Net'): transaction.net_amount = Money(t[' Net'].replace(',', ''), 'USD') if t[' Balance'] != '...': transaction.balance = Money(t[' Balance'].replace(',', ''), 'USD') if t.has_key(' Reference Txn ID') and t[' Reference Txn ID'] != '': try: referenced_transaction = Transaction.objects.get( id=t[' Reference Txn ID']) transaction.reference = referenced_transaction except Transaction.DoesNotExist: print("Skipping filling in reference for {}->{}".format( transaction.id, t[' Reference Txn ID'])) transaction.save()
def handle(self, *args, **options): now = datetime.now() - timedelta(days=30) try: latest = Transaction.objects.latest('timestamp').timestamp except Transaction.DoesNotExist: latest = datetime.now() - timedelta(days=365 * 5) pprint(latest) paypal = PayPalInterface(CONFIG) result = paypal.transaction_search(startdate=latest) print "grabbed %d items" % len(result.items()) for item in result.items(): error = False transaction = Transaction(id=item['TRANSACTIONID']) transaction.timestamp = datetime.strptime( item[u'TIMESTAMP'], '%Y-%m-%dT%H:%M:%SZ').replace(tzinfo=pytz.UTC) transaction.type = item[u'TYPE'] try: transaction.email = item[u'EMAIL'] except KeyError: error = True transaction.name = item[u'NAME'] transaction.status = item[u'STATUS'] try: transaction.amount = Money(item[u'AMT'], item[u'CURRENCYCODE']) except KeyError: error = True try: transaction.fee_amount = Money(item[u'FEEAMT'], item[u'CURRENCYCODE']) except KeyError: error = True try: transaction.net_amount = Money(item[u'NETAMT'], item[u'CURRENCYCODE']) except KeyError: error = True if error: pprint(item) transaction.save()