mirror of
https://github.com/django/django.git
synced 2025-07-04 17:59:13 +00:00
[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
This commit is contained in:
parent
2a99b2ba5b
commit
3c8c3bd651
@ -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
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user