Skip to content

Commit

Permalink
Use the correct DB in post_migration handler
Browse files Browse the repository at this point in the history
Django provides a keyword argument called `using` to the `post_migrate`
signal handler. This argument tells the handler which database was
migrated.

Previously, the `create_anonymous_user()` function would always try to
use the default database to create the anonymous user. This change uses
the `using` parameter to make sure we use the database that actually
underwent the migration.

Closes: #630
  • Loading branch information
AdamZ authored and ad-m committed Aug 19, 2019
1 parent 4da5202 commit c6901c1
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions guardian/management/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ def create_anonymous_user(sender, **kwargs):
User = get_user_model()
try:
lookup = {User.USERNAME_FIELD: guardian_settings.ANONYMOUS_USER_NAME}
User.objects.get(**lookup)
User.objects.using(kwargs['using']).get(**lookup)
except User.DoesNotExist:
retrieve_anonymous_function = import_string(
guardian_settings.GET_INIT_ANONYMOUS_USER)
user = retrieve_anonymous_function(User)
user.save()
user.save(using=kwargs['using'])

# Only create an anonymous user if support is enabled.
if guardian_settings.ANONYMOUS_USER_NAME is not None:
Expand Down
2 changes: 1 addition & 1 deletion guardian/testapp/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
class CustomUserTests(TestCase):

def test_create_anonymous_user(self):
create_anonymous_user(object())
create_anonymous_user(object(), using='default')
self.assertEqual(1, User.objects.all().count())
anonymous = User.objects.all()[0]
self.assertEqual(anonymous.username, guardian_settings.ANONYMOUS_USER_NAME)
Expand Down
8 changes: 4 additions & 4 deletions guardian/testapp/tests/test_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ def test_uses_custom_function(self, guardian_settings):

anon = mocked_get_init_anon.return_value = mock.Mock()

create_anonymous_user('sender')
create_anonymous_user('sender', using='default')

mocked_get_init_anon.assert_called_once_with(User)

anon.save.assert_called_once_with()
anon.save.assert_called_once_with(using='default')

@mock.patch('guardian.management.guardian_settings')
@override_settings(AUTH_USER_MODEL='testapp.CustomUsernameUser')
Expand All @@ -37,9 +37,9 @@ def test_uses_custom_username_field_model(self, guardian_settings):
User = get_user_model()

anon = mocked_get_init_anon.return_value = mock.Mock()
create_anonymous_user('sender')
create_anonymous_user('sender', using='default')
mocked_get_init_anon.assert_called_once_with(User)
anon.save.assert_called_once_with()
anon.save.assert_called_once_with(using='default')

def test_get_anonymous_user(self):
anon = get_anonymous_user()
Expand Down

0 comments on commit c6901c1

Please sign in to comment.