Commits

Anonymous committed 3c712e4

Implement confirmation emails.

To do this, we need to allow sucessive requests for the same
confirmation URL.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>

Comments (0)

Files changed (9)

apps/patchwork/forms.py

 
     class Meta:
         model = RegistrationRequest
-        exclude = ['key']
+        exclude = ['key', 'active', 'date']
 
     def clean_email(self):
         value = self.cleaned_data['email']

apps/patchwork/models.py

     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
         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
 class UserPersonConfirmation(models.Model):
     user = models.ForeignKey(User)
     email = models.CharField(max_length = 200)
+    key = models.CharField(max_length = 32, default = _confirm_key)
     date = models.DateTimeField(default=datetime.datetime.now)
-    key = models.CharField(max_length = 32, default = _confirm_key)
+    active = models.BooleanField(default = True)
 
     def confirm(self):
+	if not self.active:
+	    return
         person = None
         try:
             person = Person.objects.get(email = self.email)
 
         person.link_to_user(self.user)
         person.save()
+        self.active = False
 
 
     class Admin:

apps/patchwork/requestcontext.py

 
 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
 
 
         self.update({
                 'project': self.project,
+                'site': Site.objects.get_current(),
                 'other_projects': len(self.projects) > 1
             })
 

apps/patchwork/views/user.py

 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):
 
     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)
 
         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)
     else:
         context['person'] = Person.objects.get(email = confirmation.email)
 
-    confirmation.delete()
-
     return render_to_response('patchwork/user-link-confirm.html', context)
 
 @login_required
 )
 
 DEFAULT_PATCHES_PER_PAGE = 100
+PATCHWORK_FROM_EMAIL = 'Patchwork <patchwork@patchwork.example.com>'

templates/patchwork/register.html

 
 {% 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>

templates/patchwork/register.mail

+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.

templates/patchwork/user-link.html

 
 {% block body %}
 
-{% if confirmation %}
+{% if confirmation and not error %}
 <p>A confirmation email has been sent to {{ confirmation.email }}.</p>
 
 <p>beta link: <a
    <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}}

templates/patchwork/user-link.mail

+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.