Commit 08d1e437 authored by fred's avatar fred

rewrote subscribe form

parent 091853ae
# *-* coding: utf8 -*-
from django import forms from django import forms
from .models import Subscriber
class SubscriptionForm(forms.Form): class SubscribeForm(forms.ModelForm):
email = forms.EmailField() class Meta:
model = Subscriber
exclude = ('inscription_date', 'is_validated', 'is_registered', 'password')
# -*- coding: utf8 -*- # -*- coding: utf8 -*-
import hashlib
import random
from django.db import models from django.db import models
from django.template import loader, Context from django.template import loader, Context
...@@ -19,31 +22,17 @@ class Subscriber(models.Model) : ...@@ -19,31 +22,17 @@ class Subscriber(models.Model) :
def __unicode__(self) : def __unicode__(self) :
return self.email return self.email
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
super(Subscriber, self).save(*args, **kwargs) super(Subscriber, self).save(*args, **kwargs)
if self.is_validated is None: if self.is_validated is None:
self.send_confirmation_email(args[0]) self.send_confirmation_email()
def send_confirmation_email(self, request): def send_confirmation_email(self):
subject = _("%s's newsletter registration." % settings.ORGANIZATION) self.password = hashlib.sha1(str(random.random())).hexdigest()
confirmation_link = ("%s/newsletter/%s" % (request.get_host(), self.password)) confirm_subject = loader.get_template('newsletter/confirmation_email_subject.txt')
sender = ("noreplay@%s" % request.get_host().strip("www.")) confirm_body = loader.get_template('newsletter/confirmation_email_body.txt')
organization = settings.ORGANIZATION context = Context({})
organization_url = request.get_host() send_mail(confirm_subject.render(context).strip(), confirm_body.render(context),
message = loader.get_template("confirmation_email.txt") settings.NEWSLETTER_SENDER, [self.email])
message_context = Context({ self.is_validated = False
'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.save() self.save()
{% extends "base.html" %}
{% block newsletter_confirmation %} {% block newsletter_confirmation %}
{% if user_exist %} {% 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.urls import *
from django.conf import settings from django.conf import settings
from .views import subscription, unsubscription, registration from .views import unsubscription, registration
urlpatterns = patterns('', 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'^unsubscription/$', unsubscription),
url(r'^(?P<validation_value>[0-9a-f]{40})$', registration), url(r'^(?P<validation_value>[0-9a-f]{40})$', registration),
) )
...@@ -9,51 +9,27 @@ import cookielib ...@@ -9,51 +9,27 @@ import cookielib
from smtplib import SMTPException from smtplib import SMTPException
from django.db import models, IntegrityError
from django.conf import settings from django.conf import settings
from django.db import models, IntegrityError
from django.shortcuts import render
from django.utils.translation import ugettext as _ 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 .forms import SubscribeForm
from .models import Subscriber from .models import Subscriber
def subscription(request) : class SubscribeView(CreateView):
INTEGRITY_ERROR = _("You're already registered at our newsletter.") form_class = SubscribeForm
SOCKET_ERROR = _("Connexion error. Try later.") model = Subscriber
SMTP_ERROR = _("Error to sending email.")
REVALIDATION_ERROR = _("You've already receipt a confirmation email.") success_url = 'thanks'
subscribe = SubscribeView.as_view()
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 ThanksView(TemplateView):
template_name = 'newsletter/thanks.html'
thanks = ThanksView.as_view()
def unsubscription(request) : 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