Skip to content

Commit 554d11c

Browse files
renzonrenzon
authored andcommitted
Created filter by payment, included pagarme item on Subscritption admin
close #3772
1 parent be18554 commit 554d11c

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

pythonpro/memberkit/admin.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from django.contrib import admin
22
from django.shortcuts import redirect
33
from django.urls import path
4+
from django.utils.html import format_html
45
from django_pagarme.admin import PagarmeItemConfigAdmin
56
from django_pagarme.models import PagarmeItemConfig
67

@@ -47,13 +48,44 @@ class NewPagarmeItemConfigAdmin(PagarmeItemConfigAdmin):
4748
inlines = [PaymentItemConfigInline]
4849

4950

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+
5066
@admin.register(Subscription)
5167
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']
5470
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'
5789

5890
def has_delete_permission(self, request, obj=None):
5991
return False

0 commit comments

Comments
 (0)