summaryrefslogtreecommitdiff
path: root/app/lttr/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'app/lttr/views.py')
-rw-r--r--app/lttr/views.py108
1 files changed, 108 insertions, 0 deletions
diff --git a/app/lttr/views.py b/app/lttr/views.py
new file mode 100644
index 0000000..d17a609
--- /dev/null
+++ b/app/lttr/views.py
@@ -0,0 +1,108 @@
+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 Subscriber, Newsletter
+from .forms import SubscribeRequestForm, UpdateForm
+
+ACTIONS = ('subscribe', 'unsubscribe', 'update')
+
+
+class NewsletterSubscribedView(TemplateView):
+ template_name = "lttr/subscribed.html"
+
+
+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'
+ slug = None
+
+
+ def get_template_names(self):
+ return ["lttr/%s_subscribe.html" % self.slug, 'lttr/subscribe.html']
+
+ def get_form_kwargs(self):
+ kwargs = super(NewsletterSubscribeView, self).get_form_kwargs()
+ try:
+ self.slug = self.kwargs['slug']
+ except:
+ pass
+ nl = Newsletter.objects.get(slug=self.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
+
+