]> git.ozlabs.org Git - patchwork/commitdiff
Implement confirmation emails.
authorJeremy Kerr <jk@ozlabs.org>
Thu, 21 Aug 2008 05:41:10 +0000 (13:41 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Thu, 21 Aug 2008 05:41:10 +0000 (13:41 +0800)
To do this, we need to allow sucessive requests for the same
confirmation URL.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
apps/patchwork/forms.py
apps/patchwork/models.py
apps/patchwork/requestcontext.py
apps/patchwork/views/user.py
apps/settings.py
templates/patchwork/register.html
templates/patchwork/register.mail [new file with mode: 0644]
templates/patchwork/user-link.html
templates/patchwork/user-link.mail [new file with mode: 0644]

index ed55c4fc0bbdb1d6bdd15ed831343b0272037afb..22408d93791cf5dfd41c9d67e44541cb04160fa8 100644 (file)
@@ -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']
index f6943fc5296553ff10050b8925fd563175d276e4..7eb28d0853ca099b754b3744af3423dac3d23e02 100644 (file)
@@ -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:
index cb9a78290909e7f0d392ab66f392ec491c96d654..e9a267519fa3e1bf772daa31f511f5437aef1afe 100644 (file)
@@ -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
             })
 
index 223cfc6bcb4cd99f61b75fdbcac5e0ca9455e35a..59d01a57311d6d6e8011fd5726dbc7450d73e1ee 100644 (file)
@@ -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
index 0d74b107bae8168f751dac9a1ccb789b9156089c..df656dec951abc631c0ec3f1740f13d811fb4fca 100644 (file)
@@ -92,3 +92,4 @@ INSTALLED_APPS = (
 )
 
 DEFAULT_PATCHES_PER_PAGE = 100
+PATCHWORK_FROM_EMAIL = 'Patchwork <patchwork@patchwork.example.com>'
index 8bd422e893ead29ff67f89975c0484147870dbc7..4790facc439f71ee14c28b0c95c516f1ac789ae8 100644 (file)
@@ -6,13 +6,12 @@
 
 {% block body %}
 
-{% if request %}
+{% if request and not error %}
  <p>Registration successful!</p>
- <p>email sent to {{ request.email }}</p>
- <p>Beta note: While we're testing, the confirmation email has been replaced
- by a single link:
- <a href="{% url patchwork.views.user.register_confirm key=request.key %}"
- >{% url patchwork.views.user.register_confirm key=request.key %}</a>
+ <p>A confirmation email has been sent to {{ request.email }}. You'll
+ need to visit the link provided in that email to confirm your
+ registration.</p>
+ <pre>{{email}}</pre>
 </p>
 {% else %}
 <p>By creating a patchwork account, you can:<p>
diff --git a/templates/patchwork/register.mail b/templates/patchwork/register.mail
new file mode 100644 (file)
index 0000000..86dcd14
--- /dev/null
@@ -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.
index 3eeb527392279669e50b01f1918636a36ff4f931..588a9994beeef29f569e43e502d6e49c4e1fa0af 100644 (file)
@@ -6,7 +6,7 @@
 
 {% block body %}
 
-{% if confirmation %}
+{% if confirmation and not error %}
 <p>A confirmation email has been sent to {{ confirmation.email }}.</p>
 
 <p>beta link: <a
@@ -19,6 +19,9 @@
    <p>There was an error submitting your link request.</p>
     {{ form.non_field_errors }}
    {% endif %}
+   {% if error %}
+    <ul class="errorlist"><li>{{error}}</li></ul>
+   {% endif %}
 
    <form action="{% url patchwork.views.user.link %}" method="post">
     {{linkform.email.errors}}
diff --git a/templates/patchwork/user-link.mail b/templates/patchwork/user-link.mail
new file mode 100644 (file)
index 0000000..5f74d3b
--- /dev/null
@@ -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.