Doug Latornell avatar Doug Latornell committed 615c244

Add admin function to email event page URL to club webmaster.

Comments (0)

Files changed (6)

media/register/templates/email/to_webmaster.txt

+The pre-registration page for the {{ event }} event has been added to the RandoPony. The URL is {{ event_page_url }}
+
+This is an auto-generated email. If you are having problems with the RandoPony system please send email to {{ admin_email }}
+
+Sincerely,
+
+The Rando Pony

register/admin.py

 """
 # Django:
 from django import forms
+from django.conf import settings
+from django.core import mail
+from django.core.urlresolvers import reverse
 from django.contrib import admin
 from django.core.validators import validate_email
+from django.template.loader import render_to_string
 # Model classes:
 from randopony.register.models import Brevet
 from randopony.register.models import BrevetRider
 from randopony.register.models import ClubEvent
-
-
-def clean_email_address_list(data):
-    """Custom validator for a comma separated list of email addresses.
-    """
-    for email in (email.strip() for email in data.split(',')):
-        validate_email(email)
-    return data
-
+        
 
 class CustomBrevetAdminForm(forms.ModelForm):
     """Custom brevet admin forms to validate organizer email
         model = Brevet
 
     def clean_organizer_email(self):
-        data = clean_email_address_list(self.cleaned_data['organizer_email'])
+        data = _clean_email_address_list(self.cleaned_data['organizer_email'])
         return data
 
 
     # Display the brevets distance choices as radio buttons instead of
     # a select list
     radio_fields = {'event': admin.HORIZONTAL}
+
+    actions = ['notify_webmaster']
+
+    def notify_webmaster(self, request, queryset):
+        _notify_webmaster(request, queryset)
+        brevets_count = queryset.count()
+        if brevets_count == 1:
+            msg_bit = 'URL for 1 brevet'
+        else:
+            msg_bit = 'URLs for {0} brevets'.format(brevets_count)
+        self.message_user(request, '{0} sent to webmaster'.format(msg_bit))
+    description = 'Send email with URL for brevet to webmaster'
+    notify_webmaster.short_description = description
 admin.site.register(Brevet, BrevetAdmin)
 
 
         model = ClubEvent
 
     def clean_organizer_email(self):
-        data = clean_email_address_list(self.cleaned_data['organizer_email'])
+        data = _clean_email_address_list(self.cleaned_data['organizer_email'])
         return data
 
 
     # Display the event type choices as radio buttons instead of a
     # select list
     radio_fields = {'event': admin.HORIZONTAL}
+
+    actions = ['notify_webmaster']
+
+    def notify_webmaster(self, request, queryset):
+        _notify_webmaster(request, queryset)
+        events_count = queryset.count()
+        if events_count == 1:
+            msg_bit = 'URL for 1 event'
+        else:
+            msg_bit = 'URLs for {0} events'.format(events_count)
+        self.message_user(request, '{0} sent to webmaster'.format(msg_bit))
+    description = 'Send email with URL for event to webmaster'
+    notify_webmaster.short_description = description
 admin.site.register(ClubEvent, ClubEventAdmin)
         
 
         ('Qualifying info', {'fields': ['club_member', 'info_answer']})
     ]
 admin.site.register(BrevetRider, RiderAdmin)
+
+
+
+def _clean_email_address_list(data):
+    """Custom validator for a comma separated list of email addresses.
+    """
+    for email in (email.strip() for email in data.split(',')):
+        validate_email(email)
+    return data
+
+
+def _notify_webmaster(request, queryset):
+    """Send email message to club webmaster containing the URL of the
+    pre-registration page for the event(s) in the queryset.
+
+    Handle for notify_webmaster admin actions.
+    """
+    for event in queryset:
+        event_page = reverse(
+            'register:brevet',
+            args=(event.region, event.event, event.date.strftime('%d%b%Y')))
+        host = request.get_host()
+        event_page_url = 'http://{0}{1}'.format(host, event_page)
+        email = mail.EmailMessage(
+            subject='RandoPony Pre-registration Page for {0}'.format(event),
+            body=render_to_string(
+                'email/to_webmaster.txt',
+                {'event': event,
+                 'event_page_url': event_page_url,
+                 'admin_email': settings.ADMINS[0][1]}
+            ),
+            from_email=settings.REGISTRATION_EMAIL_FROM,
+            to=[settings.WEBMASTER_EMAIL],
+        )
+        email.send()

