Commits

Marko Mrdjenovic committed 943d706

added invitation exception

Comments (0)

Files changed (3)

registration/backends/__init__.py

     return backend_class()
     
 
+class InvitationException(Exception):
+    pass
+    
+
 class EmptyBackend(object):
     """
     A skeleton for backends that fit with the views and models provided in 

registration/backends/invitational/__init__.py

 from django.conf import settings
 from django.contrib.auth import authenticate, login
+from django.contrib.auth.models import User
+from django.utils.translation import ugettext_lazy as _
 
 from registration import signals
+from registration.backends import InvitationException
 from registration.forms import InvitationForm
 from registration.forms import RegistrationInvitationalForm
 from registration.models import EmailValidation
 from registration.future import get_current_site
 
 
+class UserAlreadyExists(InvitationException):
+    pass
+    
+
 class InvitationBackend(object):
     """
     An invitation-registration backend which follows a simple workflow:
     invitation_model_class = RegistrationInvitation
     validation_model_class = EmailValidation
     
+    
     def invite(self, request, **kwargs):
         """
         Given a user and an email, send an invitation to the email if the 
         """
         user, email = request.user, kwargs['email']
         if user.is_authenticated():
-            site = get_current_site(request)
-            return self.invitation_model_class.objects.send_invitation(user, 
-                                                                  email, site)
+            try:
+                User.objects.get(email=email)
+                raise UserAlreadyExists(_("User with the specified email already exists."))
+            except User.DoesNotExist:
+                site = get_current_site(request)
+                return self.invitation_model_class.objects.send_invitation(user, 
+                                                                           email, 
+                                                                           site)
         return None
     
     def get_invitation_form_class(self, request):

registration/views.py

 from django.utils.translation import ugettext_lazy as _
 
 from registration.backends import get_backend
+from registration.backends.invitational import UserAlreadyExists, InvitationException
 from registration.models import RegistrationInvitation
 
 
     if request.method == 'POST':
         form = form_class(data=request.POST, files=request.FILES)
         if form.is_valid():
-            invitation = backend.invite(request, **form.cleaned_data)
-            if invitation:
-                if success_url is None:
-                    to, args, kwargs = backend.post_invitation_redirect(request, invitation)
-                    return redirect(to, *args, **kwargs)
+            try:
+                invitation = backend.invite(request, **form.cleaned_data)
+                if invitation:
+                    if success_url is None:
+                        to, args, kwargs = backend.post_invitation_redirect(request, invitation)
+                        return redirect(to, *args, **kwargs)
+                    else:
+                        return redirect(success_url)
                 else:
-                    return redirect(success_url)
-            else:
-                error = _("Sending the invitation failed.")
+                    error = _("Sending the invitation failed.")
+            except InvitationException, ex:
+                error = unicode(ex)
     else:
         form = form_class(initial=request.GET)