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
8 class EmailSentInline(admin.TabularInline):
9 model = models.EmailSent
10 fields = ['timestamp', 'contact', 'subject']
11 readonly_fields = ['timestamp', 'contact', 'subject']
14 show_change_link = True
16 def has_add_permission(self, request, obj):
20 class EmailTemplateAdmin(admin.ModelAdmin):
21 list_display = ['state', 'min_days_since', 'subject', 'min_hour']
22 inlines = [EmailSentInline]
26 ('min_days_since', 'max_days_since'),
29 (_('E-mail content'), {"fields": [
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'),
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})
44 messages.warning(request, _('You have no email set. Test e-mail not sent.'))
46 def response_add(self, request, obj):
47 self._test_email(request, obj)
48 return super().response_add(request, obj)
50 def response_change(self, request, obj):
51 self._test_email(request, obj)
52 return super().response_change(request, obj)
55 admin.site.register(models.EmailTemplate, EmailTemplateAdmin)
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']
66 admin.site.register(models.EmailSent, EmailSentAdmin)
69 class ContactEmailSentInline(admin.TabularInline):
70 model = models.EmailSent
71 fields = ['timestamp', 'template', 'subject']
72 readonly_fields = ['timestamp', 'template', 'subject']
75 show_change_link = True
77 def has_add_permission(self, request, obj):
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'])]
90 admin.site.register(models.Contact, ContactAdmin)