2018-12-18 12:24:26 +00:00
|
|
|
"""OAuth Client User Creation Utils"""
|
2018-11-11 12:41:48 +00:00
|
|
|
|
|
|
|
from django.db.utils import IntegrityError
|
|
|
|
|
2019-02-27 12:18:16 +00:00
|
|
|
from passbook.core.models import User
|
2018-11-11 12:41:48 +00:00
|
|
|
|
2019-02-27 12:18:16 +00:00
|
|
|
|
|
|
|
def user_get_or_create(**kwargs):
|
2018-11-11 12:41:48 +00:00
|
|
|
"""Create user or return existing user"""
|
|
|
|
try:
|
2019-02-27 12:18:16 +00:00
|
|
|
new_user = User.objects.create_user(**kwargs)
|
2018-11-11 12:41:48 +00:00
|
|
|
except IntegrityError:
|
2019-02-27 12:18:16 +00:00
|
|
|
# At this point we've already checked that there is no existing connection
|
|
|
|
# to any user. Hence if we can't create the user,
|
|
|
|
kwargs['username'] = '%s_1' % kwargs['username']
|
|
|
|
new_user = User.objects.create_user(**kwargs)
|
2018-11-11 12:41:48 +00:00
|
|
|
return new_user
|