Commits

James Bennett committed 6502163

Add an admin action for re-sending activation emails.

Comments (0)

Files changed (2)

registration/admin.py

 from django.contrib import admin
+from django.contrib.sites.models import RequestSite
+from django.contrib.sites.models import Site
+from django.utils.translation import ugettext_lazy as _
 
 from registration.models import RegistrationProfile
 
 
 class RegistrationAdmin(admin.ModelAdmin):
+    actions = ['resend_activation_email']
     list_display = ('__unicode__', 'activation_key_expired')
     raw_id_fields = ['user']
     search_fields = ('user__username', 'user__first_name')
 
+    def resend_activation_email(self, request, queryset):
+        """
+        Re-sends activation emails for the selected users.
+
+        Note that this will *only* send activation emails for users
+        who are eligible to activate; emails will not be sent to users
+        whose activation keys have expired or who have already
+        activated.
+        
+        """
+        if Site._meta.installed:
+            site = Site.objects.get_current()
+        else:
+            site = RequestSite(request)
+
+        for profile in queryset:
+            if not profile.activation_key_expired():
+                profile.send_activation_email(site)
+    resend_activation_email.short_description = _("Re-send activation emails")
+
 
 admin.site.register(RegistrationProfile, RegistrationAdmin)

registration/tests/backends.py

 import datetime
 
 from django.conf import settings
+from django.contrib import admin
 from django.contrib.auth.models import User
 from django.contrib.sites.models import Site
 from django.core import mail
 
 from registration import forms
 from registration import signals
+from registration.admin import RegistrationAdmin
 from registration.backends import get_backend
 from registration.backends.default import DefaultBackend
 from registration.models import RegistrationProfile
 
         self.assertEqual(len(received_signals), 0)
 
+    def test_email_send_action(self):
+        """
+        Test re-sending of activation emails via admin action.
+        
+        """
+        admin_class = RegistrationAdmin(RegistrationProfile, admin.site)
+        
+        alice = self.backend.register(_mock_request(),
+                                      username='alice',
+                                      email='alice@example.com',
+                                      password1='swordfish')
+        
+        admin_class.resend_activation_email(_mock_request(),
+                                            RegistrationProfile.objects.all())
+        self.assertEqual(len(mail.outbox), 2) # One on registering, one more on the resend.
+        
+        RegistrationProfile.objects.filter(user=alice).update(activation_key=RegistrationProfile.ACTIVATED)
+        admin_class.resend_activation_email(_mock_request(),
+                                            RegistrationProfile.objects.all())
+        self.assertEqual(len(mail.outbox), 2) # No additional email because the account has activated.