register/fixtures/club_events.yaml

+# Test data fixture for RandoPony register app ClubEvent model
+
+- model: register.ClubEvent
+  pk: 1
+  fields:
+    date: 2010-03-16
+    event: Dinner
+    organizer_email: jeff@example.com
+    region: Club
+    location: Moose's Down Under
+    time: '18:00'
+
+- model: register.ClubEvent
+  pk: 2
+  fields:
+    date: 2010-10-03
+    event: AGM
+    organizer_email: harold@example.com
+    region: Club
+    location: Bedford House, Fort Langley
+    time: '13:00'

register/tests/test_admin.py

 # Django:
 import django.test
 from django.contrib.auth.models import User
+from django.conf import settings
+from django.core import mail
 
 
 class TestAdminBrevet(django.test.TestCase):
     """Functional tests for the add/change brevet admin form.
     """
+    fixtures = ['brevets']
+    
     def setUp(self):
         user = User.objects.create_superuser(
             'test_admin', 'test_admin@example.com', 'foobar42')
             'mcroy@example.com, dug.andrusiek@example.com')
 
 
+    def test_brevet_notify_webmaster_1_brevet(self):
+        """notify webmaster admin action sends email for 1 brevet
+        """
+        params = {
+            u'action': [u'notify_webmaster'],
+            u'_selected_action': [u'1'],
+        }
+        response = self.client.post(
+            '/admin/register/brevet/', params, follow=True)
+        self.assertContains(response, 'URL for 1 brevet sent to webmaster')
+        self.assertEqual(len(mail.outbox), 1)
+        self.assertEqual(
+            mail.outbox[0].subject,
+            'RandoPony Pre-registration Page for LM300 01-May-2010')
+        self.assertEqual(mail.outbox[0].to, [settings.WEBMASTER_EMAIL])
+        self.assertEqual(
+            mail.outbox[0].from_email, settings.REGISTRATION_EMAIL_FROM)
+        body = mail.outbox[0].body
+        self.assertTrue(
+            'pre-registration page for the LM300 01-May-2010 event' in body)
+        self.assertTrue(
+            'The URL is http://testserver/register/LM300/01May2010/' in body)
+        self.assertTrue(
+            'please send email to {0}'.format(settings.ADMINS[0][1])
+            in body)
+
+
+    def test_brevet_notify_webmaster_2_brevets(self):
+        """notify webmaster admin action sends email for 2 brevets
+        """
+        params = {
+            u'action': [u'notify_webmaster'],
+            u'_selected_action': [u'1', u'2'],
+        }
+        response = self.client.post(
+            '/admin/register/brevet/', params, follow=True)
+        self.assertContains(response, 'URLs for 2 brevets sent to webmaster')
+        self.assertEqual(len(mail.outbox), 2)
+        self.assertEqual(
+            mail.outbox[0].subject,
+            'RandoPony Pre-registration Page for LM300 01-May-2010')
+        self.assertEqual(
+            mail.outbox[1].subject,
+            'RandoPony Pre-registration Page for LM400 22-May-2010')
+
+
 class TestAdminClubEvent(django.test.TestCase):
     """Functional tests for the add/change brevet admin form.
     """
+    fixtures = ['club_events']
+    
     def setUp(self):
         user = User.objects.create_superuser(
             'test_admin', 'test_admin@example.com', 'foobar42')
         self.assertEqual(
             event.organizer_email,
             'mcroy@example.com, dug.andrusiek@example.com')
