Commit 72ffb1d9 authored by fred's avatar fred

rewrote the confirmation step

parent 08d1e437
......@@ -2,6 +2,7 @@
import hashlib
import random
import urllib
from django.db import models
......@@ -31,8 +32,21 @@ class Subscriber(models.Model) :
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({})
context = Context({'token': self.password})
send_mail(confirm_subject.render(context).strip(), confirm_body.render(context),
settings.NEWSLETTER_SENDER, [self.email])
self.is_validated = False
self.save()
def subscribe_in_mailman(self):
params = urllib.urlencode(
{'email' : self.email, 'fullname' : '',
'pw' : self.password, 'pw-conf' : self.password,
'digest' : '0'})
response = urllib.urlopen(settings.NEWSLETTER_SUBSCRIBE_URL, params)
response.read()
response.close()
# TODO: error handling
# TODO: connecting to the moderation URL to validate
#self.registered = True
#self.save()
from django.conf.urls import *
from django.conf import settings
from .views import unsubscription, registration
from .views import unsubscription
urlpatterns = patterns('',
url(r'^$', 'newsletter.views.subscribe', name='newsletter-subscribe'),
url(r'^thanks/$', 'newsletter.views.thanks', name='newsletter-thanks'),
url(r'^(?P<token>[0-9a-f]{40})$', 'newsletter.views.confirmation', name='newsletter-confirmation'),
url(r'^unsubscription/$', unsubscription),
url(r'^(?P<validation_value>[0-9a-f]{40})$', registration),
)
......@@ -11,6 +11,7 @@ from smtplib import SMTPException
from django.conf import settings
from django.db import models, IntegrityError
from django.shortcuts import get_object_or_404
from django.utils.translation import ugettext as _
from django.views.generic.base import TemplateView
from django.views.generic.edit import CreateView
......@@ -32,6 +33,19 @@ class ThanksView(TemplateView):
thanks = ThanksView.as_view()
class ConfirmationView(TemplateView):
template_name = 'newsletter/done.html'
def get_context_data(self, token, **kwargs):
context = super(ConfirmationView, self).get_context_data(**kwargs)
subscriber = get_object_or_404(Subscriber, password=token, is_registered=None)
subscriber.is_validated = True
subscriber.save()
subscriber.subscribe_in_mailman()
return context
confirmation = ConfirmationView.as_view()
def unsubscription(request) :
UNSUBSCRIPTION_MESSAGE = _("Enter your email for unsunscribe to the newsletter.")
UNSUBSCRIPTION_CONFIRMATION_MESSAGE = _("You're now unsubscribed")
......@@ -65,41 +79,3 @@ def unsubscription(request) :
except Subscriber.DoesNotExist :
error_message = _("The entered email address does not exist. Please try another one.")
return render(request, 'unsubscription_form.html', {'form' : form, 'message' : error_message})
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).
response.close()
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