Fixes #4023: nicer club schedule filtering.
[wolnelektury.git] / src / messaging / admin.py
1 from django.contrib import admin
2 from django.contrib import messages
3 from django.utils.translation import ugettext_lazy as _
4 from fnpdjango.actions import export_as_csv_action
5 from . import models
6
7
8 class EmailSentInline(admin.TabularInline):
9     model = models.EmailSent
10     fields = ['timestamp', 'contact', 'subject']
11     readonly_fields = ['timestamp', 'contact', 'subject']
12     extra = 0
13     can_delete = False
14     show_change_link = True
15
16     def has_add_permission(self, request, obj):
17         return False
18
19
20 class EmailTemplateAdmin(admin.ModelAdmin):
21     list_display = ['state', 'min_days_since', 'subject', 'min_hour']
22     inlines = [EmailSentInline]
23     fieldsets = [
24         (None, {"fields": [
25             'state',
26             ('min_days_since', 'max_days_since'),
27             'is_active',
28             ]}),
29         (_('E-mail content'), {"fields": [
30             'subject', 'body'
31         ]}),
32         (_('Sending constraints'), {"fields": [
33             ('min_day_of_month', 'max_day_of_month'),
34             ('dow_1', 'dow_2', 'dow_3', 'dow_4', 'dow_5', 'dow_6', 'dow_7'),
35             ('min_hour', 'max_hour'),
36         ]}),
37     ]
38
39     def _test_email(self, request, obj):
40         if request.user.email:
41             obj.send_test_email(request.user.email)
42             messages.info(request, _('Test e-mail has been sent to %(email)s.') % {"email": request.user.email})
43         else:
44             messages.warning(request, _('You have no email set. Test e-mail not sent.'))
45
46     def response_add(self, request, obj):
47         self._test_email(request, obj)
48         return super().response_add(request, obj)
49
50     def response_change(self, request, obj):
51         self._test_email(request, obj)
52         return super().response_change(request, obj)
53
54
55 admin.site.register(models.EmailTemplate, EmailTemplateAdmin)
56
57
58 class EmailSentAdmin(admin.ModelAdmin):
59     list_filter = ['template']
60     list_display = ['timestamp', 'template', 'contact', 'subject']
61     fields = ['timestamp', 'template', 'contact', 'subject', 'body']
62     readonly_fields = fields
63     change_links = ['template']
64
65
66 admin.site.register(models.EmailSent, EmailSentAdmin)
67
68
69 class ContactEmailSentInline(admin.TabularInline):
70     model = models.EmailSent
71     fields = ['timestamp', 'template', 'subject']
72     readonly_fields = ['timestamp', 'template', 'subject']
73     extra = 0
74     can_delete = False
75     show_change_link = True
76
77     def has_add_permission(self, request, obj):
78         return False
79
80
81 class ContactAdmin(admin.ModelAdmin):
82     inlines = [ContactEmailSentInline]
83     list_filter = ['level']
84     list_display = ['email', 'level', 'since', 'expires_at']
85     search_fields = ['email']
86     date_hierarchy = 'since'
87     actions = [export_as_csv_action(fields=['id', 'email', 'get_level_display', 'since', 'expires_at'])]
88
89
90 admin.site.register(models.Contact, ContactAdmin)