Fixes #4076: Recommendations from collections.
[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 fnpdjango.actions import export_as_csv_action
12 from modeltranslation.admin import TranslationAdmin
13 from wolnelektury.utils import YesNoFilter
14 from . import models
15
16
17 admin.site.register(models.Club)
18
19
20 class PayUOrderInline(admin.TabularInline):
21     model = models.PayUOrder
22     fields = ['order_id', 'status', 'customer_ip']
23     readonly_fields = fields
24     extra = 0
25     show_change_link = True
26     can_delete = False
27
28     def has_add_permission(self, request, obj):
29         return False
30
31
32 class PayUCardTokenInline(admin.TabularInline):
33     model = models.PayUCardToken
34     fields = ['created_at', 'disposable_token', 'reusable_token']
35     readonly_fields = fields
36     extra = 0
37     show_change_link = True
38     can_delete = False
39     show_change_link = True
40
41     def has_add_permission(self, request, obj):
42         return False
43
44
45 class PayedFilter(YesNoFilter):
46     title = _('payment complete')
47     parameter_name = 'payed'
48     q = ~Q(payed_at=None)
49
50
51 class ExpiredFilter(YesNoFilter):
52     title = _('schedule expired')
53     parameter_name = 'expired'
54     q = Q(expires_at__isnull=False, expires_at__lt=Now())
55
56
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()]
65
66 admin.site.register(models.Schedule, ScheduleAdmin)
67
68
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
73     extra = 0
74     show_change_link = True
75     can_delete = False
76
77     def has_add_permission(self, request, obj):
78         return False
79
80
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]
86
87 admin.site.register(models.Membership, MembershipAdmin)
88
89
90 admin.site.register(models.ReminderEmail, TranslationAdmin)
91
92
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']
98
99     def body_(self, obj):
100         return mark_safe(
101                 "<pre>" +
102                 conditional_escape(json.dumps(json.loads(obj.body), indent=4))
103                 + "</pre>")
104
105
106 admin.site.register(models.PayUNotification, PayUNotificationAdmin)
107
108
109 class PayUNotificationInline(admin.TabularInline):
110     model = models.PayUNotification
111     fields = ['received_at', 'body_']
112     readonly_fields = fields
113     extra = 0
114     show_change_link = True
115     can_delete = False
116
117     def body_(self, obj):
118         return mark_safe(
119                 "<pre>" +
120                 conditional_escape(json.dumps(json.loads(obj.body), indent=4))
121                 + "</pre>")
122
123     def has_add_permission(self, request, obj):
124         return False
125
126
127 class PayUOrderAdmin(admin.ModelAdmin):
128     list_display = ['schedule']
129     raw_id_fields = ['schedule']
130     inlines = [PayUNotificationInline]
131
132
133 admin.site.register(models.PayUOrder, PayUOrderAdmin)
134
135
136 admin.site.register(models.Ambassador)