Fixes #4076: Recommendations from collections.
[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 EmailTemplateAdmin(admin.ModelAdmin):
9     list_display = ['state', 'min_days_since', 'subject', 'min_hour', 'is_active']
10     fieldsets = [
11         (None, {"fields": [
12             'state',
13             ('min_days_since', 'max_days_since'),
14             'is_active',
15             ]}),
16         (_('E-mail content'), {"fields": [
17             'subject', 'body'
18         ]}),
19         (_('Sending constraints'), {"fields": [
20             ('min_day_of_month', 'max_day_of_month'),
21             ('dow_1', 'dow_2', 'dow_3', 'dow_4', 'dow_5', 'dow_6', 'dow_7'),
22             ('min_hour', 'max_hour'),
23         ]}),
24     ]
25
26     def _test_email(self, request, obj):
27         if request.user.email:
28             obj.send_test_email(request.user.email)
29             messages.info(request, _('Test e-mail has been sent to %(email)s.') % {"email": request.user.email})
30         else:
31             messages.warning(request, _('You have no email set. Test e-mail not sent.'))
32
33     def response_add(self, request, obj):
34         self._test_email(request, obj)
35         return super().response_add(request, obj)
36
37     def response_change(self, request, obj):
38         self._test_email(request, obj)
39         return super().response_change(request, obj)
40
41
42 admin.site.register(models.EmailTemplate, EmailTemplateAdmin)
43
44
45 class EmailSentAdmin(admin.ModelAdmin):
46     list_filter = ['template']
47     list_display = ['timestamp', 'template', 'contact', 'subject']
48     fields = ['timestamp', 'template', 'contact', 'subject', 'body']
49     readonly_fields = fields
50     change_links = ['template']
51
52
53 admin.site.register(models.EmailSent, EmailSentAdmin)
54
55
56 class ContactEmailSentInline(admin.TabularInline):
57     model = models.EmailSent
58     fields = ['timestamp', 'template', 'subject']
59     readonly_fields = ['timestamp', 'template', 'subject']
60     extra = 0
61     can_delete = False
62     show_change_link = True
63
64     def has_add_permission(self, request, obj):
65         return False
66
67
68 class ContactAdmin(admin.ModelAdmin):
69     inlines = [ContactEmailSentInline]
70     list_filter = ['level']
71     list_display = ['email', 'level', 'since', 'expires_at']
72     search_fields = ['email']
73     date_hierarchy = 'since'
74     actions = [export_as_csv_action(fields=['id', 'email', 'get_level_display', 'since', 'expires_at'])]
75
76
77 admin.site.register(models.Contact, ContactAdmin)