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
|
2015-06-02 12:59:49 +00:00
|
|
|
from django.http import HttpResponseRedirect
|
2014-07-29 20:10:37 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2014-07-23 16:24:56 +00:00
|
|
|
|
2014-11-12 16:33:40 +00:00
|
|
|
from orchestra.admin import ChangeViewActionsMixin, ExtendedModelAdmin
|
2014-09-26 19:21:09 +00:00
|
|
|
from orchestra.admin.utils import admin_colored, admin_link
|
2015-07-21 10:44:32 +00:00
|
|
|
from orchestra.contrib.accounts.actions import list_accounts
|
2015-04-05 10:46:24 +00:00
|
|
|
from orchestra.contrib.accounts.admin import AccountAdminMixin, SelectAccountAdminMixin
|
2014-11-24 14:39:41 +00:00
|
|
|
from orchestra.plugins.admin import SelectPluginAdminMixin
|
2014-07-24 09:53:34 +00:00
|
|
|
|
2015-06-02 12:59:49 +00:00
|
|
|
from . import actions, helpers
|
2014-09-05 14:27:30 +00:00
|
|
|
from .methods import PaymentMethod
|
|
|
|
from .models import PaymentSource, Transaction, TransactionProcess
|
2014-07-23 16:24:56 +00:00
|
|
|
|
|
|
|
|
2014-07-24 09:53:34 +00:00
|
|
|
STATE_COLORS = {
|
|
|
|
Transaction.WAITTING_PROCESSING: 'darkorange',
|
2014-09-18 15:07:39 +00:00
|
|
|
Transaction.WAITTING_EXECUTION: 'magenta',
|
2014-09-16 17:14:24 +00:00
|
|
|
Transaction.EXECUTED: 'olive',
|
2014-09-05 14:27:30 +00:00
|
|
|
Transaction.SECURED: 'green',
|
2014-07-24 09:53:34 +00:00
|
|
|
Transaction.REJECTED: 'red',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-26 19:21:09 +00:00
|
|
|
class PaymentSourceAdmin(SelectPluginAdminMixin, AccountAdminMixin, admin.ModelAdmin):
|
2014-09-18 15:07:39 +00:00
|
|
|
list_display = ('label', 'method', 'number', 'account_link', 'is_active')
|
|
|
|
list_filter = ('method', 'is_active')
|
2014-11-27 19:17:26 +00:00
|
|
|
search_fields = ('account__username', 'account__full_name', 'data')
|
2014-09-26 19:21:09 +00:00
|
|
|
plugin = PaymentMethod
|
|
|
|
plugin_field = 'method'
|
2014-09-18 15:07:39 +00:00
|
|
|
|
|
|
|
|
2014-09-05 14:27:30 +00:00
|
|
|
class TransactionInline(admin.TabularInline):
|
|
|
|
model = Transaction
|
|
|
|
can_delete = False
|
|
|
|
extra = 0
|
2014-09-16 17:14:24 +00:00
|
|
|
fields = (
|
|
|
|
'transaction_link', 'bill_link', 'source_link', 'display_state',
|
|
|
|
'amount', 'currency'
|
|
|
|
)
|
2014-09-05 14:27:30 +00:00
|
|
|
readonly_fields = fields
|
|
|
|
|
2015-04-02 16:14:55 +00:00
|
|
|
transaction_link = admin_link('__str__', short_description=_("ID"))
|
2014-09-05 14:27:30 +00:00
|
|
|
bill_link = admin_link('bill')
|
|
|
|
source_link = admin_link('source')
|
|
|
|
display_state = admin_colored('state', colors=STATE_COLORS)
|
|
|
|
|
|
|
|
class Media:
|
|
|
|
css = {
|
|
|
|
'all': ('orchestra/css/hide-inline-id.css',)
|
|
|
|
}
|
|
|
|
|
|
|
|
def has_add_permission(self, *args, **kwargs):
|
|
|
|
return False
|
2016-02-23 11:49:10 +00:00
|
|
|
|
|
|
|
def get_queryset(self, *args, **kwargs):
|
|
|
|
qs = super().get_queryset(*args, **kwargs)
|
|
|
|
return qs.select_related('source', 'bill')
|
2014-09-05 14:27:30 +00:00
|
|
|
|
|
|
|
|
2014-10-21 15:29:36 +00:00
|
|
|
class TransactionAdmin(SelectAccountAdminMixin, ExtendedModelAdmin):
|
2014-07-24 09:53:34 +00:00
|
|
|
list_display = (
|
2014-09-16 17:14:24 +00:00
|
|
|
'id', 'bill_link', 'account_link', 'source_link', 'display_state',
|
|
|
|
'amount', 'process_link'
|
2014-07-24 09:53:34 +00:00
|
|
|
)
|
2014-07-28 17:28:00 +00:00
|
|
|
list_filter = ('source__method', 'state')
|
2014-10-21 15:29:36 +00:00
|
|
|
fieldsets = (
|
|
|
|
(None, {
|
|
|
|
'classes': ('wide',),
|
|
|
|
'fields': (
|
|
|
|
'account_link',
|
|
|
|
'bill_link',
|
|
|
|
'source_link',
|
|
|
|
'display_state',
|
|
|
|
'amount',
|
|
|
|
'currency',
|
|
|
|
'process_link'
|
|
|
|
)
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
add_fieldsets = (
|
|
|
|
(None, {
|
|
|
|
'classes': ('wide',),
|
|
|
|
'fields': (
|
|
|
|
'bill',
|
|
|
|
'source',
|
|
|
|
'display_state',
|
|
|
|
'amount',
|
|
|
|
'currency',
|
|
|
|
'process'
|
|
|
|
)
|
|
|
|
}),
|
|
|
|
)
|
2015-06-03 12:49:30 +00:00
|
|
|
change_view_actions = (
|
|
|
|
actions.process_transactions, actions.mark_as_executed, actions.mark_as_secured,
|
|
|
|
actions.mark_as_rejected,
|
2014-09-16 17:14:24 +00:00
|
|
|
)
|
2015-07-21 10:44:32 +00:00
|
|
|
actions = change_view_actions + (actions.report, list_accounts)
|
2014-10-21 15:29:36 +00:00
|
|
|
filter_by_account_fields = ('bill', 'source')
|
|
|
|
change_readonly_fields = ('amount', 'currency')
|
2015-07-14 10:19:45 +00:00
|
|
|
readonly_fields = (
|
|
|
|
'bill_link', 'display_state', 'process_link', 'account_link', 'source_link'
|
|
|
|
)
|
|
|
|
list_select_related = ('source', 'bill__account', 'process')
|
2015-06-22 14:14:16 +00:00
|
|
|
date_hierarchy = 'created_at'
|
2014-07-24 09:53:34 +00:00
|
|
|
|
|
|
|
bill_link = admin_link('bill')
|
2014-09-04 15:55:43 +00:00
|
|
|
source_link = admin_link('source')
|
2014-09-05 14:27:30 +00:00
|
|
|
process_link = admin_link('process', short_description=_("proc"))
|
2014-07-24 09:53:34 +00:00
|
|
|
account_link = admin_link('bill__account')
|
2014-08-19 18:59:23 +00:00
|
|
|
|
2014-09-16 17:14:24 +00:00
|
|
|
def get_change_view_actions(self, obj=None):
|
|
|
|
actions = super(TransactionAdmin, self).get_change_view_actions()
|
2014-09-18 15:07:39 +00:00
|
|
|
exclude = []
|
2014-09-16 17:14:24 +00:00
|
|
|
if obj:
|
2014-09-19 14:47:25 +00:00
|
|
|
if obj.state == Transaction.WAITTING_PROCESSING:
|
2015-07-08 10:21:19 +00:00
|
|
|
exclude = ['mark_as_executed', 'mark_as_secured']
|
2014-09-19 14:47:25 +00:00
|
|
|
elif obj.state == Transaction.WAITTING_EXECUTION:
|
2015-07-08 10:21:19 +00:00
|
|
|
exclude = ['process_transactions', 'mark_as_secured']
|
2014-09-16 17:14:24 +00:00
|
|
|
if obj.state == Transaction.EXECUTED:
|
2014-09-19 14:47:25 +00:00
|
|
|
exclude = ['process_transactions', 'mark_as_executed']
|
|
|
|
elif obj.state in [Transaction.REJECTED, Transaction.SECURED]:
|
|
|
|
return []
|
2014-09-18 15:07:39 +00:00
|
|
|
return [action for action in actions if action.__name__ not in exclude]
|
2015-09-29 12:35:22 +00:00
|
|
|
|
|
|
|
def display_state(self, obj):
|
|
|
|
state = admin_colored('state', colors=STATE_COLORS)(obj)
|
|
|
|
help_text = obj.get_state_help()
|
|
|
|
state = state.replace('<span ', '<span title="%s" ' % help_text)
|
|
|
|
return state
|
|
|
|
display_state.admin_order_field = 'state'
|
|
|
|
display_state.short_description = _("State")
|
|
|
|
display_state.allow_tags = True
|
2014-07-28 17:28:00 +00:00
|
|
|
|
|
|
|
|
2014-09-18 15:07:39 +00:00
|
|
|
class TransactionProcessAdmin(ChangeViewActionsMixin, admin.ModelAdmin):
|
2014-07-29 20:10:37 +00:00
|
|
|
list_display = ('id', 'file_url', 'display_transactions', 'created_at')
|
2014-10-11 16:21:51 +00:00
|
|
|
fields = ('data', 'file_url', 'created_at')
|
2014-10-21 15:29:36 +00:00
|
|
|
readonly_fields = ('data', 'file_url', 'display_transactions', 'created_at')
|
2016-02-19 10:11:28 +00:00
|
|
|
list_prefetch_related = ('transactions',)
|
2014-09-05 14:27:30 +00:00
|
|
|
inlines = [TransactionInline]
|
2015-06-03 12:49:30 +00:00
|
|
|
change_view_actions = (
|
|
|
|
actions.mark_process_as_executed, actions.abort, actions.commit, actions.report
|
|
|
|
)
|
2015-05-30 14:44:05 +00:00
|
|
|
actions = change_view_actions + (actions.delete_selected,)
|
2014-07-29 20:10:37 +00:00
|
|
|
|
|
|
|
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):
|
2014-07-30 12:55:33 +00:00
|
|
|
ids = []
|
|
|
|
lines = []
|
|
|
|
counter = 0
|
2016-02-19 10:11:28 +00:00
|
|
|
for trans in process.transactions.all():
|
2015-09-30 13:22:17 +00:00
|
|
|
color = STATE_COLORS.get(trans.state, 'black')
|
|
|
|
state = trans.get_state_display()
|
|
|
|
ids.append('<span style="color:%s" title="%s">%i</span>' % (color, state, trans.id))
|
2014-07-30 12:55:33 +00:00
|
|
|
counter += 1
|
|
|
|
if counter > 10:
|
|
|
|
counter = 0
|
|
|
|
lines.append(','.join(ids))
|
|
|
|
ids = []
|
|
|
|
lines.append(','.join(ids))
|
2014-10-11 16:21:51 +00:00
|
|
|
transactions = '<br'.join(lines)
|
2014-07-30 12:55:33 +00:00
|
|
|
url = reverse('admin:payments_transaction_changelist')
|
2016-02-19 10:11:28 +00:00
|
|
|
url += '?process_id=%i' % process.id
|
2014-10-11 16:21:51 +00:00
|
|
|
return '<a href="%s">%s</a>' % (url, transactions)
|
2014-07-29 20:10:37 +00:00
|
|
|
display_transactions.short_description = _("Transactions")
|
|
|
|
display_transactions.allow_tags = True
|
2014-09-18 15:07:39 +00:00
|
|
|
|
2014-09-22 15:59:53 +00:00
|
|
|
def has_add_permission(self, *args, **kwargs):
|
|
|
|
return False
|
|
|
|
|
2014-09-18 15:07:39 +00:00
|
|
|
def get_change_view_actions(self, obj=None):
|
2016-02-19 10:11:28 +00:00
|
|
|
actions = super().get_change_view_actions()
|
2014-09-18 15:07:39 +00:00
|
|
|
exclude = []
|
|
|
|
if obj:
|
|
|
|
if obj.state == TransactionProcess.EXECUTED:
|
|
|
|
exclude.append('mark_process_as_executed')
|
|
|
|
elif obj.state == TransactionProcess.COMMITED:
|
|
|
|
exclude = ['mark_process_as_executed', 'abort', 'commit']
|
|
|
|
elif obj.state == TransactionProcess.ABORTED:
|
|
|
|
exclude = ['mark_process_as_executed', 'abort', 'commit']
|
|
|
|
return [action for action in actions if action.__name__ not in exclude]
|
2015-06-02 12:59:49 +00:00
|
|
|
|
|
|
|
def delete_view(self, request, object_id, extra_context=None):
|
|
|
|
queryset = self.model.objects.filter(id=object_id)
|
|
|
|
related_transactions = helpers.pre_delete_processes(self, request, queryset)
|
2016-02-19 10:11:28 +00:00
|
|
|
response = super().delete_view(request, object_id, extra_context)
|
2015-06-02 12:59:49 +00:00
|
|
|
if isinstance(response, HttpResponseRedirect):
|
|
|
|
helpers.post_delete_processes(self, request, related_transactions)
|
|
|
|
return response
|
2014-07-29 20:10:37 +00:00
|
|
|
|
2014-07-28 17:28:00 +00:00
|
|
|
admin.site.register(PaymentSource, PaymentSourceAdmin)
|
2014-07-24 09:53:34 +00:00
|
|
|
admin.site.register(Transaction, TransactionAdmin)
|
2014-09-05 14:27:30 +00:00
|
|
|
admin.site.register(TransactionProcess, TransactionProcessAdmin)
|