django-orchestra/orchestra/apps/payments/admin.py

68 lines
2.3 KiB
Python
Raw Normal View History

2014-07-23 16:24:56 +00:00
from django.contrib import admin
2014-07-29 20:10:37 +00:00
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
2014-07-23 16:24:56 +00:00
2014-07-24 09:53:34 +00:00
from orchestra.admin.utils import admin_colored, admin_link
from orchestra.apps.accounts.admin import AccountAdminMixin
2014-07-24 09:53:34 +00:00
2014-07-29 20:10:37 +00:00
from .actions import process_transactions
from .methods import DirectDebit
2014-07-29 20:10:37 +00:00
from .models import PaymentSource, Transaction, PaymentProcess
2014-07-23 16:24:56 +00:00
2014-07-24 09:53:34 +00:00
STATE_COLORS = {
Transaction.WAITTING_PROCESSING: 'darkorange',
Transaction.WAITTING_CONFIRMATION: 'orange',
Transaction.CONFIRMED: 'green',
Transaction.REJECTED: 'red',
Transaction.LOCKED: 'magenta',
Transaction.DISCARTED: 'blue',
}
class TransactionAdmin(admin.ModelAdmin):
list_display = (
'id', 'bill_link', 'account_link', 'source', 'display_state', 'amount'
2014-07-24 09:53:34 +00:00
)
list_filter = ('source__method', 'state')
2014-07-29 20:10:37 +00:00
actions = (process_transactions,)
2014-07-24 09:53:34 +00:00
bill_link = admin_link('bill')
account_link = admin_link('bill__account')
display_state = admin_colored('state', colors=STATE_COLORS)
class PaymentSourceAdmin(AccountAdminMixin, admin.ModelAdmin):
list_display = ('label', 'method', 'number', 'account_link', 'is_active')
list_filter = ('method', 'is_active')
form = DirectDebit().get_form()
# TODO select payment source method
2014-07-29 20:10:37 +00:00
class PaymentProcessAdmin(admin.ModelAdmin):
list_display = ('id', 'file_url', 'display_transactions', 'created_at')
fields = ('data', 'file_url', 'display_transactions', 'created_at')
readonly_fields = ('file_url', 'display_transactions', 'created_at')
def file_url(self, process):
if process.file:
return '<a href="%s">%s</a>' % (process.file.url, process.file.name)
file_url.allow_tags = True
file_url.admin_order_field = 'file'
def display_transactions(self, process):
links = []
for transaction in process.transactions.all():
url = reverse('admin:payments_transaction_change', args=(transaction.pk,))
links.append(
'<a href="%s">%s</a>' % (url, str(transaction))
)
return '<br>'.join(links)
display_transactions.short_description = _("Transactions")
display_transactions.allow_tags = True
admin.site.register(PaymentSource, PaymentSourceAdmin)
2014-07-24 09:53:34 +00:00
admin.site.register(Transaction, TransactionAdmin)
2014-07-29 20:10:37 +00:00
admin.site.register(PaymentProcess, PaymentProcessAdmin)