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

Enregistrement à mailman.

parent 1350dc37
# -*- 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()
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 .views import subscription
from .views import subscription, registration
urlpatterns = patterns('',
url(r'^$', subscription),
url(r'^(?P<validation_value>[0-9a-f]{40})$', registration),
)
......@@ -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})
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