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):
58 list_display = ['email', 'started_at', 'payed_at', 'expires_at', 'amount', 'monthly', 'yearly', 'is_cancelled']
59 search_fields = ['email']
60 list_filter = ['is_cancelled', 'monthly', 'yearly', PayedFilter, ExpiredFilter]
61 date_hierarchy = 'started_at'
62 raw_id_fields = ['membership']
63 inlines = [PayUOrderInline, PayUCardTokenInline]
64 actions = [export_as_csv_action()]
66 admin.site.register(models.Schedule, ScheduleAdmin)
69 class ScheduleInline(admin.TabularInline):
70 model = models.Schedule
71 fields = ['email', 'amount', 'is_cancelled', 'started_at', 'payed_at', 'expires_at', 'email_sent']
72 readonly_fields = fields
74 show_change_link = True
77 def has_add_permission(self, request, obj):
81 class MembershipAdmin(admin.ModelAdmin):
82 list_display = ['user', 'manual', 'updated_at', 'notes']
83 list_filter = ['manual']
84 date_hierarchy = 'updated_at'
85 raw_id_fields = ['user']
86 search_fields = ['user__username', 'user__email', 'schedule__email', 'notes']
87 inlines = [ScheduleInline]
89 admin.site.register(models.Membership, MembershipAdmin)
92 admin.site.register(models.ReminderEmail, TranslationAdmin)
95 class PayUNotificationAdmin(admin.ModelAdmin):
96 list_display = ['received_at', 'order']
97 fields = ['received_at', 'order', 'body_']
98 readonly_fields = ['received_at', 'body_']
99 raw_id_fields = ['order']
101 def body_(self, obj):
104 conditional_escape(json.dumps(json.loads(obj.body), indent=4))
108 admin.site.register(models.PayUNotification, PayUNotificationAdmin)
111 class PayUNotificationInline(admin.TabularInline):
112 model = models.PayUNotification
113 fields = ['received_at', 'body_']
114 readonly_fields = fields
116 show_change_link = True
119 def body_(self, obj):
122 conditional_escape(json.dumps(json.loads(obj.body), indent=4))
125 def has_add_permission(self, request, obj):
129 class PayUOrderAdmin(admin.ModelAdmin):
130 list_display = ['schedule']
131 raw_id_fields = ['schedule']
132 inlines = [PayUNotificationInline]
135 admin.site.register(models.PayUOrder, PayUOrderAdmin)
138 admin.site.register(models.Ambassador)