Skip to content

Commit

Permalink
add payment export
Browse files Browse the repository at this point in the history
  • Loading branch information
nwolff committed Sep 12, 2019
1 parent 7bc1d4d commit 149af08
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
8 changes: 7 additions & 1 deletion payment/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from import_export.admin import ExportMixin
from import_export.formats import base_formats
from moneyed.localization import format_money

from .export import PaymentResource
from .models import Payment, Transaction


Expand Down Expand Up @@ -66,7 +69,7 @@ def has_module_permission(self, request):
# Payments

@admin.register(Payment)
class PaymentAdmin(admin.ModelAdmin):
class PaymentAdmin(ExportMixin, admin.ModelAdmin):
date_hierarchy = 'created'
ordering = ['-created']
list_filter = ['gateway', 'is_active', 'charge_status']
Expand All @@ -76,6 +79,9 @@ class PaymentAdmin(admin.ModelAdmin):

inlines = [TransactionInline]

resource_class = PaymentResource
formats = (base_formats.CSV, base_formats.XLS, base_formats.JSON) # Only useful and safe formats.

def formatted_total(self, obj):
return format_money(obj.total)

Expand Down
35 changes: 35 additions & 0 deletions payment/export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from import_export import resources
from import_export.fields import Field

from .models import Payment


class PaymentResource(resources.ModelResource):
"""
We take precise control of the output to separate amounts and currencies into separate columns for easier
downstream processing.
"""
total_amount = Field()
total_currency = Field()

captured_amount = Field()
captured_currency = Field()

active = Field('is_active')

class Meta:
model = Payment
fields = ['created', 'gateway', 'charge_status', 'customer_email']
export_order = ['active', 'total_amount', 'total_currency', 'captured_amount', 'captured_currency']

def dehydrate_total_amount(self, payment):
return payment.total.amount

def dehydrate_total_currency(self, payment):
return payment.total.currency.code

def dehydrate_captured_amount(self, payment):
return payment.captured_amount.amount

def dehydrate_captured_currency(self, payment):
return payment.captured_amount.currency.code
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name='django-payment',
version='0.8',
version='0.9',
description='',
long_description='',
author='Nicholas Wolff',
Expand All @@ -27,6 +27,7 @@
'stripe',
'django-countries',
'dataclasses',
'django-import-export',
],
license='MIT',
classifiers=[
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ deps =
django-money
structlog
typing
django-import-export
pytest-django
pytest-cov
flake8
Expand Down

0 comments on commit 149af08

Please sign in to comment.