1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from django.contrib import admin
6 from django.db.models.functions import Now
7 from django.db.models import Q
8 from django.utils.html import conditional_escape
9 from django.utils.safestring import mark_safe
10 from django.utils.translation import gettext_lazy as _
11 from fnpdjango.actions import export_as_csv_action
12 from modeltranslation.admin import TranslationAdmin
13 from wolnelektury.utils import YesNoFilter
17 class SingleAmountInline(admin.TabularInline):
18 model = models.SingleAmount
21 class MonthlyAmountInline(admin.TabularInline):
22 model = models.MonthlyAmount
25 @admin.register(models.Club)
26 class ClubAdmin(admin.ModelAdmin):
33 class PayUOrderInline(admin.TabularInline):
34 model = models.PayUOrder
35 fields = ['order_id', 'status', 'customer_ip']
36 readonly_fields = fields
38 show_change_link = True
41 def has_add_permission(self, request, obj):
45 class PayUCardTokenInline(admin.TabularInline):
46 model = models.PayUCardToken
47 fields = ['created_at', 'disposable_token', 'reusable_token']
48 readonly_fields = fields
50 show_change_link = True
52 show_change_link = True
54 def has_add_permission(self, request, obj):
58 class PayedFilter(YesNoFilter):
59 title = _('payment complete')
60 parameter_name = 'payed'
64 class ExpiredFilter(YesNoFilter):
65 title = _('schedule expired')
66 parameter_name = 'expired'
67 q = Q(expires_at__isnull=False, expires_at__lt=Now())
70 class ScheduleAdmin(admin.ModelAdmin):
72 'email', 'started_at', 'payed_at', 'expires_at', 'amount', 'monthly', 'yearly', 'is_cancelled',
75 search_fields = ['email']
76 list_filter = ['is_cancelled', 'monthly', 'yearly', 'method', PayedFilter, ExpiredFilter, 'source']
77 filter_horizontal = ['consent']
78 date_hierarchy = 'started_at'
79 raw_id_fields = ['membership']
80 inlines = [PayUOrderInline, PayUCardTokenInline]
81 actions = [export_as_csv_action()]
83 admin.site.register(models.Schedule, ScheduleAdmin)
86 class ScheduleInline(admin.TabularInline):
87 model = models.Schedule
88 fields = ['email', 'amount', 'is_cancelled', 'started_at', 'payed_at', 'expires_at', 'email_sent']
89 readonly_fields = fields
91 show_change_link = True
94 def has_add_permission(self, request, obj):
98 class MembershipAdmin(admin.ModelAdmin):
99 list_display = ['user', 'manual', 'updated_at', 'notes']
100 list_filter = ['manual']
101 date_hierarchy = 'updated_at'
102 raw_id_fields = ['user']
103 search_fields = ['user__username', 'user__email', 'schedule__email', 'notes']
104 inlines = [ScheduleInline]
106 admin.site.register(models.Membership, MembershipAdmin)
109 admin.site.register(models.ReminderEmail, TranslationAdmin)
112 class PayUNotificationAdmin(admin.ModelAdmin):
113 list_display = ['received_at', 'order']
114 fields = ['received_at', 'order', 'body_']
115 readonly_fields = ['received_at', 'body_']
116 raw_id_fields = ['order']
118 def body_(self, obj):
121 conditional_escape(json.dumps(json.loads(obj.body), indent=4))
125 admin.site.register(models.PayUNotification, PayUNotificationAdmin)
128 class PayUNotificationInline(admin.TabularInline):
129 model = models.PayUNotification
130 fields = ['received_at', 'body_']
131 readonly_fields = fields
133 show_change_link = True
136 def body_(self, obj):
139 conditional_escape(json.dumps(json.loads(obj.body), indent=4))
142 def has_add_permission(self, request, obj):
146 class PayUOrderAdmin(admin.ModelAdmin):
147 list_display = ['schedule']
148 raw_id_fields = ['schedule']
149 inlines = [PayUNotificationInline]
152 admin.site.register(models.PayUOrder, PayUOrderAdmin)
155 admin.site.register(models.Ambassador)
160 @admin.register(models.Consent)
161 class ConsentAdmin(admin.ModelAdmin):
162 list_display = ['text', 'order', 'active', 'required']
164 def get_readonly_fields(self, request, obj=None):