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