Fixes #4001: TOC contains parent books, and add links to previous and next book.
[wolnelektury.git] / src / club / admin.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 import json
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 modeltranslation.admin import TranslationAdmin
12 from wolnelektury.utils import YesNoFilter
13 from . import models
14
15
16 admin.site.register(models.Club)
17
18
19 class PayUOrderInline(admin.TabularInline):
20     model = models.PayUOrder
21     fields = ['order_id', 'status', 'customer_ip']
22     readonly_fields = fields
23     extra = 0
24     show_change_link = True
25     can_delete = False
26
27     def has_add_permission(self, request, obj):
28         return False
29
30
31 class PayUCardTokenInline(admin.TabularInline):
32     model = models.PayUCardToken
33     fields = ['created_at', 'disposable_token', 'reusable_token']
34     readonly_fields = fields
35     extra = 0
36     show_change_link = True
37     can_delete = False
38     show_change_link = True
39
40     def has_add_permission(self, request, obj):
41         return False
42
43
44 class PayedFilter(YesNoFilter):
45     title = _('payment complete')
46     parameter_name = 'payed'
47     q = ~Q(payed_at=None)
48
49
50 class ExpiredFilter(YesNoFilter):
51     title = _('schedule expired')
52     parameter_name = 'expired'
53     q = Q(expires_at__isnull=False, expires_at__lt=Now())
54
55
56 class ScheduleAdmin(admin.ModelAdmin):
57     list_display = ['email', 'started_at', 'payed_at', 'expires_at', 'amount', 'monthly', 'yearly', 'is_cancelled']
58     search_fields = ['email']
59     list_filter = ['is_cancelled', 'monthly', 'yearly', PayedFilter, ExpiredFilter]
60     date_hierarchy = 'started_at'
61     raw_id_fields = ['membership']
62     inlines = [PayUOrderInline, PayUCardTokenInline]
63
64 admin.site.register(models.Schedule, ScheduleAdmin)
65
66
67 class ScheduleInline(admin.TabularInline):
68     model = models.Schedule
69     fields = ['email', 'amount', 'is_cancelled', 'started_at', 'payed_at', 'expires_at', 'email_sent']
70     readonly_fields = fields
71     extra = 0
72     show_change_link = True
73     can_delete = False
74
75     def has_add_permission(self, request, obj):
76         return False
77
78
79 class MembershipAdmin(admin.ModelAdmin):
80     list_display = ['user']
81     raw_id_fields = ['user']
82     search_fields = ['user__username', 'user__email', 'schedule__email']
83     inlines = [ScheduleInline]
84
85 admin.site.register(models.Membership, MembershipAdmin)
86
87
88 admin.site.register(models.ReminderEmail, TranslationAdmin)
89
90
91 class PayUNotificationAdmin(admin.ModelAdmin):
92     list_display = ['received_at', 'order']
93     fields = ['received_at', 'order', 'body_']
94     readonly_fields = ['received_at', 'body_']
95     raw_id_fields = ['order']
96
97     def body_(self, obj):
98         return mark_safe(
99                 "<pre>" +
100                 conditional_escape(json.dumps(json.loads(obj.body), indent=4))
101                 + "</pre>")
102
103
104 admin.site.register(models.PayUNotification, PayUNotificationAdmin)
105
106
107 class PayUNotificationInline(admin.TabularInline):
108     model = models.PayUNotification
109     fields = ['received_at', 'body_']
110     readonly_fields = fields
111     extra = 0
112     show_change_link = True
113     can_delete = False
114
115     def body_(self, obj):
116         return mark_safe(
117                 "<pre>" +
118                 conditional_escape(json.dumps(json.loads(obj.body), indent=4))
119                 + "</pre>")
120
121     def has_add_permission(self, request, obj):
122         return False
123
124
125 class PayUOrderAdmin(admin.ModelAdmin):
126     list_display = ['schedule']
127     raw_id_fields = ['schedule']
128     inlines = [PayUNotificationInline]
129
130
131 admin.site.register(models.PayUOrder, PayUOrderAdmin)
132
133
134 admin.site.register(models.Ambassador)