Commit 819e5bf2 authored by fred's avatar fred

allow sending newsletter

parent 69fae094
# -*- coding: utf8 -*-
import datetime
import hashlib
import random
import smtplib
import subprocess
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import html2text
from django.db import models
from django.template import loader, Context
......@@ -61,3 +68,31 @@ class Newsletter(models.Model):
text = RichTextField(_('Text'), null=True, blank=True)
expedition_datetime = models.DateTimeField(_('Expedition Date/time'), null=True, blank=True)
def send(self):
msg = MIMEMultipart('alternative')
msg['Subject'] = self.subject
msg['From'] = settings.NEWSLETTER_SENDER
if settings.DEBUG:
msg['To'] = 'fred@localhost'
else:
msg['To'] = '%s@%s' % (settings.NEWSLETTER_NAME, settings.NEWSLETTER_DOMAIN)
h2t = html2text.HTML2Text()
h2t.unicode_snob = True
html = self.text.encode('utf-8')
if settings.NEWSLETTER_STYLE:
html += '''\n<style type="text/css">\n%s\n</style>''' % settings.NEWSLETTER_STYLE
part1 = MIMEText(h2t.handle(self.text).encode('utf-8'), 'plain', _charset='utf-8')
part2 = MIMEText(html, 'html', _charset='utf-8')
msg.attach(part1)
msg.attach(part2)
s = smtplib.SMTP('localhost')
s.sendmail(msg['From'], msg['To'], msg.as_string())
s.quit()
self.expedition_datetime = datetime.datetime.now()
self.save()
{% extends "base.html" %}
{% block appbar %}
<h2>{{ object.date }}</h2>
<h2>{{ object.date }} - {{ object.subject }}</h2>
{% endblock %}
{% block content %}
......@@ -9,7 +9,7 @@
{% if perms.newsletter.add_newsletter %}
<p>
<a class="big-friendly-button" href="{% url 'newsletter-edit' pk=object.id %}">Modifier</a>
<a class="big-friendly-button" href="#">Envoyer</a>
<a class="big-friendly-button" href="{% url 'newsletter-send' pk=object.id %}">Envoyer</a>
</p>
{% endif %}
......
......@@ -15,4 +15,5 @@ management_patterns = patterns('newsletter.views',
url(r'^add$', 'newsletter_create', name='newsletter-create'),
url(r'^(?P<pk>\d+)/$', 'newsletter_view', name='newsletter-view'),
url(r'^(?P<pk>\d+)/edit$', 'newsletter_update', name='newsletter-edit'),
url(r'^(?P<pk>\d+)/send$', 'newsletter_send', name='newsletter-send'),
)
# -*- coding: utf8 -*-
import email
import hashlib
import random
import socket
......@@ -8,8 +9,6 @@ import urllib2
import cookielib
import datetime
from smtplib import SMTPException
from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse_lazy
......@@ -17,7 +16,7 @@ from django.db import models, IntegrityError
from django.shortcuts import get_object_or_404
from django.utils.translation import ugettext as _
from django.views.generic.base import TemplateView
from django.views.generic.base import TemplateView, RedirectView
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from django.views.generic.edit import CreateView, UpdateView
......@@ -131,6 +130,17 @@ class NewsletterUpdateView(UpdateView):
newsletter_update = NewsletterUpdateView.as_view()
class NewsletterSendView(RedirectView):
def get_redirect_url(self, pk):
if not self.request.user.has_perm('newsletter.add_newsletter'):
raise PermissionDenied()
newsletter = get_object_or_404(Newsletter, pk=pk)
newsletter.send()
return reverse_lazy('newsletter-list')
newsletter_send = NewsletterSendView.as_view()
@csrf_exempt
@to_json('api')
def stats(request) :
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment