admin.py 2.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
from django.contrib import admin, messages
from django.contrib.admin.utils import model_ngettext
from django.utils.translation import ugettext_lazy as _

from payments import services
from .models import Payment


def _show_message(admin, request, n, message, error):
    if n == 0:
        admin.message_user(request, error, messages.ERROR)
    else:
        admin.message_user(request, message % {
            "count": n,
            "items": model_ngettext(admin.opts, n)
        }, messages.SUCCESS)


@admin.register(Payment)
class PaymentAdmin(admin.ModelAdmin):
    list_display = ('created_at', 'amount',
                    'processed', 'processing_date', 'type')
    list_filter = ('processed', 'amount',)
    date_hierarchy = 'created_at'
    fields = ('created_at', 'amount',
              'type', 'processed', 'processing_date')
    readonly_fields = ('created_at', 'amount', 'processed',
                       'type', 'processing_date')
    actions = ['process_cash_selected', 'process_card_selected']

    def changeform_view(self, request, object_id=None, form_url='',
                        extra_context=None):
        obj = None
        if (object_id is not None and
                request.user.has_perm('payments.process_payments')):
            obj = Payment.objects.get(id=object_id)
            if obj.processed:
                obj = None
        return super().changeform_view(
            request, object_id, form_url, {'payment': obj})

    def get_actions(self, request):
        actions = super().get_actions(request)
        if not request.user.has_perm('payments.process_payments'):
            del(actions['process_cash_selected'])
            del(actions['process_card_selected'])
        return actions

    def process_cash_selected(self, request, queryset):
        if request.user.has_perm('payments.process_payments'):
            updated_payments = services.process_payment(
                queryset, Payment.CASH
            )
            self._process_feedback(request, updated_payments)
    process_cash_selected.short_description = _(
        'Process selected payments (cash)')

    def process_card_selected(self, request, queryset):
        if request.user.has_perm('payments.process_payments'):
            updated_payments = services.process_payment(
                queryset, Payment.CARD
            )
            self._process_feedback(request, updated_payments)
    process_card_selected.short_description = _(
        'Process selected payments (card)')

    def _process_feedback(self, request, updated_payments):
        rows_updated = len(updated_payments)
        _show_message(
            self, request, rows_updated,
            message=_("Successfully processed %(count)d %(items)s."),
            error=_('The selected payment(s) could not be processed.')
        )