Skip to content

Commit

Permalink
Use UserAttrsProxy as last resort in build_initial_user (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
apragacz committed Oct 30, 2023
1 parent 334f1ba commit f6cec0a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
17 changes: 15 additions & 2 deletions rest_registration/utils/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@
from django.db.models import Field, ForeignObjectRel

UserField = Union[Field[Any, Any], ForeignObjectRel, GenericForeignKey]
BaseUserAttrsProxy = AbstractBaseUser
else:
BaseUserAttrsProxy = object


class UserAttrsProxy(BaseUserAttrsProxy):

def __init__(self, **kwargs) -> None:
super().__init__()
self.__dict__.update(kwargs)


def authenticate_by_login_data(
Expand Down Expand Up @@ -187,8 +197,11 @@ def build_initial_user(data: Dict[str, Any]) -> 'AbstractBaseUser':
for field_name in user_field_names:
if field_name in data:
user_data[field_name] = data[field_name]
user_class = get_user_model()
return user_class(**user_data)
try:
user_class = get_user_model()
return user_class(**user_data)
except ValueError:
return UserAttrsProxy(**user_data)


def get_user_field_names(
Expand Down
1 change: 0 additions & 1 deletion tests/unit_tests/api/views/register/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ def test_ok(
assert_valid_register_verification_email(sent_email, user, timer)


@pytest.mark.skip("TODO: Issue #259")
@pytest.mark.django_db
@override_rest_registration_settings({
'REGISTER_SERIALIZER_CLASS': 'tests.testapps.custom_users.serializers.RegisterUserSerializer', # noqa: E501
Expand Down

0 comments on commit f6cec0a

Please sign in to comment.