From 20785520a46a699410126303b3969abf214a68c8 Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Fri, 29 Jul 2016 17:25:33 +0200 Subject: [PATCH 1/1] admin support for newsletter --- src/newsletter/admin.py | 20 ++++++++++++++++++- src/newsletter/models.py | 11 ++++++++-- .../newsletter/subscription/change_list.html | 14 +++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 src/newsletter/templates/admin/newsletter/subscription/change_list.html diff --git a/src/newsletter/admin.py b/src/newsletter/admin.py index 8c38f3f3d..bcb789761 100644 --- a/src/newsletter/admin.py +++ b/src/newsletter/admin.py @@ -1,3 +1,21 @@ +# -*- coding: utf-8 -*- +from django.conf.urls import url 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) diff --git a/src/newsletter/models.py b/src/newsletter/models.py index 481cb85a3..6d100a380 100644 --- a/src/newsletter/models.py +++ b/src/newsletter/models.py @@ -4,7 +4,14 @@ from django.utils.translation import ugettext_lazy as _ 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) + + 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 index 000000000..4a301df30 --- /dev/null +++ b/src/newsletter/templates/admin/newsletter/subscription/change_list.html @@ -0,0 +1,14 @@ +{% extends "admin/change_list.html" %} +{% load i18n %} +{% load admin_urls %} + + +{% block object-tools-items %} +
  • + + {% trans 'Extract' %} + +
  • + {{block.super}} +{% endblock %} + -- 2.20.1