Commit 08d1e437 authored by fred's avatar fred

rewrote subscribe form

parent 091853ae
# *-* coding: utf8 -*-
from django import forms
from .models import Subscriber
class SubscriptionForm(forms.Form):
email = forms.EmailField()
class SubscribeForm(forms.ModelForm):
class Meta:
model = Subscriber
exclude = ('inscription_date', 'is_validated', 'is_registered', 'password')
# -*- coding: utf8 -*-
import hashlib
import random
from django.db import models
from django.template import loader, Context
......@@ -19,31 +22,17 @@ class Subscriber(models.Model) :
def __unicode__(self) :
return self.email
def save(self, *args, **kwargs):
super(Subscriber, self).save(*args, **kwargs)
if self.is_validated is None:
self.send_confirmation_email(args[0])
def send_confirmation_email(self, request):
subject = _("%s's newsletter registration." % settings.ORGANIZATION)
confirmation_link = ("%s/newsletter/%s" % (request.get_host(), self.password))
sender = ("noreplay@%s" % request.get_host().strip("www."))
organization = settings.ORGANIZATION
organization_url = request.get_host()
message = loader.get_template("confirmation_email.txt")
message_context = Context({
'organization' : organization,
'organization_url' : organization_url,
'confirmation_link' : confirmation_link,
})
# Susceptible de lever une socket.error ou une SMTPException
send_mail(
subject,
message.render(message_context),
sender,
[self.email]
)
self.is_validated=False
self.send_confirmation_email()
def send_confirmation_email(self):
self.password = hashlib.sha1(str(random.random())).hexdigest()
confirm_subject = loader.get_template('newsletter/confirmation_email_subject.txt')
confirm_body = loader.get_template('newsletter/confirmation_email_body.txt')
context = Context({})
send_mail(confirm_subject.render(context).strip(), confirm_body.render(context),
settings.NEWSLETTER_SENDER, [self.email])
self.is_validated = False
self.save()
{% extends "base.html" %}
{% block newsletter_confirmation %}
{% if user_exist %}
......
{% block newsletter_form %}
{% if is_sent %}
<p>
Vous devriez recevoir sous peu un mail de confirmation dans votre boîte mail.
</p>
{% endif %}
{% if custom_errors %}
<p>
{% for error in custom_errors %}
{{ error }} </br>
{% endfor %}
</p>
{% endif %}
<form action="" method="post" >
<ul>
{{ form.as_ul }}
</ ul>
{% csrf_token %}
<input type="submit" value="Soumettre">
</form>
{% endblock %}
from django.conf.urls import *
from django.conf import settings
from .views import subscription, unsubscription, registration
from .views import unsubscription, registration
urlpatterns = patterns('',
url(r'^$', subscription),
url(r'^$', 'newsletter.views.subscribe', name='newsletter-subscribe'),
url(r'^thanks/$', 'newsletter.views.thanks', name='newsletter-thanks'),
url(r'^unsubscription/$', unsubscription),
url(r'^(?P<validation_value>[0-9a-f]{40})$', registration),
)
......@@ -9,51 +9,27 @@ import cookielib
from smtplib import SMTPException
from django.db import models, IntegrityError
from django.conf import settings
from django.shortcuts import render
from django.db import models, IntegrityError
from django.utils.translation import ugettext as _
from django.views.generic.base import TemplateView
from django.views.generic.edit import CreateView
from .forms import SubscriptionForm
from .models import Subscriber
from .forms import SubscribeForm
from .models import Subscriber
def subscription(request) :
INTEGRITY_ERROR = _("You're already registered at our newsletter.")
SOCKET_ERROR = _("Connexion error. Try later.")
SMTP_ERROR = _("Error to sending email.")
REVALIDATION_ERROR = _("You've already receipt a confirmation email.")
if request.method == 'POST' :
form = SubscriptionForm(request.POST)
custom_errors = []
if form.is_valid() :
cd = form.cleaned_data
# Va aussi servir pour le lien de validation.
passwd = hashlib.sha1(str(random.random())).hexdigest()
subscriber = Subscriber(email=cd['email'], password=passwd, is_validated=None, is_registered=False)
is_sent=False
try :
subscriber.save(request)
is_sent=True
except IntegrityError :
custom_errors.append(INTEGRITY_ERROR)
if(Subscriber.objects.get(email=subscriber.email).is_validated != False) :
custom_errors.append(REVALIDATION_ERROR)
except socket.error :
custom_errors.append(SOCKET_ERROR)
except SMTPException :
custom_errors.append(SMTP_ERROR)
return render(request, 'subscription_form.html', {'form' : form, 'custom_errors' : custom_errors, 'is_sent' : is_sent})
else :
return render(request, 'subscription_form.html', {'form' : form})
else :
form = SubscriptionForm()
return render(request, "subscription_form.html", {'form' : form})
class SubscribeView(CreateView):
form_class = SubscribeForm
model = Subscriber
success_url = 'thanks'
subscribe = SubscribeView.as_view()
class ThanksView(TemplateView):
template_name = 'newsletter/thanks.html'
thanks = ThanksView.as_view()
def unsubscription(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