Commit 36b4e0f0 authored by jean-philippe's avatar jean-philippe

Désinscription.

parent f71aa1ec
{% block unsubscription_form %}
{% if message %}
<p> {{ message }} </p>
{% endif %}
{% if form %}
<form action="" method="post" >
<ul>
{{ form.as_ul }}
</ ul>
{% csrf_token %}
<input type="submit" value="Soumettre">
</form>
{% endif %}
{% endblock %}
from django.conf.urls import *
from .views import subscription, registration
from django.conf import settings
from .views import subscription, unsubscription, registration
urlpatterns = patterns('',
url(r'^$', subscription),
url(r'^unsubscription/$', unsubscription),
url(r'^(?P<validation_value>[0-9a-f]{40})$', registration),
)
......@@ -4,12 +4,15 @@ import hashlib
import random
import socket
import urllib
import urllib2
import cookielib
from smtplib import SMTPException
from django.db import models, IntegrityError
from django.shortcuts import render
from django.conf import settings
from django.shortcuts import render
from django.utils.translation import ugettext as _
from .forms import SubscriptionForm
......@@ -53,6 +56,44 @@ def subscription(request) :
def unsubscription(request) :
UNSUBSCRIPTION_MESSAGE = _("Enter your email for unsunscribe to the newsletter.")
UNSUBSCRIPTION_CONFIRMATION_MESSAGE = _("You're now unsubscribed")
if request.method == 'GET' :
form = SubscriptionForm()
return render(request, 'unsubscription_form.html', {'form' : form, 'message' : UNSUBSCRIPTION_MESSAGE})
elif request.method == 'POST' :
form = SubscriptionForm(request.POST)
if form.is_valid() :
cd = form.cleaned_data
try :
subscriber = Subscriber.objects.get(email = cd['email'])
cookie_jar = cookielib.CookieJar()
url_opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar))
login_params = urllib.urlencode({'email' : subscriber.email, 'password' : subscriber.password})
login_request = urllib2.Request(settings.NEWSLETTER_UNSUBSCRIPTION_URL, login_params)
url = url_opener.open(login_request)
unsubscribe_action_url = ("%s/%s" % (settings.NEWSLETTER_UNSUBSCRIPTION_URL, subscriber.email))
unsubscribe_params = urllib.urlencode({'unsub' : 'Résilier', 'unsubconfirm' : '1'})
unsubscribe_request = urllib2.Request(unsubscribe_action_url, unsubscribe_params)
url = url_opener.open(unsubscribe_request)
url.close()
subscriber.delete()
return render(request, 'unsubscription_form.html', {'message' : UNSUBSCRIPTION_CONFIRMATION_MESSAGE})
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 = ''
......
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