Commit fe6deb96 authored by jean-philippe's avatar jean-philippe

Enregistrement à mailman.

parent 1350dc37
# -*- coding: utf8 -*- # -*- coding: utf8 -*-
from django.db import models 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 from django.core.mail import send_mail
# rajout d'un commentaire inutile
class Subscriber(models.Model) : class Subscriber(models.Model) :
email = models.EmailField(unique = True) # TODO : informer si déjà inscrit ? Que faire dans ce cas. email = models.EmailField(unique = True) # TODO : informer si déjà inscrit ? Que faire dans ce cas.
...@@ -16,19 +22,28 @@ class Subscriber(models.Model) : ...@@ -16,19 +22,28 @@ class Subscriber(models.Model) :
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
super(Subscriber, self).save(*args, **kwargs) super(Subscriber, self).save(*args, **kwargs)
self.send_confirmation_email() if self.is_validated is None:
self.send_confirmation_email(args[0])
def send_confirmation_email(self):
if(self.is_validated==None): def send_confirmation_email(self, request):
subject = 'confirmation de votre inscription' subject = _("%s's newsletter registration." % settings.ORGANIZATION)
message = 'blabla, veuillez cliquer sur le lien suivant http://127.0.0.1:8000/newsletter%s' % self.password confirmation_link = ("%s/newsletter/%s" % (request.get_host(), self.password))
sender = 'no-reply@panik.org' sender = ("noreplay@%s" % request.get_host().strip("www."))
# Susceptible de lever une socket.error ou une SMTPException organization = settings.ORGANIZATION
send_mail( organization_url = request.get_host()
subject, message = loader.get_template("confirmation_email.txt")
message, message_context = Context({
sender, 'organization' : organization,
[self.email] 'organization_url' : organization_url,
) 'confirmation_link' : confirmation_link,
Subscriber.objects.filter(email=self.email).update(is_validated=False) })
# 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()
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 }}
{% block newsletter_confirmation %}
{% if user_exist %}
{% if error_message %}
{{ error_message }}
{% else %}
{{ registration_message }}
{% endif %}
{% else %}
{{ error_message }}
{% endif %}
{% endblock %}
from django.conf.urls import * from django.conf.urls import *
from .views import subscription from .views import subscription, registration
urlpatterns = patterns('', urlpatterns = patterns('',
url(r'^$', subscription), url(r'^$', subscription),
url(r'^(?P<validation_value>[0-9a-f]{40})$', registration),
) )
...@@ -3,22 +3,24 @@ ...@@ -3,22 +3,24 @@
import hashlib import hashlib
import random import random
import socket import socket
import urllib
from smtplib import SMTPException from smtplib import SMTPException
from django.db import models, IntegrityError from django.db import models, IntegrityError
from django.shortcuts import render 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 .forms import SubscriptionForm
from .models import Subscriber from .models import Subscriber
def subscription(request) : def subscription(request) :
INTEGRITY_ERROR = u"Vous êtes déjà inscrit à notre newsletter." INTEGRITY_ERROR = _("You're already registered at our newsletter.")
SOCKET_ERROR = u"Connexion impossible pour l'instant." SOCKET_ERROR = _("Connexion error. Try later.")
SMTP_ERROR = u"Échec de l'envoi du message" SMTP_ERROR = _("Error to sending email.")
REVALIDATION_ERROR = u"Vous avez déjà reçu un mail de confirmation pour votre inscription." REVALIDATION_ERROR = _("You've already receipt a confirmation email.")
if request.method == 'POST' : if request.method == 'POST' :
form = SubscriptionForm(request.POST) form = SubscriptionForm(request.POST)
...@@ -31,7 +33,7 @@ def subscription(request) : ...@@ -31,7 +33,7 @@ def subscription(request) :
subscriber = Subscriber(email=cd['email'], password=passwd, is_validated=None, is_registered=False) subscriber = Subscriber(email=cd['email'], password=passwd, is_validated=None, is_registered=False)
is_sent=False is_sent=False
try : try :
subscriber.save() subscriber.save(request)
is_sent=True is_sent=True
except IntegrityError : except IntegrityError :
custom_errors.append(INTEGRITY_ERROR) custom_errors.append(INTEGRITY_ERROR)
...@@ -50,3 +52,36 @@ def subscription(request) : ...@@ -50,3 +52,36 @@ def subscription(request) :
return render(request, "subscription_form.html", {'form' : form}) 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})
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