From 3c8c3bd651c91e2f732b0ba92e14357665606445 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Thu, 17 Dec 2009 16:12:23 +0000 Subject: [PATCH] [soc2009/multidb] Updated contrib.auth User model for multi-db support. Patch from Russell Keith-Magee. git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/multidb@11890 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/auth/models.py | 12 +++++----- .../multiple_database/models.py | 6 +++++ .../multiple_database/tests.py | 24 ++++++++++++++++++- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py index b20a2caf17..66e3a110aa 100644 --- a/django/contrib/auth/models.py +++ b/django/contrib/auth/models.py @@ -3,7 +3,7 @@ import urllib 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 @@ class Group(models.Model): 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 @@ class UserManager(models.Manager): 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 @@ class User(models.Model): 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 diff --git a/tests/regressiontests/multiple_database/models.py b/tests/regressiontests/multiple_database/models.py index 289ed4512a..bd664c94e3 100644 --- a/tests/regressiontests/multiple_database/models.py +++ b/tests/regressiontests/multiple_database/models.py @@ -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 Author(models.Model): class Meta: ordering = ('name',) + +class UserProfile(models.Model): + user = models.OneToOneField(User) + flavor = models.CharField(max_length=100) + diff --git a/tests/regressiontests/multiple_database/tests.py b/tests/regressiontests/multiple_database/tests.py index b3ed077b50..1633674d07 100644 --- a/tests/regressiontests/multiple_database/tests.py +++ b/tests/regressiontests/multiple_database/tests.py @@ -2,10 +2,11 @@ import datetime 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,6 +591,27 @@ class QueryTestCase(TestCase): 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', 'alice@example.com') + bob = User.objects.create_user('bob', 'bob@example.com', 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