Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

django migration should respect SOCIAL_AUTH_USER_MODEL setting #936

Merged
merged 2 commits into from
Jul 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand All @@ -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)
),
]
12 changes: 11 additions & 1 deletion social/storage/django_orm.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down