views.py 1.31 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# -*- 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})