From ec8c69b3f3ff0c12a31dc941722c86d7ace6d36e Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Sun, 27 Dec 2020 20:36:11 +0200 Subject: [PATCH] Fix a few FIXMEs. --- etebase_fastapi/authentication.py | 21 +++++++++++---------- etebase_fastapi/test_reset_view.py | 6 +++--- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/etebase_fastapi/authentication.py b/etebase_fastapi/authentication.py index e355aa4..a8fbbfe 100644 --- a/etebase_fastapi/authentication.py +++ b/etebase_fastapi/authentication.py @@ -24,7 +24,7 @@ from django_etebase.models import UserInfo from django_etebase.signals import user_signed_up from django_etebase.token_auth.models import AuthToken from django_etebase.token_auth.models import get_default_expiry -from django_etebase.utils import create_user +from django_etebase.utils import create_user, get_user_queryset, CallbackContext from django_etebase.views import msgpack_encode, msgpack_decode from .exceptions import AuthenticationFailed, transform_validation_error, ValidationError from .msgpack import MsgpackResponse, MsgpackRoute @@ -268,20 +268,21 @@ async def change_password(data: ChangePassword, request: Request, user: User = D return Response(status_code=status.HTTP_204_NO_CONTENT) -def signup_save(data: SignupIn) -> User: +def signup_save(data: SignupIn, request: Request) -> User: user_data = data.user with transaction.atomic(): try: - # XXX-TOM - # view = self.context.get("view", None) - # user_queryset = get_user_queryset(User.objects.all(), view) - user_queryset = User.objects.all() + user_queryset = get_user_queryset(User.objects.all(), CallbackContext(request.path_params)) 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 try: - # XXX-TOM - instance = create_user(**user_data.dict(), password=None, first_name=user_data.username, view=None) + instance = create_user( + **user_data.dict(), + password=None, + first_name=user_data.username, + context=CallbackContext(request.path_params), + ) instance.full_clean() except EtebaseValidationError as e: raise e @@ -298,8 +299,8 @@ def signup_save(data: SignupIn) -> User: @authentication_router.post("/signup/") -async def signup(data: SignupIn): - user = await sync_to_async(signup_save)(data) +async def signup(data: SignupIn, request: Request): + user = await sync_to_async(signup_save)(data, request) # XXX-TOM data = await sync_to_async(LoginOut.from_orm)(user) await sync_to_async(user_signed_up.send)(sender=user.__class__, request=None, user=user) diff --git a/etebase_fastapi/test_reset_view.py b/etebase_fastapi/test_reset_view.py index ea7d8d9..435a56e 100644 --- a/etebase_fastapi/test_reset_view.py +++ b/etebase_fastapi/test_reset_view.py @@ -2,7 +2,7 @@ from django.conf import settings from django.contrib.auth import get_user_model from django.db import transaction from django.shortcuts import get_object_or_404 -from fastapi import APIRouter, Response, status +from fastapi import APIRouter, Request, Response, status from django_etebase.utils import get_user_queryset from etebase_fastapi.authentication import SignupIn, signup_save @@ -13,7 +13,7 @@ User = get_user_model() @test_reset_view_router.post("/reset/") -def reset(data: SignupIn): +def reset(data: SignupIn, request: Request): # Only run when in DEBUG mode! It's only used for tests if not settings.DEBUG: return Response("Only allowed in debug mode.", status_code=status.HTTP_400_BAD_REQUEST) @@ -28,7 +28,7 @@ def reset(data: SignupIn): if hasattr(user, "userinfo"): user.userinfo.delete() - signup_save(data) + signup_save(data, request) # Delete all of the journal data for this user for a clear test env user.collection_set.all().delete() user.collectionmember_set.all().delete()