2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 from django.contrib import admin
7 from django.db.models.functions import Now
8 from django.db.models import Q
9 from django import forms
10 from django.utils.html import conditional_escape
11 from django.utils.safestring import mark_safe
12 from django.utils.translation import gettext_lazy as _
13 from fnpdjango.actions import export_as_csv_action
14 from modeltranslation.admin import TranslationAdmin
15 from wolnelektury.utils import YesNoFilter
19 class SingleAmountInline(admin.TabularInline):
20 model = models.SingleAmount
23 class MonthlyAmountInline(admin.TabularInline):
24 model = models.MonthlyAmount
27 @admin.register(models.Club)
28 class ClubAdmin(admin.ModelAdmin):
35 class PayUOrderInline(admin.TabularInline):
36 model = models.PayUOrder
37 fields = ['order_id', 'status', 'customer_ip']
38 readonly_fields = fields
40 show_change_link = True
43 def has_add_permission(self, request, obj):
47 class PayUCardTokenInline(admin.TabularInline):
48 model = models.PayUCardToken
49 fields = ['created_at', 'disposable_token', 'reusable_token']
50 readonly_fields = fields
52 show_change_link = True
54 show_change_link = True
56 def has_add_permission(self, request, obj):
60 class PayedFilter(YesNoFilter):
61 title = _('payment complete')
62 parameter_name = 'payed'
66 class ExpiredFilter(YesNoFilter):
67 title = _('schedule expired')
68 parameter_name = 'expired'
69 q = Q(expires_at__isnull=False, expires_at__lt=Now())
72 class ScheduleForm(forms.ModelForm):
73 def __init__(self, *args, **kwargs):
74 super().__init__(*args, **kwargs)
75 self.fields['email'].required = False
76 self.fields['method'].required = False
77 self.fields['consent'].required = False
80 model = models.Schedule
84 class ScheduleAdmin(admin.ModelAdmin):
88 'email', 'started_at', 'payed_at', 'expires_at', 'amount', 'monthly', 'yearly', 'is_cancelled',
91 list_display_links = ['email', 'started_at']
92 search_fields = ['email']
93 list_filter = ['is_cancelled', 'monthly', 'yearly', 'method', PayedFilter, ExpiredFilter, 'source']
94 filter_horizontal = ['consent']
95 date_hierarchy = 'started_at'
96 raw_id_fields = ['membership']
97 inlines = [PayUOrderInline, PayUCardTokenInline]
98 actions = [export_as_csv_action()]
100 admin.site.register(models.Schedule, ScheduleAdmin)
103 class ScheduleInline(admin.TabularInline):
104 model = models.Schedule
105 fields = ['email', 'amount', 'is_cancelled', 'started_at', 'payed_at', 'expires_at', 'email_sent']
106 readonly_fields = fields
108 show_change_link = True
111 def has_add_permission(self, request, obj):
115 class MembershipAdmin(admin.ModelAdmin):
116 list_display = ['user', 'manual', 'updated_at', 'notes']
117 list_filter = ['manual']
118 date_hierarchy = 'updated_at'
119 raw_id_fields = ['user']
120 search_fields = ['user__username', 'user__email', 'schedule__email', 'notes']
121 inlines = [ScheduleInline]
123 admin.site.register(models.Membership, MembershipAdmin)
126 admin.site.register(models.ReminderEmail, TranslationAdmin)
129 class PayUNotificationAdmin(admin.ModelAdmin):
130 list_display = ['received_at', 'order']
131 fields = ['received_at', 'order', 'body_']
132 readonly_fields = ['received_at', 'body_']
133 raw_id_fields = ['order']
135 def body_(self, obj):
138 conditional_escape(json.dumps(json.loads(obj.body), indent=4))
142 admin.site.register(models.PayUNotification, PayUNotificationAdmin)
145 class PayUNotificationInline(admin.TabularInline):
146 model = models.PayUNotification
147 fields = ['received_at', 'body_']
148 readonly_fields = fields
150 show_change_link = True
153 def body_(self, obj):
156 conditional_escape(json.dumps(json.loads(obj.body), indent=4))
159 def has_add_permission(self, request, obj):
163 class PayUOrderAdmin(admin.ModelAdmin):
164 list_display = ['schedule']
165 raw_id_fields = ['schedule']
166 inlines = [PayUNotificationInline]
169 admin.site.register(models.PayUOrder, PayUOrderAdmin)
172 admin.site.register(models.Ambassador)
177 @admin.register(models.Consent)
178 class ConsentAdmin(admin.ModelAdmin):
179 list_display = ['text', 'order', 'active', 'required']
181 def get_readonly_fields(self, request, obj=None):