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

Add atomic transaction around social_auth creation. #770

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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