Project

General

Profile

0001-unsubscribe-link-10795.patch

Serghei Mihai, 24 June 2016 09:39 AM

Download (8.17 KB)

View differences:

Subject: [PATCH] unsubscribe link (#10795)

 corbo/models.py                                    | 16 +++++++++++---
 corbo/static/css/corbo.css                         | 17 +++++++++++++++
 corbo/templates/corbo/announce.html                |  9 ++++++++
 .../corbo/subscription_confirm_delete.html         | 18 ++++++++++++++++
 corbo/templates/corbo/unsubscription.html          | 13 +++++++++++
 corbo/templates/corbo/unsubscription_done.html     | 10 +++++++++
 corbo/urls.py                                      |  8 +++++--
 corbo/views.py                                     | 25 ++++++++++++++++++++++
 8 files changed, 111 insertions(+), 5 deletions(-)
 create mode 100644 corbo/templates/corbo/announce.html
 create mode 100644 corbo/templates/corbo/subscription_confirm_delete.html
 create mode 100644 corbo/templates/corbo/unsubscription.html
 create mode 100644 corbo/templates/corbo/unsubscription_done.html
corbo/models.py
9 9
from django.db import models
10 10
from django.core.files.storage import DefaultStorage
11 11
from django.utils.translation import ugettext_lazy as _
12
from django.core import signing
13
from django.template import loader, Context
14
from django.core.urlresolvers import reverse
12 15

  
13 16
from ckeditor.fields import RichTextField
14 17

  
......
75 78
        subscriptions = self.announce.category.subscription_set.all()
76 79
        total_sent = 0
77 80
        handler = HTML2Text()
78
        m = Message(html=self.announce.text, subject=self.announce.title,
79
                    text=handler.handle(self.announce.text),
80
                    mail_from=settings.DEFAULT_FROM_EMAIL)
81
        template = loader.get_template('corbo/announce.html')
82
        m = Message(subject=self.announce.title, mail_from=settings.DEFAULT_FROM_EMAIL)
81 83
        html_tree = HTMLTree(self.announce.text)
82 84
        storage = DefaultStorage()
83 85
        for img in html_tree.xpath('//img/@src'):
......
89 91
        for s in subscriptions:
90 92
            if not s.identifier:
91 93
                continue
94
            unsubscribe_token = signing.dumps({'category': self.announce.category.pk,
95
                                               'identifier': s.identifier})
96
            unsubscribe_link = reverse('unsubscribe', kwargs={'unsubscription_token': unsubscribe_token})
97
            message = template.render(Context({'unsubscribe_link': unsubscribe_link,
98
                                               'content': self.announce.text}))
99
            m.html = message
100
            m.text=handler.handle(message)
101

  
92 102
            result = m.send(to=s.identifier)
93 103
            if result.status_code == 250:
94 104
                total_sent += 1
corbo/static/css/corbo.css
280 280

  
281 281
#id_transport_channel li, #id_transport_channel label {
282 282
    display: inline;
283
}
284

  
285
.content {
286
    width: 50%;
287
    margin: auto;
288
}
289

  
290
.unsubscription {
291
    background: #e6db74;
292
    border: 1px solid #aaa;
293
    padding: 5px;
294
}
295

  
296
.info {
297
    background: #a6e22e;
298
    border: 1px solid #aaa;
299
    padding: 5px;
283 300
}
corbo/templates/corbo/announce.html
1
{% load i18n %}
2
<div class="content">
3
  {{ content|safe }}
4
</div>
5
<div class="footer">
6
  {% blocktrans %}
7
  Click <a href='{{ unsubscribe_link}}'>here</a> to unsubscribe from this newsletter.
8
  {% endblocktrans %}
