admin support for newsletter
authorJan Szejko <jan.szejko@gmail.com>
Fri, 29 Jul 2016 15:25:33 +0000 (17:25 +0200)
committerJan Szejko <janek37@gmail.com>
Wed, 14 Sep 2016 10:47:47 +0000 (12:47 +0200)
src/newsletter/admin.py
src/newsletter/models.py
src/newsletter/templates/admin/newsletter/subscription/change_list.html [new file with mode: 0644]

index 8c38f3f..bcb7897 100644 (file)
@@ -1,3 +1,21 @@
+# -*- coding: utf-8 -*-
+from django.conf.urls import url
 from django.contrib import admin
 from django.contrib import admin
+from django.http.response import HttpResponse
 
 
-# Register your models here.
+from newsletter.models import Subscription
+
+
+class SubscriptionAdmin(admin.ModelAdmin):
+    def get_urls(self):
+        urls = super(SubscriptionAdmin, self).get_urls()
+        my_urls = [
+            url(r'^extract/$', self.extract_subscribers, name='extract_subscribers'),
+        ]
+        return my_urls + urls
+
+    def extract_subscribers(self, request):
+        return HttpResponse(',\n'.join(Subscription.objects.values_list('email', flat=True)), content_type='text/plain')
+
+
+admin.site.register(Subscription, SubscriptionAdmin)
index 481cb85..6d100a3 100644 (file)
@@ -4,7 +4,14 @@ from django.utils.translation import ugettext_lazy as _
 
 
 class Subscription(Model):
 
 
 class Subscription(Model):
-    email = EmailField(verbose_name=_('email address'))
-    active = BooleanField(default=True)
+    email = EmailField(verbose_name=_('email address'), unique=True)
+    active = BooleanField(default=True, verbose_name=_(u'active'))
     created_at = DateTimeField(auto_now_add=True)
     last_modified = DateTimeField(auto_now=True)
     created_at = DateTimeField(auto_now_add=True)
     last_modified = DateTimeField(auto_now=True)
+
+    class Meta:
+        verbose_name = _('subscription')
+        verbose_name_plural = _('subscriptions')
+
+    def __unicode__(self):
+        return self.email
\ No newline at end of file
diff --git a/src/newsletter/templates/admin/newsletter/subscription/change_list.html b/src/newsletter/templates/admin/newsletter/subscription/change_list.html
new file mode 100644 (file)
index 0000000..4a301df
--- /dev/null
@@ -0,0 +1,14 @@
+{% extends "admin/change_list.html" %}
+{% load i18n %}
+{% load admin_urls %}
+
+
+{% block object-tools-items %}
+  <li>
+    <a href="{% url 'admin:extract_subscribers' %}">
+      {% trans 'Extract' %}
+    </a>
+  </li>
+  {{block.super}}
+{% endblock %}
+