Commit 88097e56 authored by jean-philippe's avatar jean-philippe

modification de la gestion de la souscritpion

parents
# -*- coding: utf8 -*-
from django.contrib import admin
from .models import Subscriber
admin.site.register(Subscriber)
# *-* coding: utf8 -*-
from django import forms
class SubscriptionForm(forms.Form):
email = forms.EmailField()
# -*- coding: utf8 -*-
from smtplib import SMTPException
import socket
from django.db import models
from django.core.mail import send_mail
class Subscriber(models.Model) :
email = models.EmailField(unique = True) # TODO : informer si déjà inscrit ? Que faire dans ce cas.
inscription_date = models.DateField(auto_now_add=True)
is_validated = models.NullBooleanField() # Au click sur le lien de confirmation. Null si erreur à l'envoi au souscripteur.
is_registered = models.NullBooleanField() # À l'inscription après la confirmation Null si erreur à l'envoi à mailman.
password = models.CharField(max_length=100) # sh1
def __unicode__(self) :
return self.email
def save(self, *args, **kwargs):
super(Subscriber, self).save(*args, **kwargs)
self.send_confirmation_email()
def send_confirmation_email(self):
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'
try :
send_mail(
subject,
message,
sender,
[self.email]
)
except socket.error as error :
raise error
self.is_validated=False
Subscriber.objects.filter(email=self.email).update(is_validated=False)
{% block newsletter_form %}
{% if is_sent %}
<p>
Vous devriez recevoir sous peu un mail de confirmation dans votre boîte mail.
</p>
{% endif %}
{% if custom_errors %}
<p>
{% for error in custom_errors %}
{{ error }} </br>
{% endfor %}
</p>
{% endif %}
<form action="" method="post" >
<table>
{{ form.as_table }}
</ table>
{% csrf_token %}
<input type="submit" value="Soumettre">
</form>
{% endblock %}
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
Replace this with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.assertEqual(1 + 1, 2)
from django.conf.urls import *
from .views import subscription
urlpatterns = patterns('',
url(r'^$', subscription),
)
# -*- coding: utf8 -*-
import hashlib
import random
import socket
from django.db import models, IntegrityError
from django.shortcuts import render
from django.core.mail import send_mail
from .forms import SubscriptionForm
from .models import Subscriber
# Create your views here.
def subscription(request) :
if request.method == 'POST' :
form = SubscriptionForm(request.POST)
custom_errors = []
if form.is_valid() :
cd = form.cleaned_data
# Va aussi servir pour le lien de validation.
passwd = hashlib.sha1(str(random.random())).hexdigest()
subscriber = Subscriber(email=cd['email'], password=passwd, is_validated=None, is_registered=False)
try :
subscriber.save()
except IntegrityError, validation_error :
custom_errors.append(str(validation_error))
except socket.error:
custom_errors.append("Connexion impossible pour l'instant")
return render(request, 'subscription_form.html', {'form' : form, 'custom_errors' : custom_errors})
else :
return render(request, 'subscription_form.html', {'form' : form})
else :
form = SubscriptionForm()
return render(request, "subscription_form.html", {'form' : form})
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