From fe6deb965eec77704241c83a2b1025c69492d9cf Mon Sep 17 00:00:00 2001 From: jean-philippe Date: Mon, 15 Jul 2013 01:52:34 +0200 Subject: [PATCH] =?utf8?q?Enregistrement=20=C3=A0=20mailman.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- models.py | 45 ++++++++++++++++++++---------- templates/confirmation_email.txt | 12 ++++++++ templates/registration.html | 13 +++++++++ urls.py | 3 +- views.py | 47 ++++++++++++++++++++++++++++---- 5 files changed, 98 insertions(+), 22 deletions(-) create mode 100644 templates/confirmation_email.txt create mode 100644 templates/registration.html diff --git a/models.py b/models.py index da6d91d..ba7972c 100644 --- a/models.py +++ b/models.py @@ -1,7 +1,13 @@ # -*- coding: utf8 -*- from django.db import models + +from django.template import loader, Context +from django.conf import settings + +from django.utils.translation import ugettext as _ from django.core.mail import send_mail +# rajout d'un commentaire inutile class Subscriber(models.Model) : email = models.EmailField(unique = True) # TODO : informer si déjà inscrit ? Que faire dans ce cas. @@ -16,19 +22,28 @@ class Subscriber(models.Model) : def save(self, *args, **kwargs): super(Subscriber, self).save(*args, **kwargs) - self.send_confirmation_email() - - def send_confirmation_email(self): - if(self.is_validated==None): - subject = 'confirmation de votre inscription' - message = 'blabla, veuillez cliquer sur le lien suivant http://127.0.0.1:8000/newsletter%s' % self.password - sender = 'no-reply@panik.org' - # Susceptible de lever une socket.error ou une SMTPException - send_mail( - subject, - message, - sender, - [self.email] - ) - Subscriber.objects.filter(email=self.email).update(is_validated=False) + 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.save() diff --git a/templates/confirmation_email.txt b/templates/confirmation_email.txt new file mode 100644 index 0000000..549d4a0 --- /dev/null +++ b/templates/confirmation_email.txt @@ -0,0 +1,12 @@ +You have been registered at the newsletter of {{ organization }}. + +Thanks to click on the following link to confirm your registration : {{ confirmation_link }} . + + +Regards, + +{{ organization }} +-- +{{ organization_url }} + + diff --git a/templates/registration.html b/templates/registration.html new file mode 100644 index 0000000..de95417 --- /dev/null +++ b/templates/registration.html @@ -0,0 +1,13 @@ +{% block newsletter_confirmation %} + + {% if user_exist %} + {% if error_message %} + {{ error_message }} + {% else %} + {{ registration_message }} + {% endif %} + {% else %} + {{ error_message }} + {% endif %} + +{% endblock %} diff --git a/urls.py b/urls.py index 0f5db4e..80543e8 100644 --- a/urls.py +++ b/urls.py @@ -1,8 +1,9 @@ from django.conf.urls import * -from .views import subscription +from .views import subscription, registration urlpatterns = patterns('', url(r'^$', subscription), + url(r'^(?P[0-9a-f]{40})$', registration), ) diff --git a/views.py b/views.py index c1b5751..dd35a88 100644 --- a/views.py +++ b/views.py @@ -3,22 +3,24 @@ import hashlib import random import socket +import urllib from smtplib import SMTPException from django.db import models, IntegrityError from django.shortcuts import render -from django.core.mail import send_mail +from django.conf import settings +from django.utils.translation import ugettext as _ from .forms import SubscriptionForm from .models import Subscriber def subscription(request) : - INTEGRITY_ERROR = u"Vous êtes déjà inscrit à notre newsletter." - SOCKET_ERROR = u"Connexion impossible pour l'instant." - SMTP_ERROR = u"Échec de l'envoi du message" - REVALIDATION_ERROR = u"Vous avez déjà reçu un mail de confirmation pour votre inscription." + 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) @@ -31,7 +33,7 @@ def subscription(request) : subscriber = Subscriber(email=cd['email'], password=passwd, is_validated=None, is_registered=False) is_sent=False try : - subscriber.save() + subscriber.save(request) is_sent=True except IntegrityError : custom_errors.append(INTEGRITY_ERROR) @@ -50,3 +52,36 @@ def subscription(request) : return render(request, "subscription_form.html", {'form' : form}) + + +def registration(request, validation_value) : + error_message = '' + registration_message = '' + try : + subscriber = Subscriber.objects.get(password = validation_value) + subscriber.is_validated = True + + if subscriber.is_registered is False : + params = urllib.urlencode({'email' : subscriber.email, 'fullname' : '', 'pw' : subscriber.password, 'pw-conf' : subscriber.password, 'digest' : '0'}) + response = urllib.urlopen(settings.NEWSLETTER_SUBSCRIPTION_URL, params) + if response.getcode() == 200 : + subscriber.is_registered = True + registration_message = _("You subscribed to our newsletter.") + else : + subscriber.is_registered = None # TODO : rajouter une méthode niveau table pour gérer les enregistrements qui échouent (pareil pour la souscription proprement dite). + elif subscriber.is_registered : + error_message = _('You already subscribed to our newsletter.') + else : + error_message = _('Your subscription is pending.') + + subscriber.save() + + return render(request, 'registration.html', {'user_exist' : True, 'error_message' : error_message, 'registration_message' : registration_message }) + + except Subscriber.DoesNotExist : + error_message = _('Forbidden page.') + return render(request, 'registration.html', {'user_exist' : False, 'error_message' : error_message}) + + + + -- 2.39.2