Add support for a custom user creation function.

master
Tom Hacohen 5 years ago
parent e41f8455f2
commit c9463cadba

@ -54,6 +54,13 @@ class AppSettings:
return self.import_from_str(get_user_queryset) return self.import_from_str(get_user_queryset)
return None return None
@cached_property
def CREATE_USER_FUNC(self): # pylint: disable=invalid-name
func = self._setting("CREATE_USER_FUNC", None)
if func is not None:
return self.import_from_str(func)
return None
@cached_property @cached_property
def CHALLENGE_VALID_SECONDS(self): # pylint: disable=invalid-name def CHALLENGE_VALID_SECONDS(self): # pylint: disable=invalid-name
return self._setting("CHALLENGE_VALID_SECONDS", 60) return self._setting("CHALLENGE_VALID_SECONDS", 60)

@ -20,7 +20,7 @@ from django.contrib.auth import get_user_model
from django.db import transaction from django.db import transaction
from rest_framework import serializers from rest_framework import serializers
from . import models from . import models
from .utils import get_user_queryset from .utils import get_user_queryset, create_user
User = get_user_model() User = get_user_model()
@ -399,7 +399,10 @@ class AuthenticationSignupSerializer(serializers.Serializer):
instance = user_queryset.get(**{User.USERNAME_FIELD: user_data['username'].lower()}) instance = user_queryset.get(**{User.USERNAME_FIELD: user_data['username'].lower()})
except User.DoesNotExist: except User.DoesNotExist:
# Create the user and save the casing the user chose as the first name # Create the user and save the casing the user chose as the first name
instance = User.objects.create_user(**user_data, password=None, first_name=user_data['username']) try:
instance = create_user(**user_data, password=None, first_name=user_data['username'], view=view)
except Exception as e:
raise serializers.ValidationError(e)
if hasattr(instance, 'userinfo'): if hasattr(instance, 'userinfo'):
raise serializers.ValidationError('User already exists') raise serializers.ValidationError('User already exists')

@ -10,3 +10,11 @@ def get_user_queryset(queryset, view):
if custom_func is not None: if custom_func is not None:
return custom_func(queryset, view) return custom_func(queryset, view)
return queryset return queryset
def create_user(*args, **kwargs):
custom_func = app_settings.CREATE_USER_FUNC
if custom_func is not None:
return custom_func(*args, **kwargs)
_ = kwargs.pop('view')
return User.objects.create_user(*args, **kwargs)

Loading…
Cancel
Save