From eb6db921938c72c0655ca2e381818a471f99f5fa Mon Sep 17 00:00:00 2001 From: Jeremy Kerr Date: Thu, 21 Aug 2008 13:41:10 +0800 Subject: [PATCH] Implement confirmation emails. To do this, we need to allow sucessive requests for the same confirmation URL. Signed-off-by: Jeremy Kerr --- apps/patchwork/forms.py | 2 +- apps/patchwork/models.py | 13 ++++++++++-- apps/patchwork/requestcontext.py | 2 ++ apps/patchwork/views/user.py | 34 ++++++++++++++++++++++++------ apps/settings.py | 1 + templates/patchwork/register.html | 11 +++++----- templates/patchwork/register.mail | 11 ++++++++++ templates/patchwork/user-link.html | 5 ++++- templates/patchwork/user-link.mail | 12 +++++++++++ 9 files changed, 75 insertions(+), 16 deletions(-) create mode 100644 templates/patchwork/register.mail create mode 100644 templates/patchwork/user-link.mail diff --git a/apps/patchwork/forms.py b/apps/patchwork/forms.py index ed55c4f..22408d9 100644 --- a/apps/patchwork/forms.py +++ b/apps/patchwork/forms.py @@ -30,7 +30,7 @@ class RegisterForm(forms.ModelForm): class Meta: model = RegistrationRequest - exclude = ['key'] + exclude = ['key', 'active', 'date'] def clean_email(self): value = self.cleaned_data['email'] diff --git a/apps/patchwork/models.py b/apps/patchwork/models.py index f6943fc..7eb28d0 100644 --- a/apps/patchwork/models.py +++ b/apps/patchwork/models.py @@ -145,8 +145,12 @@ class RegistrationRequest(models.Model): email = models.CharField(max_length = 200, unique = True) password = models.CharField(max_length = 200) key = models.CharField(max_length = 32, default = _confirm_key) + date = models.DateTimeField(default=datetime.datetime.now) + active = models.BooleanField(default = True) def create_user(self): + if not self.active: + return user = User.objects.create_user(self.username, self.email, self.password) user.first_name = self.first_name @@ -154,7 +158,8 @@ class RegistrationRequest(models.Model): user.save() profile = UserProfile(user = user) profile.save() - self.delete() + self.active = False + self.save() # link a person to this user. if none exists, create. person = None @@ -176,10 +181,13 @@ class RegistrationRequest(models.Model): class UserPersonConfirmation(models.Model): user = models.ForeignKey(User) email = models.CharField(max_length = 200) - date = models.DateTimeField(default=datetime.datetime.now) key = models.CharField(max_length = 32, default = _confirm_key) + date = models.DateTimeField(default=datetime.datetime.now) + active = models.BooleanField(default = True) def confirm(self): + if not self.active: + return person = None try: person = Person.objects.get(email = self.email) @@ -190,6 +198,7 @@ class UserPersonConfirmation(models.Model): person.link_to_user(self.user) person.save() + self.active = False class Admin: diff --git a/apps/patchwork/requestcontext.py b/apps/patchwork/requestcontext.py index cb9a782..e9a2675 100644 --- a/apps/patchwork/requestcontext.py +++ b/apps/patchwork/requestcontext.py @@ -19,6 +19,7 @@ from django.template import RequestContext from django.utils.html import escape +from django.contrib.sites.models import Site from patchwork.filters import Filters from patchwork.models import Bundle, Project @@ -65,6 +66,7 @@ class PatchworkRequestContext(RequestContext): self.update({ 'project': self.project, + 'site': Site.objects.get_current(), 'other_projects': len(self.projects) > 1 }) diff --git a/apps/patchwork/views/user.py b/apps/patchwork/views/user.py index 223cfc6..59d01a5 100644 --- a/apps/patchwork/views/user.py +++ b/apps/patchwork/views/user.py @@ -31,6 +31,10 @@ from patchwork.utils import Order, get_patch_ids, set_patches from patchwork.filters import DelegateFilter from patchwork.paginator import Paginator from patchwork.views import generic_list +from django.template.loader import render_to_string +from django.template import Context +from django.conf import settings +from django.core.mail import send_mail import django.core.urlresolvers def register(request): @@ -47,9 +51,19 @@ def register(request): if form.is_valid(): form.save() - context['request'] = reg_req - else: - context['form'] = form + try: + context['request'] = reg_req + send_mail('Patchwork account confirmation', + render_to_string('patchwork/register.mail', context), + settings.PATCHWORK_FROM_EMAIL, + [form.cleaned_data['email']]) + + except Exception, ex: + context['request'] = None + context['error'] = 'An error occurred during registration. ' + \ + 'Please try again later' + + context['form'] = form return render_to_response(template, context) @@ -128,9 +142,19 @@ def link(request): if form.is_valid(): conf = UserPersonConfirmation(user = request.user, email = form.cleaned_data['email']) - conf.save() context['confirmation'] = conf + try: + send_mail('Patchwork email address confirmation', + render_to_string('patchwork/user-link.mail', + context), + settings.PATCHWORK_FROM_EMAIL, + [form.cleaned_data['email']]) + conf.save() + except Exception, ex: + context['confirmation'] = None + context['error'] = 'An error occurred during confirmation. ' + \ + 'Please try again later' context['linkform'] = form return render_to_response('patchwork/user-link.html', context) @@ -146,8 +170,6 @@ def link_confirm(request, key): else: context['person'] = Person.objects.get(email = confirmation.email) - confirmation.delete() - return render_to_response('patchwork/user-link-confirm.html', context) @login_required diff --git a/apps/settings.py b/apps/settings.py index 0d74b10..df656de 100644 --- a/apps/settings.py +++ b/apps/settings.py @@ -92,3 +92,4 @@ INSTALLED_APPS = ( ) DEFAULT_PATCHES_PER_PAGE = 100 +PATCHWORK_FROM_EMAIL = 'Patchwork ' diff --git a/templates/patchwork/register.html b/templates/patchwork/register.html index 8bd422e..4790fac 100644 --- a/templates/patchwork/register.html +++ b/templates/patchwork/register.html @@ -6,13 +6,12 @@ {% block body %} -{% if request %} +{% if request and not error %}

