Fix
[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.utils.html import conditional_escape
7 from django.utils.safestring import mark_safe
8 from modeltranslation.admin import TranslationAdmin
9 from . import models
10
11
12 class PlanAdmin(admin.ModelAdmin):
13     list_display = ['min_amount', 'interval']
14
15 admin.site.register(models.Plan, PlanAdmin)
16
17
18 class PayUOrderInline(admin.TabularInline):
19     model = models.PayUOrder
20     fields = ['order_id', 'status', 'customer_ip']
21     readonly_fields = fields
22     extra = 0
23     show_change_link = True
24     can_delete = False
25
26     def has_add_permission(self, request, obj):
27         return False
28
29
30 class PayUCardTokenInline(admin.TabularInline):
31     model = models.PayUCardToken
32     fields = ['created_at', 'disposable_token', 'reusable_token']
33     readonly_fields = fields
34     extra = 0
35     show_change_link = True
36     can_delete = False
37     show_change_link = True
38
39     def has_add_permission(self, request, obj):
40         return False
41
42
43 class ScheduleAdmin(admin.ModelAdmin):
44     list_display = ['email', 'started_at', 'payed_at', 'expires_at', 'plan', 'amount', 'is_cancelled']
45     list_search = ['email']
46     list_filter = ['is_cancelled']
47     date_hierarchy = 'started_at'
48     raw_id_fields = ['membership']
49     inlines = [PayUOrderInline, PayUCardTokenInline]
50
51 admin.site.register(models.Schedule, ScheduleAdmin)
52
53
54 class ScheduleInline(admin.TabularInline):
55     model = models.Schedule
56     fields = ['email', 'plan', 'amount', 'method', 'is_cancelled', 'started_at', 'payed_at', 'expires_at', 'email_sent']
57     readonly_fields = fields
58     extra = 0
59     show_change_link = True
60     can_delete = False
61
62     def has_add_permission(self, request, obj):
63         return False
64
65
66 class MembershipAdmin(admin.ModelAdmin):
67     list_display = ['user']
68     raw_id_fields = ['user']
69     search_fields = ['user__username', 'user__email', 'schedule__email']
70     inlines = [ScheduleInline]
71
72 admin.site.register(models.Membership, MembershipAdmin)
73
74
75 admin.site.register(models.ReminderEmail, TranslationAdmin)
76
77
78 class PayUNotificationAdmin(admin.ModelAdmin):
79     list_display = ['received_at', 'order']
80     fields = ['received_at', 'order', 'body_']
81     readonly_fields = ['received_at', 'body_']
82     raw_id_fields = ['order']
83
84     def body_(self, obj):
85         return mark_safe(
86                 "<pre>" +
87                 conditional_escape(json.dumps(json.loads(obj.body), indent=4))
88                 + "</pre>")
89
90
91 admin.site.register(models.PayUNotification, PayUNotificationAdmin)
92
93
94 class PayUNotificationInline(admin.TabularInline):
95     model = models.PayUNotification
96     fields = ['received_at', 'body_']
97     readonly_fields = fields
98     extra = 0
99     show_change_link = True
100     can_delete = False
101
102     def body_(self, obj):
103         return mark_safe(
104                 "<pre>" +
105                 conditional_escape(json.dumps(json.loads(obj.body), indent=4))
106                 + "</pre>")
107
108     def has_add_permission(self, request, obj):
109         return False
110
111
112 class PayUOrderAdmin(admin.ModelAdmin):
113     list_display = ['schedule']
114     raw_id_fields = ['schedule']
115     inlines = [PayUNotificationInline]
116
117
118 admin.site.register(models.PayUOrder, PayUOrderAdmin)