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']
83 raw_id_fields = ['user']
84 search_fields = ['user__username', 'user__email', 'schedule__email']
85 inlines = [ScheduleInline]
87 admin.site.register(models.Membership, MembershipAdmin)
90 admin.site.register(models.ReminderEmail, TranslationAdmin)
93 class PayUNotificationAdmin(admin.ModelAdmin):
94 list_display = ['received_at', 'order']
95 fields = ['received_at', 'order', 'body_']
96 readonly_fields = ['received_at', 'body_']
97 raw_id_fields = ['order']
102 conditional_escape(json.dumps(json.loads(obj.body), indent=4))
106 admin.site.register(models.PayUNotification, PayUNotificationAdmin)
109 class PayUNotificationInline(admin.TabularInline):
110 model = models.PayUNotification
111 fields = ['received_at', 'body_']
112 readonly_fields = fields
114 show_change_link = True
117 def body_(self, obj):
120 conditional_escape(json.dumps(json.loads(obj.body), indent=4))
123 def has_add_permission(self, request, obj):
127 class PayUOrderAdmin(admin.ModelAdmin):
128 list_display = ['schedule']
129 raw_id_fields = ['schedule']
130 inlines = [PayUNotificationInline]
133 admin.site.register(models.PayUOrder, PayUOrderAdmin)
136 admin.site.register(models.Ambassador)