[Python-modules-commits] [python-social-auth] 93/131: Added exception handling for user creation race condition in Django

Wolfgang Borgert debacle at moszumanska.debian.org
Sat Dec 24 15:17:07 UTC 2016


This is an automated email from the git hooks/post-receive script.

debacle pushed a commit to tag v0.2.20
in repository python-social-auth.

commit 9f86059e9d8070bc5ecd7ba069fadab1c9bf502a
Author: Carson Gee <x at carsongee.com>
Date:   Mon Aug 1 17:49:20 2016 -0400

    Added exception handling for user creation race condition in Django
---
 social/storage/django_orm.py | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/social/storage/django_orm.py b/social/storage/django_orm.py
index 637dd23..e331656 100644
--- a/social/storage/django_orm.py
+++ b/social/storage/django_orm.py
@@ -1,7 +1,9 @@
 """Django ORM models for Social Auth"""
 import base64
 import six
+import sys
 from django.db import transaction
+from django.db.utils import IntegrityError
 
 from social.storage.base import UserMixin, AssociationMixin, NonceMixin, \
                                 CodeMixin, BaseStorage
@@ -58,7 +60,20 @@ class DjangoUserMixin(UserMixin):
         username_field = cls.username_field()
         if 'username' in kwargs and username_field not in kwargs:
             kwargs[username_field] = kwargs.pop('username')
-        return cls.user_model().objects.create_user(*args, **kwargs)
+        try:
+            user = cls.user_model().objects.create_user(*args, **kwargs)
+        except IntegrityError:
+            # User might have been created on a different thread, try and find them.
+            # If we don't, re-raise the IntegrityError.
+            exc_info = sys.exc_info()
+            # If email comes in as None it won't get found in the get
+            if kwargs.get('email', True) is None:
+                kwargs['email'] = ''
+            try:
+                user = cls.user_model().objects.get(*args, **kwargs)
+            except cls.user_model().DoesNotExist:
+                six.reraise(*exc_info)
+        return user
 
     @classmethod
     def get_user(cls, pk=None, **kwargs):

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-social-auth.git



More information about the Python-modules-commits mailing list