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 import forms
9 from django.utils.html import conditional_escape
10 from django.utils.safestring import mark_safe
11 from django.utils.translation import gettext_lazy as _
12 from fnpdjango.actions import export_as_csv_action
13 from modeltranslation.admin import TranslationAdmin
14 from wolnelektury.utils import YesNoFilter
18 class SingleAmountInline(admin.TabularInline):
19 model = models.SingleAmount
22 class MonthlyAmountInline(admin.TabularInline):
23 model = models.MonthlyAmount
26 @admin.register(models.Club)
27 class ClubAdmin(admin.ModelAdmin):
34 class PayUOrderInline(admin.TabularInline):
35 model = models.PayUOrder
36 fields = ['order_id', 'status', 'customer_ip']
37 readonly_fields = fields
39 show_change_link = True
42 def has_add_permission(self, request, obj):
46 class PayUCardTokenInline(admin.TabularInline):
47 model = models.PayUCardToken
48 fields = ['created_at', 'disposable_token', 'reusable_token']
49 readonly_fields = fields
51 show_change_link = True
53 show_change_link = True
55 def has_add_permission(self, request, obj):
59 class PayedFilter(YesNoFilter):
60 title = _('payment complete')
61 parameter_name = 'payed'
65 class ExpiredFilter(YesNoFilter):
66 title = _('schedule expired')
67 parameter_name = 'expired'
68 q = Q(expires_at__isnull=False, expires_at__lt=Now())
71 class ScheduleForm(forms.ModelForm):
72 def __init__(self, *args, **kwargs):
73 super().__init__(*args, **kwargs)
74 self.fields['email'].required = False
75 self.fields['method'].required = False
76 self.fields['consent'].required = False
79 model = models.Schedule
83 class SourceFilter(admin.SimpleListFilter):
84 title = _('Source') # display title
85 parameter_name = 'source'
86 template = "admin/long_filter.html"
88 def lookups(self, request, model_admin):
91 model_admin.model.objects.exclude(source='').values_list('source', flat=True).distinct()[:10]
96 def queryset(self, request, queryset):
99 #field_name = 'source' # name of the foreign key field
104 class ScheduleAdmin(admin.ModelAdmin):
108 'email', 'started_at', 'payed_at', 'expires_at', 'amount', 'monthly', 'yearly', 'is_cancelled',
111 list_display_links = ['email', 'started_at']
112 search_fields = ['email', 'source']
114 'is_cancelled', 'monthly', 'yearly', 'method',
115 PayedFilter, ExpiredFilter,
118 filter_horizontal = ['consent']
119 date_hierarchy = 'started_at'
120 raw_id_fields = ['membership']
121 inlines = [PayUOrderInline, PayUCardTokenInline]
122 actions = [export_as_csv_action()]
124 admin.site.register(models.Schedule, ScheduleAdmin)
127 class ScheduleInline(admin.TabularInline):
128 model = models.Schedule
129 fields = ['email', 'amount', 'is_cancelled', 'started_at', 'payed_at', 'expires_at', 'email_sent']
130 readonly_fields = fields
132 show_change_link = True
135 def has_add_permission(self, request, obj):
139 class MembershipAdmin(admin.ModelAdmin):
140 list_display = ['user', 'manual', 'updated_at', 'notes']
141 list_filter = ['manual']
142 date_hierarchy = 'updated_at'
143 raw_id_fields = ['user']
144 search_fields = ['user__username', 'user__email', 'schedule__email', 'notes']
145 inlines = [ScheduleInline]
147 admin.site.register(models.Membership, MembershipAdmin)
150 admin.site.register(models.ReminderEmail, TranslationAdmin)
153 class PayUNotificationAdmin(admin.ModelAdmin):
154 list_display = ['received_at', 'order']
155 fields = ['received_at', 'order', 'body_']
156 readonly_fields = ['received_at', 'body_']
157 raw_id_fields = ['order']
159 def body_(self, obj):
162 conditional_escape(json.dumps(json.loads(obj.body), indent=4))
166 admin.site.register(models.PayUNotification, PayUNotificationAdmin)
169 class PayUNotificationInline(admin.TabularInline):
170 model = models.PayUNotification
171 fields = ['received_at', 'body_']
172 readonly_fields = fields
174 show_change_link = True
177 def body_(self, obj):
180 conditional_escape(json.dumps(json.loads(obj.body), indent=4))
183 def has_add_permission(self, request, obj):
187 class PayUOrderAdmin(admin.ModelAdmin):
188 list_display = ['schedule']
189 raw_id_fields = ['schedule']
190 inlines = [PayUNotificationInline]
193 admin.site.register(models.PayUOrder, PayUOrderAdmin)
196 admin.site.register(models.Ambassador)
201 @admin.register(models.Consent)
202 class ConsentAdmin(admin.ModelAdmin):
203 list_display = ['text', 'order', 'active', 'required']
205 def get_readonly_fields(self, request, obj=None):