Commit 8847bdc6 authored by Sharad Heft's avatar Sharad Heft

Refactor the group creation process.

parent 8cda1db4
......@@ -9,7 +9,6 @@ UserModel = get_user_model()
class TESTOAuth2Backend(ModelBackend):
@transaction.atomic
def authenticate(self, id=None, username=None, **profile):
if id is None or username is None:
return None
......@@ -19,10 +18,22 @@ class TESTOAuth2Backend(ModelBackend):
username=username
)
if getattr(settings, 'TEST_OAUTH_CREATE_GROUPS', False):
self._do_create_groups(user, profile.get('groups', []))
return user
def get_user(self, user_id):
try:
return UserModel.objects.get(pk=user_id)
except UserModel.DoesNotExist:
return None
@transaction.atomic
def _do_create_groups(self, user, groups):
user.groups.clear()
if getattr(settings, 'TEST_AUTH_CREATE_GROUPS', False):
for g in profile.get('groups', []):
for g in groups:
group, _ = Group.objects.get_or_create(
id=g['id'], name=g['name']
)
......@@ -34,11 +45,3 @@ class TESTOAuth2Backend(ModelBackend):
user.is_staff = True
user.save()
return user
def get_user(self, user_id):
try:
return UserModel.objects.get(pk=user_id)
except UserModel.DoesNotExist:
return None
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment