Skip to content

Commit

Permalink
Added mechanism to allow existing Users to be notified (emailed) when…
Browse files Browse the repository at this point in the history
… they're added to an organization.
  • Loading branch information
Paul Backhouse committed Sep 7, 2012
1 parent 2d392b1 commit 31f8af7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
4 changes: 3 additions & 1 deletion organizations/app_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

from organizations.utils import model_field_attr


ORGS_INVITATION_BACKEND = getattr(settings, 'INVITATION_BACKEND',
'organizations.backends.defaults.InvitationBackend')

ORGS_REGISTRATION_BACKEND = getattr(settings, 'REGISTRATION_BACKEND',
'organizations.backends.defaults.RegistrationBackend')

ORGS_NOTIFICATION_BACKEND = getattr(settings, 'NOTIFICATION_BACKEND',
'organizations.backends.defaults.NotificationBackend')

ORGS_EMAIL_LENGTH = model_field_attr(User, 'email', 'max_length')
8 changes: 6 additions & 2 deletions organizations/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from django.utils.importlib import import_module

from organizations.app_settings import (ORGS_INVITATION_BACKEND,
ORGS_REGISTRATION_BACKEND)

ORGS_REGISTRATION_BACKEND, ORGS_NOTIFICATION_BACKEND)

def invitation_backend():
# TODO exception handling
Expand All @@ -15,3 +14,8 @@ def registration_backend():
class_module, class_name = ORGS_REGISTRATION_BACKEND.rsplit('.', 1)
mod = import_module(class_module)
return getattr(mod, class_name)()

def notification_backend():
class_module, class_name = ORGS_NOTIFICATION_BACKEND.rsplit('.', 1)
mod = import_module(class_module)
return getattr(mod, class_name)()
29 changes: 24 additions & 5 deletions organizations/backends/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,10 @@ def send_reminder(self, user, sender=None, **kwargs):
def _send_email(self, user, subject_template, body_template,
sender=None, **kwargs):
"""Utility method for sending emails to new users"""
if not sender:
try:
from_email = settings.DEFAULT_FROM_EMAIL
except AttributeError:
raise ImproperlyConfigured(_("You must define DEFAULT_FROM_EMAIL in your settings"))
try:
from_email = settings.DEFAULT_FROM_EMAIL
except AttributeError:
raise ImproperlyConfigured(_("You must define DEFAULT_FROM_EMAIL in your settings"))

if sender:
from_email = "%s %s <%s>" % (sender.first_name, sender.last_name,
Expand Down Expand Up @@ -232,3 +231,23 @@ def send_invitation(self, user, sender=None, **kwargs):
kwargs.update({'token': token})
self._send_email(user, self.invitation_subject, self.invitation_body,
sender, **kwargs)

class NotificationBackend(BaseBackend):
"""A backend for notifying existing users that they have been added an
organization.
"""
notification_subject = 'organizations/email/notification_subject.txt'
notification_body = 'organizations/email/notification_body.html'

def notify_by_email(self, email, sender=None, request=None, **kwargs):
"""Sends an active user a notification email
"""
try:
user = User.objects.get(email=email)
if not user.is_active:
return False
self._send_email(user, self.notification_subject, self.notification_body,
sender, **kwargs)
except User.DoesNotExist:
pass

13 changes: 10 additions & 3 deletions organizations/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from organizations.models import Organization, OrganizationUser
from organizations.utils import create_organization
from organizations.backends import invitation_backend
from organizations.backends import invitation_backend, notification_backend


class OrganizationForm(forms.ModelForm):
Expand Down Expand Up @@ -78,8 +78,15 @@ def save(self, *args, **kwargs):
except User.DoesNotExist:
user = invitation_backend().invite_by_email(
self.cleaned_data['email'],
**{'domain': get_current_site(self.request),
'organization': self.organization})
**{'site': get_current_site(self.request),
'organization': self.organization,
'sender': self.request.user})
else:
notification_backend().notify_by_email(self.cleaned_data['email'],
**{'site': get_current_site(self.request),
'organization': self.organization,
'sender': self.request.user})

return OrganizationUser.objects.create(user=user,
organization=self.organization,
is_admin=self.cleaned_data['is_admin'])
Expand Down

0 comments on commit 31f8af7

Please sign in to comment.