Commits

Matías Pizarro committed dee653c

Takes into account django 1.5 custom user models

Comments (0)

Files changed (6)

registration/backends/simple/__init__.py

 from django.conf import settings
 from django.contrib.auth import authenticate
 from django.contrib.auth import login
-from django.contrib.auth.models import User
+try:
+    from django.contrib.auth import get_user_model
+    UserModel = get_user_model()
+except ImportError:
+    # for django <= 1.4
+    from django.contrib.auth.models import User as UserModel
 
 from registration import signals
 from registration.forms import RegistrationForm
         
         """
         username, email, password = kwargs['username'], kwargs['email'], kwargs['password1']
-        User.objects.create_user(username, email, password)
+        UserModel.objects.create_user(username, email, password)
         
         # authenticate() always has to be called before login(), and
         # will return the user we just created.

registration/forms.py

 """
 
 
-from django.contrib.auth.models import User
+try:
+    from django.contrib.auth import get_user_model
+    UserModel = get_user_model()
+except ImportError:
+    # for django <= 1.4
+    from django.contrib.auth.models import User as UserModel
+
 from django import forms
 from django.utils.translation import ugettext_lazy as _
 
                                 label=_("Password"))
     password2 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
                                 label=_("Password (again)"))
-    
+
     def clean_username(self):
         """
         Validate that the username is alphanumeric and is not already
         in use.
         
         """
-        existing = User.objects.filter(username__iexact=self.cleaned_data['username'])
+        existing = UserModel.objects.filter(username__iexact=self.cleaned_data['username'])
         if existing.exists():
             raise forms.ValidationError(_("A user with that username already exists."))
         else:
         site.
         
         """
-        if User.objects.filter(email__iexact=self.cleaned_data['email']):
+        if UserModel.objects.filter(email__iexact=self.cleaned_data['email']):
             raise forms.ValidationError(_("This email address is already in use. Please supply a different email address."))
         return self.cleaned_data['email']
 

registration/models.py

 import re
 
 from django.conf import settings
-from django.contrib.auth.models import User
+try:
+    from django.contrib.auth import get_user_model
+    UserModel = get_user_model()
+except ImportError:
+    # for django <= 1.4
+    from django.contrib.auth.models import User as UserModel
+
 from django.db import models
 from django.db import transaction
 from django.template.loader import render_to_string
         user. To disable this, pass ``send_email=False``.
         
         """
-        new_user = User.objects.create_user(username, email, password)
+        new_user = UserModel.objects.create_user(username, email, password)
         new_user.is_active = False
         new_user.save()
 
                     if not user.is_active:
                         user.delete()
                         profile.delete()
-            except User.DoesNotExist:
+            except UserModel.DoesNotExist:
                 profile.delete()
 
 class RegistrationProfile(models.Model):
     """
     ACTIVATED = u"ALREADY_ACTIVATED"
     
-    user = models.ForeignKey(User, unique=True, verbose_name=_('user'))
+    user = models.ForeignKey(UserModel, unique=True, verbose_name=_('user'))
     activation_key = models.CharField(_('activation key'), max_length=40)
     
     objects = RegistrationManager()

registration/tests/backends.py

 
 from django.conf import settings
 from django.contrib import admin
-from django.contrib.auth.models import User
+try:
+    from django.contrib.auth import get_user_model
+    UserModel = get_user_model()
+except ImportError:
+    # for django <= 1.4
+    from django.contrib.auth.models import User as UserModel
+
 from django.contrib.sessions.middleware import SessionMiddleware
 from django.contrib.sites.models import Site
 from django.core import mail
         pattern ``registration_complete``.
 
         """
