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 ActiveFilter(YesNoFilter):
71 title = 'płatność aktualna'
72 parameter_name = 'active'
73 q = Q(expires_at__gt=Now())
76 class ScheduleForm(forms.ModelForm):
77 def __init__(self, *args, **kwargs):
78 super().__init__(*args, **kwargs)
79 self.fields['email'].required = False
80 self.fields['method'].required = False
81 self.fields['consent'].required = False
84 model = models.Schedule
88 class SourceFilter(admin.SimpleListFilter):
89 title = 'Źródło' # display title
90 parameter_name = 'source'
91 template = "admin/long_filter.html"
93 def lookups(self, request, model_admin):
96 model_admin.model.objects.exclude(source='').values_list('source', flat=True).distinct()[:10]
101 def queryset(self, request, queryset):
104 #field_name = 'source' # name of the foreign key field
109 class ScheduleAdmin(admin.ModelAdmin):
113 'email', 'started_at', 'payed_at', 'expires_at', 'amount', 'monthly', 'yearly', 'is_cancelled',
116 list_display_links = ['email', 'started_at']
117 search_fields = ['email', 'source']
119 'is_cancelled', 'monthly', 'yearly', 'method',
120 PayedFilter, ActiveFilter, ExpiredFilter,
123 filter_horizontal = ['consent']
124 date_hierarchy = 'started_at'
125 raw_id_fields = ['membership']
126 inlines = [PayUOrderInline, PayUCardTokenInline]
127 actions = [export_as_csv_action()]
129 admin.site.register(models.Schedule, ScheduleAdmin)
132 class ScheduleInline(admin.TabularInline):
133 model = models.Schedule
134 fields = ['email', 'amount', 'is_cancelled', 'started_at', 'payed_at', 'expires_at', 'email_sent']
135 readonly_fields = fields
137 show_change_link = True
140 def has_add_permission(self, request, obj):
144 class MembershipAdmin(admin.ModelAdmin):
145 list_display = ['user', 'manual', 'updated_at', 'notes']
146 list_filter = ['manual']
147 date_hierarchy = 'updated_at'
148 raw_id_fields = ['user']
149 search_fields = ['user__username', 'user__email', 'schedule__email', 'notes']
150 inlines = [ScheduleInline]
152 admin.site.register(models.Membership, MembershipAdmin)
155 admin.site.register(models.ReminderEmail, TranslationAdmin)
158 class PayUNotificationAdmin(admin.ModelAdmin):
159 list_display = ['received_at', 'order']
160 fields = ['received_at', 'order', 'body_']
161 readonly_fields = ['received_at', 'body_']
162 raw_id_fields = ['order']
164 def body_(self, obj):
167 conditional_escape(json.dumps(json.loads(obj.body), indent=4))
171 admin.site.register(models.PayUNotification, PayUNotificationAdmin)
174 class PayUNotificationInline(admin.TabularInline):
175 model = models.PayUNotification
176 fields = ['received_at', 'body_']
177 readonly_fields = fields
179 show_change_link = True
182 def body_(self, obj):
185 conditional_escape(json.dumps(json.loads(obj.body), indent=4))
188 def has_add_permission(self, request, obj):
192 class PayUOrderAdmin(admin.ModelAdmin):
193 list_display = ['schedule']
194 raw_id_fields = ['schedule']
195 inlines = [PayUNotificationInline]
198 admin.site.register(models.PayUOrder, PayUOrderAdmin)
201 admin.site.register(models.Ambassador)
206 @admin.register(models.Consent)
207 class ConsentAdmin(admin.ModelAdmin):
208 list_display = ['text', 'order', 'active', 'required']
210 def get_readonly_fields(self, request, obj=None):