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 admin.site.register(models.Club)
20 class PayUOrderInline(admin.TabularInline):
21 model = models.PayUOrder
22 fields = ['order_id', 'status', 'customer_ip']
23 readonly_fields = fields
25 show_change_link = True
28 def has_add_permission(self, request, obj):
32 class PayUCardTokenInline(admin.TabularInline):
33 model = models.PayUCardToken
34 fields = ['created_at', 'disposable_token', 'reusable_token']
35 readonly_fields = fields
37 show_change_link = True
39 show_change_link = True
41 def has_add_permission(self, request, obj):
45 class PayedFilter(YesNoFilter):
46 title = _('payment complete')
47 parameter_name = 'payed'
51 class ExpiredFilter(YesNoFilter):
52 title = _('schedule expired')
53 parameter_name = 'expired'
54 q = Q(expires_at__isnull=False, expires_at__lt=Now())
57 class ScheduleAdmin(admin.ModelAdmin):
59 'email', 'started_at', 'payed_at', 'expires_at', 'amount', 'monthly', 'yearly', 'is_cancelled',
62 search_fields = ['email']
63 list_filter = ['is_cancelled', 'monthly', 'yearly', 'method', PayedFilter, ExpiredFilter, 'source']
64 filter_horizontal = ['consent']
65 date_hierarchy = 'started_at'
66 raw_id_fields = ['membership']
67 inlines = [PayUOrderInline, PayUCardTokenInline]
68 actions = [export_as_csv_action()]
70 admin.site.register(models.Schedule, ScheduleAdmin)
73 class ScheduleInline(admin.TabularInline):
74 model = models.Schedule
75 fields = ['email', 'amount', 'is_cancelled', 'started_at', 'payed_at', 'expires_at', 'email_sent']
76 readonly_fields = fields
78 show_change_link = True
81 def has_add_permission(self, request, obj):
85 class MembershipAdmin(admin.ModelAdmin):
86 list_display = ['user', 'manual', 'updated_at', 'notes']
87 list_filter = ['manual']
88 date_hierarchy = 'updated_at'
89 raw_id_fields = ['user']
90 search_fields = ['user__username', 'user__email', 'schedule__email', 'notes']
91 inlines = [ScheduleInline]
93 admin.site.register(models.Membership, MembershipAdmin)
96 admin.site.register(models.ReminderEmail, TranslationAdmin)
99 class PayUNotificationAdmin(admin.ModelAdmin):
100 list_display = ['received_at', 'order']
101 fields = ['received_at', 'order', 'body_']
102 readonly_fields = ['received_at', 'body_']
103 raw_id_fields = ['order']
105 def body_(self, obj):
108 conditional_escape(json.dumps(json.loads(obj.body), indent=4))
112 admin.site.register(models.PayUNotification, PayUNotificationAdmin)
115 class PayUNotificationInline(admin.TabularInline):
116 model = models.PayUNotification
117 fields = ['received_at', 'body_']
118 readonly_fields = fields
120 show_change_link = True
123 def body_(self, obj):
126 conditional_escape(json.dumps(json.loads(obj.body), indent=4))
129 def has_add_permission(self, request, obj):
133 class PayUOrderAdmin(admin.ModelAdmin):
134 list_display = ['schedule']
135 raw_id_fields = ['schedule']
136 inlines = [PayUNotificationInline]
139 admin.site.register(models.PayUOrder, PayUOrderAdmin)
142 admin.site.register(models.Ambassador)
146 @admin.register(models.DirectDebit)
147 class DirectDebitAdmin(admin.ModelAdmin):
148 list_display = ['date', 'amount', 'first_name', 'last_name']
150 ('first_name', 'sex', 'date_of_birth'),
152 ('street', 'building'),
154 ('postal_code', 'phone'),
164 @admin.register(models.Consent)
165 class ConsentAdmin(admin.ModelAdmin):
166 list_display = ['text', 'order', 'active', 'required']
168 def get_readonly_fields(self, request, obj=None):