|
1 | 1 | from django.contrib import admin |
2 | 2 | from django.shortcuts import redirect |
3 | 3 | from django.urls import path |
| 4 | +from django.utils.html import format_html |
4 | 5 | from django_pagarme.admin import PagarmeItemConfigAdmin |
5 | 6 | from django_pagarme.models import PagarmeItemConfig |
6 | 7 |
|
@@ -47,13 +48,44 @@ class NewPagarmeItemConfigAdmin(PagarmeItemConfigAdmin): |
47 | 48 | inlines = [PaymentItemConfigInline] |
48 | 49 |
|
49 | 50 |
|
| 51 | +class PaymentListFilter(admin.SimpleListFilter): |
| 52 | + title = 'Por Pagamento' |
| 53 | + parameter_name = 'has_payment' |
| 54 | + |
| 55 | + def lookups(self, request, model_admin): |
| 56 | + return [('yes', 'Com Pagamento'), ('no', 'Sem Pagamento')] |
| 57 | + |
| 58 | + def queryset(self, request, queryset): |
| 59 | + if self.value() == 'yes': |
| 60 | + return queryset.filter(payment_id__isnull=False) |
| 61 | + elif self.value() == 'no': |
| 62 | + return queryset.filter(payment_id__isnull=True) |
| 63 | + return queryset |
| 64 | + |
| 65 | + |
50 | 66 | @admin.register(Subscription) |
51 | 67 | class SubscriptionAdmin(admin.ModelAdmin): |
52 | | - fields = ['subscriber', 'subscription_types', 'observation'] |
53 | | - list_display = ['subscriber', 'responsible', 'status', 'created_at', 'updated_at'] |
| 68 | + fields = ['payment', 'subscriber', 'subscription_types', 'observation'] |
| 69 | + list_display = ['subscriber', 'pagarme_url_field', 'responsible', 'status', 'created_at', 'updated_at'] |
54 | 70 | autocomplete_fields = ['subscriber'] |
55 | | - search_fields = ['subscriber__email'] |
56 | | - list_filter = ['status', 'subscription_types'] |
| 71 | + search_fields = ['subscriber__email', 'payment__transaction_id'] |
| 72 | + list_filter = ['status', PaymentListFilter, 'subscription_types'] |
| 73 | + ordering = ['-updated_at'] |
| 74 | + |
| 75 | + def get_queryset(self, request): |
| 76 | + return Subscription.objects.select_related('payment').select_related('subscriber').select_related('responsible') |
| 77 | + |
| 78 | + def pagarme_url_field(self, obj): |
| 79 | + if obj.payment is None: |
| 80 | + return '----' |
| 81 | + link = ( |
| 82 | + f'<a href="https://beta.dashboard.pagar.me/#/transactions/' |
| 83 | + f'{obj.payment.transaction_id}">{obj.payment.transaction_id}</a>' |
| 84 | + ) |
| 85 | + return format_html(link) |
| 86 | + |
| 87 | + pagarme_url_field.allow_tags = True |
| 88 | + pagarme_url_field.short_description = 'Link Pagarme' |
57 | 89 |
|
58 | 90 | def has_delete_permission(self, request, obj=None): |
59 | 91 | return False |
0 commit comments