-        self.assertEqual(self.backend.post_registration_redirect(_mock_request(), User()),
+        self.assertEqual(self.backend.post_registration_redirect(_mock_request(), UserModel()),
                          ('registration_complete', (), {}))
 
     def test_registration_signal(self):
 
         admin_class.activate_users(_mock_request(),
                                    RegistrationProfile.objects.all())
-        self.failUnless(User.objects.get(username='alice').is_active)
+        self.failUnless(UserModel.objects.get(username='alice').is_active)
 
 
 class SimpleRegistrationBackendTests(TestCase):
         
         """
         self.assertRaises(NotImplementedError, self.backend.post_activation_redirect,
-                          request=_mock_request(), user=User())
+                          request=_mock_request(), user=UserModel())

registration/tests/models.py

 import re
 
 from django.conf import settings
-from django.contrib.auth.models import User
+try:
+    from django.contrib.auth import get_user_model
+    UserModel = get_user_model()
+except ImportError:
+    # for django <= 1.4
+    from django.contrib.auth.models import User as UserModel
+
 from django.contrib.sites.models import Site
 from django.core import mail
 from django.core import management
         activation key.
         
         """
-        new_user = User.objects.create_user(**self.user_info)
+        new_user = UserModel.objects.create_user(**self.user_info)
         profile = RegistrationProfile.objects.create_profile(new_user)
 
         self.assertEqual(RegistrationProfile.objects.count(), 1)
         email.
         
         """
-        new_user = User.objects.create_user(**self.user_info)
+        new_user = UserModel.objects.create_user(**self.user_info)
         profile = RegistrationProfile.objects.create_profile(new_user)
         profile.send_activation_email(Site.objects.get_current())
         self.assertEqual(len(mail.outbox), 1)
         profile = RegistrationProfile.objects.get(user=new_user)
         activated = RegistrationProfile.objects.activate_user(profile.activation_key)
 
-        self.failUnless(isinstance(activated, User))
+        self.failUnless(isinstance(activated, UserModel))
         self.assertEqual(activated.id, new_user.id)
         self.failUnless(activated.is_active)
 
         profile = RegistrationProfile.objects.get(user=new_user)
         activated = RegistrationProfile.objects.activate_user(profile.activation_key)
 
-        self.failIf(isinstance(activated, User))
+        self.failIf(isinstance(activated, UserModel))
         self.failIf(activated)
 
-        new_user = User.objects.get(username='alice')
+        new_user = UserModel.objects.get(username='alice')
         self.failIf(new_user.is_active)
 
         profile = RegistrationProfile.objects.get(user=new_user)
 
         RegistrationProfile.objects.delete_expired_users()
         self.assertEqual(RegistrationProfile.objects.count(), 1)
-        self.assertRaises(User.DoesNotExist, User.objects.get, username='bob')
+        self.assertRaises(UserModel.DoesNotExist, UserModel.objects.get, username='bob')
 
     def test_management_command(self):
         """
 
         management.call_command('cleanupregistration')
         self.assertEqual(RegistrationProfile.objects.count(), 1)
-        self.assertRaises(User.DoesNotExist, User.objects.get, username='bob')
+        self.assertRaises(UserModel.DoesNotExist, UserModel.objects.get, username='bob')

registration/tests/views.py

 import datetime
 
 from django.conf import settings
-from django.contrib.auth.models import User
+try:
+    from django.contrib.auth import get_user_model
+    UserModel = get_user_model()
+except ImportError:
+    # for django <= 1.4
+    from django.contrib.auth.models import User as UserModel
+
 from django.core import mail
 from django.core.urlresolvers import reverse
 from django.test import TestCase
         response = self.client.get(reverse('registration_activate',
                                            kwargs={'activation_key': profile.activation_key}))
         self.assertRedirects(response, success_redirect)
-        self.failUnless(User.objects.get(username='alice').is_active)
+        self.failUnless(UserModel.objects.get(username='alice').is_active)
 
     def test_invalid_activation(self):
         """
                                'email': 'bob@example.com',
                                'password1': 'secret',
                                'password2': 'secret'})
-        expired_user = User.objects.get(username='bob')
+        expired_user = UserModel.objects.get(username='bob')
         expired_user.date_joined = expired_user.date_joined - datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS)
         expired_user.save()
 
         self.assertEqual(response.status_code, 200)
         self.assertEqual(response.context['activation_key'],
                          expired_profile.activation_key)
-        self.failIf(User.objects.get(username='bob').is_active)
+        self.failIf(UserModel.objects.get(username='bob').is_active)
 
     def test_activation_success_url(self):
         """
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.