1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
import socket
from django.views.generic import ListView, CreateView, TemplateView, FormView
from django.views.generic.detail import DetailView
from django.views.generic.dates import YearArchiveView, MonthArchiveView
from django.contrib.syndication.views import Feed
from django.template.response import TemplateResponse
from django.contrib.auth import get_user_model
from django.db.models import Q
from django.shortcuts import get_object_or_404, redirect
from django.conf import settings
from django.urls import reverse, reverse_lazy
from utils.views import PaginatedListView, LuxDetailView
from smtplib import SMTPException
from .models import NewsletterMailing, Subscriber, Newsletter
from .forms import SubscribeRequestForm, UpdateForm
ACTIONS = ('subscribe', 'unsubscribe', 'update')
class NewsletterMailingDetail(LuxDetailView):
model = NewsletterMailing
slug_field = "slug"
slug_url_kwarg = 'mailing'
def get_queryset(self):
queryset = super(NewsletterMailingDetail, self).get_queryset()
return queryset.filter(issue=self.kwargs['issue'])
def get_template_names(self):
obj = self.get_object()
return ["lttr/%s_detail.html" % obj.newsletter.slug, 'post_detail.html']
class NewsletterSubscribedView(TemplateView):
template_name = "lttr/subscribed.html"
class NewsletterListView(ListView):
"""
Return a list of Newsletter posts in reverse chronological order
"""
model = NewsletterMailing
def get_template_names(self):
return ["lttr/%s_list.html" % self.kwargs['slug'], 'newsletter_list.html']
def get_queryset(self):
queryset = super(NewsletterListView, self).get_queryset()
return queryset.filter(newsletter__slug=self.kwargs['slug']).order_by('-pub_date')
def get_context_data(self, **kwargs):
context = super(NewsletterListView, self).get_context_data(**kwargs)
context['breadcrumbs'] = [self.kwargs['slug'],]
return context
class NewsletterOptionsView(ListView):
model = Newsletter
class ConfirmSubscriptionView(DetailView):
model = Subscriber
template_name = "lttr/confirm_activate.html"
def get_object(self):
obj = Subscriber.objects.get(newsletter__slug=self.kwargs['slug'], activation_code=self.kwargs['activation_code'])
if obj.subscribed is False:
obj.update('subscribe')
return obj
def get_context_data(self, **kwargs):
context = super(ConfirmSubscriptionView, self).get_context_data(**kwargs)
context['newsletter'] = self.kwargs['slug']
return context
class NewsletterSubscribeView(CreateView):
"""
Return a subscribe form for iframe embedding
"""
model = Subscriber
form_class = SubscribeRequestForm
action = 'subscribe'
def get_template_names(self):
return ["lttr/%s_subscribe.html" % self.kwargs['slug'], 'lttr/subscribe.html']
def get_form_kwargs(self):
kwargs = super(NewsletterSubscribeView, self).get_form_kwargs()
nl = Newsletter.objects.get(slug=self.kwargs['slug'])
kwargs['newsletter'] = nl
return kwargs
def get_success_url(self):
return reverse_lazy('lttr:subscribed')
def form_valid(self, form, **kwargs):
form.instance.user, created = get_user_model().objects.get_or_create(
email=form.cleaned_data['email_field'],
username=form.cleaned_data['email_field']
)
self.object = form.save()
try:
self.object.send_activation_email(action=self.action)
except (SMTPException, socket.error) as e:
print(e)
self.error = True
# Although form was valid there was error while sending email,
# so stay at the same url.
return super(NewsletterSubscribeView, self).form_invalid(form)
return super(NewsletterSubscribeView, self).form_valid(form)
class UnsubscribeRequestView(DetailView):
model = Subscriber
template_name = "lttr/unsubscribe.html"
def get_object(self):
obj = Subscriber.objects.get(newsletter__slug=self.kwargs['slug'],activation_code=self.kwargs['activation_code'])
if obj.subscribed is True:
obj.update('unsubscribe')
return obj
def get_context_data(self, **kwargs):
context = super(UnsubscribeRequestView, self).get_context_data(**kwargs)
context['subscriber'] = self.get_object()
context['newsletter'] = self.kwargs['slug']
return context
class NewsletterRSSFeedView(Feed):
title = "Luxagraf: Range, A Weekly Photo"
link = "/newsletter/range/"
description = "Latest Range Newsletter"
description_template = 'feeds/blog_description.html'
def items(self):
return NewsletterMailing.objects.filter(newsletter__slug='ranger',status__exact=1)
def item_pubdate(self, item):
"""
Takes an item, as returned by items(), and returns the item's
pubdate.
"""
|