Author: Alex
Date: 2009-12-17 10:12:23 -0600 (Thu, 17 Dec 2009)
New Revision: 11890

Modified:
   django/branches/soc2009/multidb/django/contrib/auth/models.py
   
django/branches/soc2009/multidb/tests/regressiontests/multiple_database/models.py
   
django/branches/soc2009/multidb/tests/regressiontests/multiple_database/tests.py
Log:
[soc2009/multidb] Updated contrib.auth User model for multi-db support.  Patch 
from Russell Keith-Magee.

Modified: django/branches/soc2009/multidb/django/contrib/auth/models.py
===================================================================
--- django/branches/soc2009/multidb/django/contrib/auth/models.py       
2009-12-17 16:12:06 UTC (rev 11889)
+++ django/branches/soc2009/multidb/django/contrib/auth/models.py       
2009-12-17 16:12:23 UTC (rev 11890)
@@ -3,7 +3,7 @@
 
 from django.contrib import auth
 from django.core.exceptions import ImproperlyConfigured
-from django.db import models
+from django.db import models, DEFAULT_DB_ALIAS
 from django.db.models.manager import EmptyManager
 from django.contrib.contenttypes.models import ContentType
 from django.utils.encoding import smart_str
@@ -106,7 +106,7 @@
         return self.name
 
 class UserManager(models.Manager):
-    def create_user(self, username, email, password=None):
+    def create_user(self, username, email, password=None, 
using=DEFAULT_DB_ALIAS):
         "Creates and saves a User with the given username, e-mail and 
password."
         now = datetime.datetime.now()
         user = self.model(None, username, '', '', email.strip().lower(), 
'placeholder', False, True, False, now, now)
@@ -114,15 +114,15 @@
             user.set_password(password)
         else:
             user.set_unusable_password()
-        user.save()
+        user.save(using=using)
         return user
 
-    def create_superuser(self, username, email, password):
+    def create_superuser(self, username, email, password, 
using=DEFAULT_DB_ALIAS):
         u = self.create_user(username, email, password)
         u.is_staff = True
         u.is_active = True
         u.is_superuser = True
-        u.save()
+        u.save(using=using)
         return u
 
     def make_random_password(self, length=10, 
allowed_chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789'):
@@ -319,7 +319,7 @@
             try:
                 app_label, model_name = settings.AUTH_PROFILE_MODULE.split('.')
                 model = models.get_model(app_label, model_name)
-                self._profile_cache = 
model._default_manager.get(user__id__exact=self.id)
+                self._profile_cache = 
model._default_manager.using(self._state.db).get(user__id__exact=self.id)
                 self._profile_cache.user = self
             except (ImportError, ImproperlyConfigured):
                 raise SiteProfileNotAvailable

Modified: 
django/branches/soc2009/multidb/tests/regressiontests/multiple_database/models.py
===================================================================
--- 
django/branches/soc2009/multidb/tests/regressiontests/multiple_database/models.py
   2009-12-17 16:12:06 UTC (rev 11889)
+++ 
django/branches/soc2009/multidb/tests/regressiontests/multiple_database/models.py
   2009-12-17 16:12:23 UTC (rev 11890)
@@ -1,4 +1,5 @@
 from django.conf import settings
+from django.contrib.auth.models import User
 from django.contrib.contenttypes.models import ContentType
 from django.contrib.contenttypes import generic
 from django.db import models, DEFAULT_DB_ALIAS
@@ -36,3 +37,8 @@
 
     class Meta:
         ordering = ('name',)
+
+class UserProfile(models.Model):
+    user = models.OneToOneField(User)
+    flavor = models.CharField(max_length=100)
+

Modified: 
django/branches/soc2009/multidb/tests/regressiontests/multiple_database/tests.py
===================================================================
--- 
django/branches/soc2009/multidb/tests/regressiontests/multiple_database/tests.py
    2009-12-17 16:12:06 UTC (rev 11889)
+++ 
django/branches/soc2009/multidb/tests/regressiontests/multiple_database/tests.py
    2009-12-17 16:12:23 UTC (rev 11890)
@@ -2,10 +2,11 @@
 import pickle
 
 from django.conf import settings
+from django.contrib.auth.models import User
 from django.db import connections
 from django.test import TestCase
 
-from models import Book, Author, Review
+from models import Book, Author, Review, UserProfile
 
 try:
     # we only have these models if the user is using multi-db, it's safe the
@@ -590,7 +591,28 @@
         
self.assertEquals(list(Review.objects.using('other').filter(object_id=dive.pk).values_list('source',flat=True)),
                           [u'Python Daily', u'Python Weekly'])
 
+class UserProfileTestCase(TestCase):
+    def setUp(self):
+        self.old_auth_profile_module = getattr(settings, 
'AUTH_PROFILE_MODULE', None)
+        settings.AUTH_PROFILE_MODULE = 'multiple_database.UserProfile'
 
+    def tearDown(self):
+        settings.AUTH_PROFILE_MODULE = self.old_auth_profile_module
+
+    def test_user_profiles(self):
+
+        alice = User.objects.create_user('alice', '[email protected]')
+        bob = User.objects.create_user('bob', '[email protected]', 
using='other')
+
+        alice_profile = UserProfile(user=alice, flavor='chocolate')
+        alice_profile.save()
+
+        bob_profile = UserProfile(user=bob, flavor='crunchy frog')
+        bob_profile.save()
+
+        self.assertEquals(alice.get_profile().flavor, 'chocolate')
+        self.assertEquals(bob.get_profile().flavor, 'crunchy frog')
+
 class FixtureTestCase(TestCase):
     multi_db = True
     fixtures = ['multidb-common', 'multidb']

--

You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.


Reply via email to