From d9b7b5f029f31bf6c037839483d099fd5ef34660 Mon Sep 17 00:00:00 2001 From: Max Arnold Date: Fri, 17 Jun 2016 11:26:00 +0700 Subject: [PATCH 1/2] django migration should respect SOCIAL_AUTH_USER_MODEL setting --- .../default/migrations/0002_add_related_name.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/social/apps/django_app/default/migrations/0002_add_related_name.py b/social/apps/django_app/default/migrations/0002_add_related_name.py index 8e39f15bf..848a64af1 100644 --- a/social/apps/django_app/default/migrations/0002_add_related_name.py +++ b/social/apps/django_app/default/migrations/0002_add_related_name.py @@ -4,6 +4,12 @@ from django.db import models, migrations from django.conf import settings +from social.utils import setting_name + +USER_MODEL = getattr(settings, setting_name('USER_MODEL'), None) or \ + getattr(settings, 'AUTH_USER_MODEL', None) or \ + 'auth.User' + class Migration(migrations.Migration): @@ -15,6 +21,6 @@ class Migration(migrations.Migration): migrations.AlterField( model_name='usersocialauth', name='user', - field=models.ForeignKey(related_name='social_auth', to=settings.AUTH_USER_MODEL) + field=models.ForeignKey(related_name='social_auth', to=USER_MODEL) ), ] From ef5394f048304cd139da4da03e43bc1d554d309f Mon Sep 17 00:00:00 2001 From: Max Arnold Date: Thu, 14 Jul 2016 11:30:38 +0700 Subject: [PATCH 2/2] Add atomic transaction around social_auth creation Cherry-pick of https://github.com/omab/python-social-auth/pull/770 --- social/storage/django_orm.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/social/storage/django_orm.py b/social/storage/django_orm.py index 8d9e67246..637dd23a2 100644 --- a/social/storage/django_orm.py +++ b/social/storage/django_orm.py @@ -1,6 +1,7 @@ """Django ORM models for Social Auth""" import base64 import six +from django.db import transaction from social.storage.base import UserMixin, AssociationMixin, NonceMixin, \ CodeMixin, BaseStorage @@ -96,7 +97,16 @@ def get_social_auth_for_user(cls, user, provider=None, id=None): def create_social_auth(cls, user, uid, provider): if not isinstance(uid, six.string_types): uid = str(uid) - return cls.objects.create(user=user, uid=uid, provider=provider) + if hasattr(transaction, 'atomic'): + # In Django versions that have an "atomic" transaction decorator / context + # manager, there's a transaction wrapped around this call. + # If the create fails below due to an IntegrityError, ensure that the transaction + # stays undamaged by wrapping the create in an atomic. + with transaction.atomic(): + social_auth = cls.objects.create(user=user, uid=uid, provider=provider) + else: + social_auth = cls.objects.create(user=user, uid=uid, provider=provider) + return social_auth class DjangoNonceMixin(NonceMixin):