1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. 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 import forms
9 from django.utils.html import conditional_escape
10 from django.utils.safestring import mark_safe
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 = 'płatność zakończona'
60 parameter_name = 'payed'
64 class ExpiredFilter(YesNoFilter):
65 title = 'harmonogram przedawniony'
66 parameter_name = 'expired'
67 q = Q(expires_at__isnull=False, expires_at__lt=Now())
70 class ScheduleForm(forms.ModelForm):
71 def __init__(self, *args, **kwargs):
72 super().__init__(*args, **kwargs)
73 self.fields['email'].required = False
74 self.fields['method'].required = False
75 self.fields['consent'].required = False
78 model = models.Schedule
82 class SourceFilter(admin.SimpleListFilter):
83 title = 'Źródło' # display title
84 parameter_name = 'source'
85 template = "admin/long_filter.html"
87 def lookups(self, request, model_admin):
90 model_admin.model.objects.exclude(source='').values_list('source', flat=True).distinct()[:10]
95 def queryset(self, request, queryset):
98 #field_name = 'source' # name of the foreign key field
103 class ScheduleAdmin(admin.ModelAdmin):
107 'email', 'started_at', 'payed_at', 'expires_at', 'amount', 'monthly', 'yearly', 'is_cancelled',
110 list_display_links = ['email', 'started_at']
111 search_fields = ['email', 'source']
113 'is_cancelled', 'monthly', 'yearly', 'method',
114 PayedFilter, ExpiredFilter,
117 filter_horizontal = ['consent']
118 date_hierarchy = 'started_at'
119 raw_id_fields = ['membership']
120 inlines = [PayUOrderInline, PayUCardTokenInline]
121 actions = [export_as_csv_action()]
123 admin.site.register(models.Schedule, ScheduleAdmin)
126 class ScheduleInline(admin.TabularInline):
127 model = models.Schedule
128 fields = ['email', 'amount', 'is_cancelled', 'started_at', 'payed_at', 'expires_at', 'email_sent']
129 readonly_fields = fields
131 show_change_link = True
134 def has_add_permission(self, request, obj):
138 class MembershipAdmin(admin.ModelAdmin):
139 list_display = ['user', 'manual', 'updated_at', 'notes']
140 list_filter = ['manual']
141 date_hierarchy = 'updated_at'
142 raw_id_fields = ['user']
143 search_fields = ['user__username', 'user__email', 'schedule__email', 'notes']
144 inlines = [ScheduleInline]
146 admin.site.register(models.Membership, MembershipAdmin)
149 admin.site.register(models.ReminderEmail, TranslationAdmin)
152 class PayUNotificationAdmin(admin.ModelAdmin):
153 list_display = ['received_at', 'order']
154 fields = ['received_at', 'order', 'body_']
155 readonly_fields = ['received_at', 'body_']
156 raw_id_fields = ['order']
158 def body_(self, obj):
161 conditional_escape(json.dumps(json.loads(obj.body), indent=4))
165 admin.site.register(models.PayUNotification, PayUNotificationAdmin)
168 class PayUNotificationInline(admin.TabularInline):
169 model = models.PayUNotification
170 fields = ['received_at', 'body_']
171 readonly_fields = fields
173 show_change_link = True
176 def body_(self, obj):
179 conditional_escape(json.dumps(json.loads(obj.body), indent=4))
182 def has_add_permission(self, request, obj):
186 class PayUOrderAdmin(admin.ModelAdmin):
187 list_display = ['schedule']
188 raw_id_fields = ['schedule']
189 inlines = [PayUNotificationInline]
192 admin.site.register(models.PayUOrder, PayUOrderAdmin)
195 admin.site.register(models.Ambassador)
200 @admin.register(models.Consent)
201 class ConsentAdmin(admin.ModelAdmin):
202 list_display = ['text', 'order', 'active', 'required']
204 def get_readonly_fields(self, request, obj=None):