Upgrades.
[wolnelektury.git] / src / newsletter / 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 from django.urls import path
5 from django.contrib import admin
6 from django.http.response import HttpResponse
7 from django.views.decorators.cache import never_cache
8 from modeltranslation.admin import TranslationAdmin
9 from newsletter.models import Subscription, Newsletter
10
11
12 class SubscriptionAdmin(admin.ModelAdmin):
13     list_display = ('email', 'active')
14     list_filter = ('active',)
15
16     def get_urls(self):
17         urls = super(SubscriptionAdmin, self).get_urls()
18         my_urls = [
19             path('extract/', self.extract_subscribers, name='extract_subscribers'),
20         ]
21         return my_urls + urls
22
23     @never_cache
24     def extract_subscribers(self, request):
25         active_subscriptions = Subscription.objects.filter(active=True)
26         return HttpResponse('\n'.join(active_subscriptions.values_list('email', flat=True)),
27                             content_type='text/plain')
28
29
30 admin.site.register(Subscription, SubscriptionAdmin)
31
32
33 admin.site.register(Newsletter, TranslationAdmin)