Commit 5346dbc83eec95c174f6e2ab625ad4c8c694c851

Authored by Sergio Oliveira
1 parent 0a531154

Adding email verification

src/accounts/templates/accounts/user_update_form.html
... ... @@ -43,6 +43,29 @@ $(function() {
43 43 event.preventDefault();
44 44 });
45 45  
  46 + $('.verify-email').on('click', function(event) {
  47 + var $email_block = $(event.target).parent().parent();
  48 + $.ajax({
  49 + url: "{% url 'archive_email_validation_view' %}",
  50 + type: 'post',
  51 + data: { email: $('.email-address', $email_block).text() },
  52 + context: $email_block[0],
  53 + beforeSend: function(xhr, settings) {
  54 + xhr.setRequestHeader("X-CSRFToken", $.cookie('csrftoken'));
  55 + }
  56 + }).done(function() {
  57 + var email = $('.email-address', $(this)).text();
  58 + var msg = '{% trans "We sent a verification email to " %}' + email + '. ' +
  59 + '{% trans "Please follow the instructions in it." %}';
  60 + $('#alert-message').text(msg);
  61 + $('#alert-js').removeClass('alert-warning').addClass('alert-success');
  62 + $('#alert-js').show();
  63 + window.scroll(0, 0);
  64 + });
  65 +
  66 + event.preventDefault();
  67 + });
  68 +
46 69 $('.set-primary').on('click', function(event) {
47 70 var $email_block = $(event.target).parent().parent();
48 71 $.ajax({
... ...
src/super_archives/urls.py
1 1 from django.conf.urls import patterns, include, url
2 2  
3   -from .views import EmailView
  3 +from .views import EmailView, EmailValidationView
4 4  
5 5  
6 6 urlpatterns = patterns('super_archives.views',
... ... @@ -8,6 +8,8 @@ urlpatterns = patterns('super_archives.views',
8 8 url(r'thread/(?P<mailinglist>[-\w]+)/(?P<thread_token>[-\w]+)$', 'thread',
9 9 name="thread_view"),
10 10 url(r'thread/$', 'list_messages', name='thread_list'),
  11 + url(r'manage/email/validate/?$', EmailValidationView.as_view(),
  12 + name="archive_email_validation_view"),
11 13 url(r'manage/email/(?P<key>[0-9a-z]{32})?', EmailView.as_view(),
12 14 name="archive_email_view"),
13 15 )
... ...
src/super_archives/views.py
1 1 # -*- coding: utf-8 -*-
2 2  
  3 +import smtplib
  4 +
3 5 from django import http
4 6 from django.contrib import messages
5 7 from django.db import IntegrityError
... ... @@ -12,6 +14,7 @@ from django.contrib.auth.decorators import login_required
12 14 from django.shortcuts import render, redirect
13 15  
14 16 from . import queries
  17 +from .utils import send_verification_email
15 18 from .decorators import count_hit
16 19 from .models import MailingList, Thread, EmailAddress, EmailAddressValidation
17 20  
... ... @@ -95,7 +98,7 @@ class EmailView(View):
95 98  
96 99 try:
97 100 email_val = EmailAddressValidation.objects.get(validation_key=key,
98   - user=request.user)
  101 + user__pk=request.user.pk)
99 102 except EmailAddressValidation.DoesNotExist:
100 103 messages.error(request, _('The email address you are trying to '
101 104 'verify either has already been verified '
... ... @@ -189,3 +192,24 @@ class EmailView(View):
189 192 request.user.email = email_addr
190 193 request.user.save()
191 194 return http.HttpResponse(status=204)
  195 +
  196 +
  197 +class EmailValidationView(View):
  198 +
  199 + http_method_names = [u'post']
  200 +
  201 + def post(self, request):
  202 + email_addr = request.POST.get('email')
  203 + try:
  204 + email = EmailAddressValidation.objects.get(address=email_addr,
  205 + user=request.user)
  206 + except http.DoesNotExist:
  207 + raise http.Http404
  208 +
  209 + try:
  210 + send_verification_email(email_addr, request.user,
  211 + email.validation_key)
  212 + except smtplib.SMTPException:
  213 + return http.HttpResponseServerError()
  214 +
  215 + return http.HttpResponse(status=204)
... ...