Skip to content

Commit

Permalink
Add atomic transaction around social_auth creation
Browse files Browse the repository at this point in the history
  • Loading branch information
max-arnold committed Jul 14, 2016
1 parent 7b8484c commit 9c4ecae
Showing 1 changed file with 11 additions and 1 deletion.
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

0 comments on commit 9c4ecae

Please sign in to comment.