From c9463cadbad200e5411627656b8ec3f61bc57caa Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Mon, 13 Jul 2020 16:20:46 +0300 Subject: [PATCH] Add support for a custom user creation function. --- django_etebase/app_settings.py | 7 +++++++ django_etebase/serializers.py | 7 +++++-- django_etebase/utils.py | 8 ++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/django_etebase/app_settings.py b/django_etebase/app_settings.py index b608717..3c659c8 100644 --- a/django_etebase/app_settings.py +++ b/django_etebase/app_settings.py @@ -54,6 +54,13 @@ class AppSettings: return self.import_from_str(get_user_queryset) 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 def CHALLENGE_VALID_SECONDS(self): # pylint: disable=invalid-name return self._setting("CHALLENGE_VALID_SECONDS", 60) diff --git a/django_etebase/serializers.py b/django_etebase/serializers.py index 29e1d4f..b3f99e0 100644 --- a/django_etebase/serializers.py +++ b/django_etebase/serializers.py @@ -20,7 +20,7 @@ from django.contrib.auth import get_user_model from django.db import transaction from rest_framework import serializers from . import models -from .utils import get_user_queryset +from .utils import get_user_queryset, create_user User = get_user_model() @@ -399,7 +399,10 @@ class AuthenticationSignupSerializer(serializers.Serializer): instance = user_queryset.get(**{User.USERNAME_FIELD: user_data['username'].lower()}) except User.DoesNotExist: # 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'): raise serializers.ValidationError('User already exists') diff --git a/django_etebase/utils.py b/django_etebase/utils.py index bce2877..08f81ae 100644 --- a/django_etebase/utils.py +++ b/django_etebase/utils.py @@ -10,3 +10,11 @@ def get_user_queryset(queryset, view): if custom_func is not None: return custom_func(queryset, view) 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)