diff options
author | luxagraf <sng@luxagraf.net> | 2019-02-12 14:31:53 -0600 |
---|---|---|
committer | luxagraf <sng@luxagraf.net> | 2019-02-12 14:31:53 -0600 |
commit | 0ccf2f7b61ea9889b1c03e51dfc847ca16cd98e7 (patch) | |
tree | 3c379d541c5aa96870a54796718078668fc8263f /app/lttr/views.py | |
parent | ee2803d2f85888ce42fd6371ef606f4a9c6fd88b (diff) |
finished up subscribe functions for lttr
Diffstat (limited to 'app/lttr/views.py')
-rw-r--r-- | app/lttr/views.py | 50 |
1 files changed, 44 insertions, 6 deletions
diff --git a/app/lttr/views.py b/app/lttr/views.py index 786bc5c..0d3dbea 100644 --- a/app/lttr/views.py +++ b/app/lttr/views.py @@ -1,14 +1,22 @@ -from django.views.generic import ListView, CreateView +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 +from smtplib import SMTPException from .models import NewsletterMailing, Subscriber, Newsletter -from .forms import SubscribeRequestForm +from .forms import SubscribeRequestForm, UpdateForm + +ACTIONS = ('subscribe', 'unsubscribe', 'update') class NewsletterMailingDetail(DetailView): @@ -29,12 +37,17 @@ class NewsletterMailingDetail(DetailView): return context +class NewsletterSubscribedView(TemplateView): + template_name = "lttr/subscribed.html" + + class NewsletterListView(CreateView): - model = Subscriber - form_class = SubscribeRequestForm """ Return a subscribe form and list of Newsletter posts in reverse chronological order """ + model = Subscriber + form_class = SubscribeRequestForm + action = 'subscribe' def get_form_kwargs(self): kwargs = super(NewsletterListView, self).get_form_kwargs() @@ -46,9 +59,34 @@ class NewsletterListView(CreateView): context['mailings'] = NewsletterMailing.objects.filter(status=1) return context + def get_success_url(self): + return reverse_lazy('lttr:subscribed') + def form_valid(self, form, **kwargs): - form.instance.user = settings.AUTH_USER_MODEL.objects.get_or_create(email=form.instance.email) + 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(NewsletterListView, self).form_invalid(form) return super(NewsletterListView, self).form_valid(form) - #super(NewsletterListView, self).form_valid() + +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 |