9
</div>
corbo/templates/corbo/subscription_confirm_delete.html
1
{% extends 'corbo/unsubscription.html' %}
2
{% load i18n %}
3

  
4
{% block title %}{% blocktrans with object.category as category %}
5
Unsubscription from {{ category }}
6
{% endblocktrans %}{% endblock %}
7

  
8
{% block content %}
9
<div class='unsubscription'>
10
{% blocktrans with object.category as category %}
11
Are you sure you want to unsubscribe from "{{ category }}"?
12
{% endblocktrans %}
13
<form method='POST'>
14
  {% csrf_token %}
15
  <button>{% trans "Unsubscribe" %}</button>
16
</form>
17
</div>
18
{% endblock %}
corbo/templates/corbo/unsubscription.html
1
{% load static %}
2
<html>
3
  <head>
4
    <title>{% block title %}{% endblock %}</title>
5
    <link rel='stylesheet' type='text/css' href='{% static "css/corbo.css" %}' />
6
  </head>
7
  <body>
8
    <div class='content'>
9
    {% block content %}
10
    {% endblock %}
11
    </div>
12
  </body>
13
</html>
corbo/templates/corbo/unsubscription_done.html
1
{% extends 'corbo/unsubscription.html' %}
2
{% load i18n %}
3

  
4
{% block title %}
5
{% trans 'Successfully unsubscription' %}
6
{% endblock %}
7

  
8
{% block content %}
9
<div class='info'>{% trans 'You were sucessfully unsubcribed.' %}</div>
10
{% endblock %}
corbo/urls.py
5 5
from django.contrib import admin
6 6

  
7 7
from .urls_utils import decorated_includes, manager_required
8
from .views import homepage, atom
8
from .views import homepage, atom, unsubscribe, unsubscription_done
9 9

  
10 10
from manage_urls import urlpatterns as manage_urls
11 11
from api_urls import urlpatterns as api_urls
......
17 17
                    include(manage_urls))),
18 18
    url(r'^ckeditor/', include('ckeditor.urls')),
19 19
    url(r'^admin/', include(admin.site.urls)),
20
    url(r'^api/', include(api_urls))
20
    url(r'^api/', include(api_urls)),
21
    url(r'^unsubscribe/done/$', unsubscription_done,
22
        name='unsubscription_done'),
23
    url(r'^unsubscribe/(?P<unsubscription_token>[\w:-]+)$', unsubscribe,
24
        name='unsubscribe'),
21 25
)
22 26

  
23 27
if 'mellon' in settings.INSTALLED_APPS:
corbo/views.py
1 1
from datetime import datetime
2 2

  
3 3
from django.conf import settings
4
from django.core import signing
4 5
from django.core.urlresolvers import reverse
5 6
from django.views.generic import CreateView, UpdateView, DeleteView, \
6 7
                                 ListView, TemplateView
7 8
from django.contrib.syndication.views import Feed
8 9
from django.utils.feedgenerator import Atom1Feed
9 10
from django.utils.http import urlencode
11
from django.http import Http404
10 12

  
11 13
import models
12 14
from .forms import AnnounceForm, CategoryForm
......
79 81
delete_category = CategoryDeleteView.as_view()
80 82

  
81 83

  
84
class UnsubscribeView(DeleteView):
85
    model = models.Subscription
86

  
87
    def get_object(self, queryset=None):
88
        data = signing.loads(self.kwargs['unsubscription_token'])
89
        try:
90
            return models.Subscription.objects.get(category__pk=data['category'],
91
                                identifier=data['identifier'])
92
        except models.Subscription.DoesNotExist:
93
            raise Http404
94

  
95
    def get_success_url(self):
96
        return reverse('unsubscription_done')
97

  
98
unsubscribe = UnsubscribeView.as_view()
99

  
100

  
101
class UnsubscriptionDoneView(TemplateView):
102
    template_name='corbo/unsubscription_done.html'
103

  
104
unsubscription_done = UnsubscriptionDoneView.as_view()
105

  
106

  
82 107
class ManageView(ListView):
83 108
    paginate_by = settings.ANNOUNCES_PER_PAGE
84 109
    template_name = 'corbo/manage.html'
85
-