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 f9fd8ca
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 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
33 changes: 33 additions & 0 deletions payment/export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from import_export import resources
from import_export.fields import Field

from .models import Payment


class PaymentResource(resources.ModelResource):
"""We mostly separate amounts and currencies to make further data processing easier."""
total_amount = Field()
total_currency = Field()

captured_amount = Field()
captured_currency = Field()

active = Field('is_active')

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

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
4 changes: 2 additions & 2 deletions 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='1.0',
description='',
long_description='',
author='Nicholas Wolff',
Expand All @@ -27,12 +27,12 @@
'stripe',
'django-countries',
'dataclasses',
'django-import-export',
],
license='MIT',
classifiers=[
'Development Status :: 5 - Production/Stable',
'Environment :: Web Environment',
'Framework :: Django :: 2.1',
'Framework :: Django :: 2.2',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
Expand Down
7 changes: 5 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tox]
envlist =
{py36,py37}-{django21,django22}-{test-with-coverage}
{py36,py37}-{django22}-{test-with-coverage}
py36-django22-{checkmigrations,flake,mypy}

[testenv]
Expand All @@ -13,11 +13,14 @@ commands =
flake: flake8
mypy: mypy .
deps =
django21: Django>=2.1,<2.2
django22: Django>=2.2,<2.3
django-money
structlog
typing
stripe
django-countries
dataclasses
django-import-export
pytest-django
pytest-cov
flake8
Expand Down

0 comments on commit f9fd8ca

Please sign in to comment.