diff --git a/src/accounts/templates/accounts/user_update_form.html b/src/accounts/templates/accounts/user_update_form.html index eda4130..1e177e9 100644 --- a/src/accounts/templates/accounts/user_update_form.html +++ b/src/accounts/templates/accounts/user_update_form.html @@ -43,6 +43,29 @@ $(function() { event.preventDefault(); }); + $('.verify-email').on('click', function(event) { + var $email_block = $(event.target).parent().parent(); + $.ajax({ + url: "{% url 'archive_email_validation_view' %}", + type: 'post', + data: { email: $('.email-address', $email_block).text() }, + context: $email_block[0], + beforeSend: function(xhr, settings) { + xhr.setRequestHeader("X-CSRFToken", $.cookie('csrftoken')); + } + }).done(function() { + var email = $('.email-address', $(this)).text(); + var msg = '{% trans "We sent a verification email to " %}' + email + '. ' + + '{% trans "Please follow the instructions in it." %}'; + $('#alert-message').text(msg); + $('#alert-js').removeClass('alert-warning').addClass('alert-success'); + $('#alert-js').show(); + window.scroll(0, 0); + }); + + event.preventDefault(); + }); + $('.set-primary').on('click', function(event) { var $email_block = $(event.target).parent().parent(); $.ajax({ diff --git a/src/super_archives/urls.py b/src/super_archives/urls.py index c233613..68b0c8d 100644 --- a/src/super_archives/urls.py +++ b/src/super_archives/urls.py @@ -1,6 +1,6 @@ from django.conf.urls import patterns, include, url -from .views import EmailView +from .views import EmailView, EmailValidationView urlpatterns = patterns('super_archives.views', @@ -8,6 +8,8 @@ urlpatterns = patterns('super_archives.views', url(r'thread/(?P[-\w]+)/(?P[-\w]+)$', 'thread', name="thread_view"), url(r'thread/$', 'list_messages', name='thread_list'), + url(r'manage/email/validate/?$', EmailValidationView.as_view(), + name="archive_email_validation_view"), url(r'manage/email/(?P[0-9a-z]{32})?', EmailView.as_view(), name="archive_email_view"), ) diff --git a/src/super_archives/views.py b/src/super_archives/views.py index 9ccfde6..5aa49f5 100644 --- a/src/super_archives/views.py +++ b/src/super_archives/views.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- +import smtplib + from django import http from django.contrib import messages from django.db import IntegrityError @@ -12,6 +14,7 @@ from django.contrib.auth.decorators import login_required from django.shortcuts import render, redirect from . import queries +from .utils import send_verification_email from .decorators import count_hit from .models import MailingList, Thread, EmailAddress, EmailAddressValidation @@ -95,7 +98,7 @@ class EmailView(View): try: email_val = EmailAddressValidation.objects.get(validation_key=key, - user=request.user) + user__pk=request.user.pk) except EmailAddressValidation.DoesNotExist: messages.error(request, _('The email address you are trying to ' 'verify either has already been verified ' @@ -189,3 +192,24 @@ class EmailView(View): request.user.email = email_addr request.user.save() return http.HttpResponse(status=204) + + +class EmailValidationView(View): + + http_method_names = [u'post'] + + def post(self, request): + email_addr = request.POST.get('email') + try: + email = EmailAddressValidation.objects.get(address=email_addr, + user=request.user) + except http.DoesNotExist: + raise http.Http404 + + try: + send_verification_email(email_addr, request.user, + email.validation_key) + except smtplib.SMTPException: + return http.HttpResponseServerError() + + return http.HttpResponse(status=204) -- libgit2 0.21.2