Skip to content

Commit

Permalink
Merge pull request #301 from bitdotioinc/boyd/user_model_default_manager
Browse files Browse the repository at this point in the history
  • Loading branch information
omab authored Aug 5, 2021
2 parents cafdafc + fbf316d commit a0c8a54
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 16 deletions.
12 changes: 6 additions & 6 deletions social_django/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def user_exists(cls, *args, **kwargs):
"""
if 'username' in kwargs:
kwargs[cls.username_field()] = kwargs.pop('username')
return cls.user_model().objects.filter(*args, **kwargs).exists()
return cls.user_model()._default_manager.filter(*args, **kwargs).exists()

@classmethod
def get_username(cls, user):
Expand All @@ -76,15 +76,15 @@ def create_user(cls, *args, **kwargs):
# stays undamaged by wrapping the create in an atomic.
using = router.db_for_write(cls.user_model())
with transaction.atomic(using=using):
user = cls.user_model().objects.create_user(*args, **kwargs)
user = cls.user_model()._default_manager.create_user(*args, **kwargs)
else:
user = cls.user_model().objects.create_user(*args, **kwargs)
user = cls.user_model()._default_manager.create_user(*args, **kwargs)
except IntegrityError as exc:
# If email comes in as None it won't get found in the get
if kwargs.get('email', True) is None:
kwargs['email'] = ''
try:
user = cls.user_model().objects.get(*args, **kwargs)
user = cls.user_model()._default_manager.get(*args, **kwargs)
except cls.user_model().DoesNotExist:
raise exc
return user
Expand All @@ -94,15 +94,15 @@ def get_user(cls, pk=None, **kwargs):
if pk:
kwargs = {'pk': pk}
try:
return cls.user_model().objects.get(**kwargs)
return cls.user_model()._default_manager.get(**kwargs)
except cls.user_model().DoesNotExist:
return None

@classmethod
def get_users_by_email(cls, email):
user_model = cls.user_model()
email_field = getattr(user_model, 'EMAIL_FIELD', 'email')
return user_model.objects.filter(**{email_field + '__iexact': email})
return user_model._default_manager.filter(**{email_field + '__iexact': email})

@classmethod
def get_social_auth(cls, provider, uid):
Expand Down
2 changes: 1 addition & 1 deletion social_django/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def from_session_value(self, val):
if isinstance(val, dict) and 'pk' in val and 'ctype' in val:
ctype = ContentType.objects.get_for_id(val['ctype'])
ModelClass = ctype.model_class()
val = ModelClass.objects.get(pk=val['pk'])
val = ModelClass._default_manager.get(pk=val['pk'])
return val

def get_language(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class SocialAdminTest(TestCase):
@classmethod
def setUpTestData(cls):
User = get_user_model()
User.objects.create_superuser(
User._default_manager.create_superuser(
username='admin', email='admin@test.com', first_name='Admin',
password='super-duper-test'
)
Expand Down
12 changes: 6 additions & 6 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ class TestSocialAuthUser(TestCase):
def test_user_relationship_none(self):
"""Accessing User.social_user outside of the pipeline doesn't work"""
User = get_user_model()
user = User.objects.create_user(username="randomtester")
user = User._default_manager.create_user(username="randomtester")
with self.assertRaises(AttributeError):
user.social_user

def test_user_existing_relationship(self):
"""Accessing User.social_user outside of the pipeline doesn't work"""
User = get_user_model()
user = User.objects.create_user(username="randomtester")
user = User._default_manager.create_user(username="randomtester")
UserSocialAuth.objects.create(user=user,
provider='my-provider',
uid='1234')
Expand All @@ -30,7 +30,7 @@ def test_user_existing_relationship(self):

def test_get_social_auth(self):
User = get_user_model()
user = User.objects.create_user(username="randomtester")
user = User._default_manager.create_user(username="randomtester")
user_social = UserSocialAuth.objects.create(user=user,
provider='my-provider',
uid='1234')
Expand Down Expand Up @@ -62,15 +62,15 @@ def test_cleanup(self):
class TestUserSocialAuth(TestCase):
def setUp(self):
self.user_model = get_user_model()
self.user = self.user_model.objects.create_user(
self.user = self.user_model._default_manager.create_user(
username='randomtester', email='user@example.com')
self.usa = UserSocialAuth.objects.create(
user=self.user, provider='my-provider', uid='1234')

def test_changed(self):
self.user.email = eml = 'test@example.com'
UserSocialAuth.changed(user=self.user)
db_eml = self.user_model.objects.get(username=self.user.username).email
db_eml = self.user_model._default_manager.get(username=self.user.username).email
self.assertEqual(db_eml, eml)

def test_set_extra_data(self):
Expand Down Expand Up @@ -117,7 +117,7 @@ def test_create_user_custom_username(self, *args):
@mock.patch('social_django.storage.transaction', spec=[])
def test_create_user_without_transaction_atomic(self, *args):
UserSocialAuth.create_user(username='test')
self.assertTrue(self.user_model.objects.filter(
self.assertTrue(self.user_model._default_manager.filter(
username='test').exists())

def test_get_user(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_random_string(self):

def test_session_value(self):
user_model = get_user_model()
user = user_model.objects.create_user(username="test")
user = user_model._default_manager.create_user(username="test")
ctype = ContentType.objects.get_for_model(user_model)

val = self.strategy.to_session_value(val=user)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_complete(self, mock_request):
@mock.patch('social_core.backends.base.BaseAuth.request')
def test_disconnect(self, mock_request):
user_model = get_user_model()
user = user_model.objects.create_user(username='test', password='pwd')
user = user_model._default_manager.create_user(username='test', password='pwd')
UserSocialAuth.objects.create(user=user, provider='facebook')
self.client.login(username='test', password='pwd')

Expand Down

0 comments on commit a0c8a54

Please sign in to comment.