From 37c091d61194a53562a250a2f8324791075c5474 Mon Sep 17 00:00:00 2001 From: Carlos Oliveira Date: Thu, 20 Aug 2015 12:20:25 -0300 Subject: [PATCH] Fixed way to build verification url --- colab/accounts/views.py | 7 ++++++- colab/super_archives/models.py | 8 ++++++-- colab/super_archives/templates/superarchives/emails/email_verification.txt | 2 +- colab/super_archives/utils/email.py | 6 +++--- colab/super_archives/views.py | 10 +++++++--- 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/colab/accounts/views.py b/colab/accounts/views.py index 280b35f..8007cab 100644 --- a/colab/accounts/views.py +++ b/colab/accounts/views.py @@ -103,7 +103,12 @@ def signup(request): user.is_active = False user.save() - EmailAddressValidation.create(user.email, user) + email = EmailAddressValidation.create(user.email, user) + + location = reverse('archive_email_view', + kwargs={'key': email.validation_key}) + verification_url = request.build_absolute_uri(location) + EmailAddressValidation.verify_email(email, verification_url) # Check if the user's email have been used previously # in the mainling lists to link the user to old messages diff --git a/colab/super_archives/models.py b/colab/super_archives/models.py index 6b94b74..60ff9e7 100644 --- a/colab/super_archives/models.py +++ b/colab/super_archives/models.py @@ -41,10 +41,14 @@ class EmailAddressValidation(models.Model): def create(cls, address, user): email_address_validation = cls.objects.create(address=address, user=user) + return email_address_validation + + @classmethod + def verify_email(cls, email_address_validation, verification_url): email.send_verification_email(email_address_validation.address, email_address_validation.user, - email_address_validation.validation_key) - return email_address_validation + email_address_validation.validation_key, + verification_url) class EmailAddress(models.Model): diff --git a/colab/super_archives/templates/superarchives/emails/email_verification.txt b/colab/super_archives/templates/superarchives/emails/email_verification.txt index 37d4321..7e671f9 100644 --- a/colab/super_archives/templates/superarchives/emails/email_verification.txt +++ b/colab/super_archives/templates/superarchives/emails/email_verification.txt @@ -1,6 +1,6 @@ {% load i18n %} {% blocktrans with fullname=user.get_full_name|title username=user.username|lower %}Hey, we want to verify that you are indeed "{{ fullname }} ({{ username }})". If that's the case, please follow the link below:{% endblocktrans %} -{{ SITE_URL }}{% url 'archive_email_view' key %} +{{ verification_url }} {% blocktrans with username=user.username %}If you're not {{ username }} or didn't request verification you can ignore this email.{% endblocktrans %} diff --git a/colab/super_archives/utils/email.py b/colab/super_archives/utils/email.py index d77f235..922fc55 100644 --- a/colab/super_archives/utils/email.py +++ b/colab/super_archives/utils/email.py @@ -10,11 +10,11 @@ def colab_send_email(subject, message, to): return mail.send_mail(subject, message, from_email, [to]) -def send_verification_email(to, user, validation_key): +def send_verification_email(to, user, validation_key, verification_url): subject = _('Please verify your email ') + u'{}'.format(to) msg_tmpl = \ loader.get_template('superarchives/emails/email_verification.txt') message = msg_tmpl.render(Context({'to': to, 'user': user, - 'key': validation_key, - 'SITE_URL': settings.SITE_URL})) + 'verification_url': verification_url + })) return colab_send_email(subject, message, to) diff --git a/colab/super_archives/views.py b/colab/super_archives/views.py index da6377e..fc8c706 100644 --- a/colab/super_archives/views.py +++ b/colab/super_archives/views.py @@ -9,6 +9,7 @@ import requests from django import http from django.conf import settings from django.contrib import messages +from django.core.urlresolvers import reverse from django.db import IntegrityError from django.views.generic import View from django.utils.translation import ugettext as _ @@ -177,7 +178,7 @@ class EmailView(View): except EmailAddressValidation.DoesNotExist: messages.error(request, _('The email address you are trying to ' 'verify either has already been verified' - 'or does not exist.')) + ' or does not exist.')) return redirect('/') try: @@ -289,8 +290,11 @@ class EmailValidationView(View): raise http.Http404 try: - send_verification_email(email_addr, email.user, - email.validation_key) + location = reverse('archive_email_view', + kwargs={'key': email.validation_key}) + verification_url = request.build_absolute_uri(location) + send_verification_email(request, email_addr, email.user, + email.validation_key, verification_url) except smtplib.SMTPException: logging.exception('Error sending validation email') return http.HttpResponseServerError() -- libgit2 0.21.2