+
+        
+    def test_club_event_notify_webmaster_1_event(self):
+        """notify webmaster admin action sends email for 1 club event
+        """
+        params = {
+            u'action': [u'notify_webmaster'],
+            u'_selected_action': [u'1'],
+        }
+        response = self.client.post(
+            '/admin/register/clubevent/', params, follow=True)
+        self.assertContains(response, 'URL for 1 event sent to webmaster')
+        self.assertEqual(len(mail.outbox), 1)
+        self.assertEqual(
+            mail.outbox[0].subject,
+            'RandoPony Pre-registration Page for Dinner 16-Mar-2010')
+        self.assertEqual(mail.outbox[0].to, [settings.WEBMASTER_EMAIL])
+        self.assertEqual(
+            mail.outbox[0].from_email, settings.REGISTRATION_EMAIL_FROM)
+        body = mail.outbox[0].body
+        self.assertTrue(
+            'pre-registration page for the Dinner 16-Mar-2010 event' in body)
+        self.assertTrue(
+            'The URL is http://testserver/register/ClubDinner/16Mar2010/'
+            in body)
+        self.assertTrue(
+            'please send email to {0}'.format(settings.ADMINS[0][1])
+            in body)
+
+
+    def test_club_event_notify_webmaster_2_events(self):
+        """notify webmaster admin action sends email for 2 club events
+        """
+        params = {
+            u'action': [u'notify_webmaster'],
+            u'_selected_action': [u'1', u'2'],
+        }
+        response = self.client.post(
+            '/admin/register/clubevent/', params, follow=True)
+        self.assertContains(response, 'URLs for 2 events sent to webmaster')
+        self.assertEqual(len(mail.outbox), 2)
+        self.assertEqual(
+            mail.outbox[0].subject,
+            'RandoPony Pre-registration Page for Dinner 16-Mar-2010')
+        self.assertEqual(
+            mail.outbox[1].subject,
+            'RandoPony Pre-registration Page for AGM 03-Oct-2010')
 )
 
 # Application-specific settings:
+
 # CAPTCHA question and answer for brevet pre-registration
 REGISTRATION_FORM_CAPTCHA_QUESTION = (
     'Are you a human? Are you a randonneur? Please prove it. '
     '200 km, 300 km, ___ km, and 600 km. Fill in the blank:'
 )
 REGISTRATION_FORM_CAPTCHA_ANSWER = 400
+
 # Email from address for messages from registration form handler to
 # brevet organizer
 REGISTRATION_EMAIL_FROM = 'randopony@sadahome.ca'
+
+# Email address of club webmaster for pre-registration page setup
+# messages from admin
+WEBMASTER_EMAIL = 'eric_fergusson@telus.net'
+
 # SMTP server settings
 #
 # Use the Python standard library SMTP DebuggingServer to handle email
 # by printing it to stdout. Run the server with:
 #    python -m smtpd -n -c DebuggingServer localhost:1025
 EMAIL_HOST = '127.0.0.1'
-EMAIL_POST = 1025
+EMAIL_PORT = 1025

webfaction-settings.py

 )
 
 # Application-specific settings:
+
 # CAPTCHA question and answer for brevet pre-registration
 REGISTRATION_FORM_CAPTCHA_QUESTION = (
     'Are you a human? Are you a randonneur? Please prove it. '
     '200 km, 300 km, ___ km, and 600 km. Fill in the blank:'
 )
 REGISTRATION_FORM_CAPTCHA_ANSWER = 400
+
 # Email from address for messages from registration form handler to
 # brevet organizer
 REGISTRATION_EMAIL_FROM = 'randopony@sadahome.ca'
+
+# Email address of club webmaster for pre-registration page setup
+# messages from admin
+WEBMASTER_EMAIL = 'eric_fergusson@telus.net'
+
 # SMTP server settings
 EMAIL_HOST = 'smtp.webfaction.com'
 EMAIL_HOST_USER = 'randopony'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.