Registration successful!

-

email sent to {{ request.email }}

-

Beta note: While we're testing, the confirmation email has been replaced - by a single link: - {% url patchwork.views.user.register_confirm key=request.key %} +

A confirmation email has been sent to {{ request.email }}. You'll + need to visit the link provided in that email to confirm your + registration.

+
{{email}}

{% else %}

By creating a patchwork account, you can:

diff --git a/templates/patchwork/register.mail b/templates/patchwork/register.mail new file mode 100644 index 0000000..86dcd14 --- /dev/null +++ b/templates/patchwork/register.mail @@ -0,0 +1,11 @@ +Hi, + +This email is to confirm your account on the patchwork patch-tracking +system. You can activate your account by visiting the url: + + http://{{site.domain}}{% url patchwork.views.user.register_confirm key=request.key %} + +If you didn't request a user account on patchwork, then you can ignore +this mail. + +Happy patchworking. diff --git a/templates/patchwork/user-link.html b/templates/patchwork/user-link.html index 3eeb527..588a999 100644 --- a/templates/patchwork/user-link.html +++ b/templates/patchwork/user-link.html @@ -6,7 +6,7 @@ {% block body %} -{% if confirmation %} +{% if confirmation and not error %}

A confirmation email has been sent to {{ confirmation.email }}.

beta link: There was an error submitting your link request.

{{ form.non_field_errors }} {% endif %} + {% if error %} +
  • {{error}}
+ {% endif %}
{{linkform.email.errors}} diff --git a/templates/patchwork/user-link.mail b/templates/patchwork/user-link.mail new file mode 100644 index 0000000..5f74d3b --- /dev/null +++ b/templates/patchwork/user-link.mail @@ -0,0 +1,12 @@ +Hi, + +This email is to confirm that you own the email address: + + {{ confirmation.email }} + +So that you can add it to your patchwork profile. You can confirm this +email address by visiting the url: + + http://{{site.domain}}{% url patchwork.views.user.link_confirm key=confirmation.key %} + +Happy patchworking. -- 2.39.2