]> git.0d.be Git - django-panik-newsletter.git/commitdiff
Enregistrement à mailman.
authorjean-philippe <user@server.tld>
Sun, 14 Jul 2013 23:52:34 +0000 (01:52 +0200)
committerjean-philippe <user@server.tld>
Sun, 14 Jul 2013 23:52:34 +0000 (01:52 +0200)
models.py
templates/confirmation_email.txt [new file with mode: 0644]
templates/registration.html [new file with mode: 0644]
urls.py
views.py

index da6d91dd46c240797b34002ba3c6af2853172cf1..ba7972cc2c1d7ddc410089626a88b14270fe0043 100644 (file)
--- 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 (file)
index 0000000..549d4a0
--- /dev/null
@@ -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 (file)
index 0000000..de95417
--- /dev/null
@@ -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 0f5db4e451ce6f31fe82dea0a575d11380b50c03..80543e8c7c2d2ece81277d3b177891fc98eee83e 100644 (file)
--- 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<validation_value>[0-9a-f]{40})$', registration), 
 )
 
index c1b575104cc247c895d8b61d6eb6f0e1aab7aa4c..dd35a88a28ae6eee9a60f6f0caa3a61875d8577c 100644 (file)
--- 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})
+        
+    
+
+