From 64be7f10bdf3dcddc00b46dbc4e27ea80ebbdd08 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Wed, 30 Dec 2020 10:17:01 +0200 Subject: [PATCH] Remove all of the needless async decorators. The code uses the django ORM which is sync, and fastapi handles sync paths just fine. So having all of this extra code for handling async was unnecessary. --- etebase_fastapi/routers/authentication.py | 20 +++++++++----------- etebase_fastapi/routers/collection.py | 18 ++++++++---------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/etebase_fastapi/routers/authentication.py b/etebase_fastapi/routers/authentication.py index efab7ef..be6a7e8 100644 --- a/etebase_fastapi/routers/authentication.py +++ b/etebase_fastapi/routers/authentication.py @@ -7,7 +7,6 @@ import nacl.encoding import nacl.hash import nacl.secret import nacl.signing -from asgiref.sync import sync_to_async from django.conf import settings from django.contrib.auth import user_logged_out, user_logged_in from django.core import exceptions as django_exceptions @@ -145,7 +144,6 @@ def save_changed_password(data: ChangePassword, user: UserType): user_info.save() -@sync_to_async def validate_login_request( validated_data: LoginResponse, challenge_sent_to_user: Authentication, @@ -193,13 +191,13 @@ def login_challenge(user: UserType = Depends(get_login_user)): @authentication_router.post("/login/", response_model=LoginOut) -async def login(data: Login, request: Request): - user = await sync_to_async(get_login_user)(request, LoginChallengeIn(username=data.response_data.username)) +def login(data: Login, request: Request): + user = get_login_user(request, LoginChallengeIn(username=data.response_data.username)) host = request.headers.get("Host") - await validate_login_request(data.response_data, data, user, "login", host) - data = await sync_to_async(LoginOut.from_orm)(user) - await sync_to_async(user_logged_in.send)(sender=user.__class__, request=None, user=user) - return data + validate_login_request(data.response_data, data, user, "login", host) + ret = LoginOut.from_orm(user) + user_logged_in.send(sender=user.__class__, request=None, user=user) + return ret @authentication_router.post("/logout/", status_code=status.HTTP_204_NO_CONTENT, responses=permission_responses) @@ -209,10 +207,10 @@ def logout(auth_data: AuthData = Depends(get_auth_data)): @authentication_router.post("/change_password/", status_code=status.HTTP_204_NO_CONTENT, responses=permission_responses) -async def change_password(data: ChangePassword, request: Request, user: UserType = Depends(get_authenticated_user)): +def change_password(data: ChangePassword, request: Request, user: UserType = Depends(get_authenticated_user)): host = request.headers.get("Host") - await validate_login_request(data.response_data, data, user, "changePassword", host) - await sync_to_async(save_changed_password)(data, user) + validate_login_request(data.response_data, data, user, "changePassword", host) + save_changed_password(data, user) @authentication_router.post("/dashboard_url/", responses=permission_responses) diff --git a/etebase_fastapi/routers/collection.py b/etebase_fastapi/routers/collection.py index e20e955..b716105 100644 --- a/etebase_fastapi/routers/collection.py +++ b/etebase_fastapi/routers/collection.py @@ -188,7 +188,6 @@ class ItemBatchIn(BaseModel): ) -@sync_to_async def collection_list_common( queryset: CollectionQuerySet, user: UserType, @@ -248,7 +247,7 @@ def has_write_access( response_model_exclude_unset=True, dependencies=PERMISSIONS_READ, ) -async def list_multi( +def list_multi( data: ListMulti, stoken: t.Optional[str] = None, limit: int = 50, @@ -261,18 +260,18 @@ async def list_multi( Q(members__collectionType__uid__in=data.collectionTypes) | Q(members__collectionType__isnull=True) ) - return await collection_list_common(queryset, user, stoken, limit, prefetch) + return collection_list_common(queryset, user, stoken, limit, prefetch) @collection_router.get("/", response_model=CollectionListResponse, dependencies=PERMISSIONS_READ) -async def collection_list( +def collection_list( stoken: t.Optional[str] = None, limit: int = 50, prefetch: Prefetch = PrefetchQuery, user: UserType = Depends(get_authenticated_user), queryset: CollectionQuerySet = Depends(get_collection_queryset), ): - return await collection_list_common(queryset, user, stoken, limit, prefetch) + return collection_list_common(queryset, user, stoken, limit, prefetch) def process_revisions_for_item(item: models.CollectionItem, revision_data: CollectionItemRevisionInOut): @@ -341,8 +340,8 @@ def _create(data: CollectionIn, user: UserType): @collection_router.post("/", status_code=status.HTTP_201_CREATED, dependencies=PERMISSIONS_READWRITE) -async def create(data: CollectionIn, user: UserType = Depends(get_authenticated_user)): - await sync_to_async(_create)(data, user) +def create(data: CollectionIn, user: UserType = Depends(get_authenticated_user)): + _create(data, user) @collection_router.get("/{collection_uid}/", response_model=CollectionOut, dependencies=PERMISSIONS_READ) @@ -407,7 +406,6 @@ def item_get( return CollectionItemOut.from_orm_context(obj, Context(user, prefetch)) -@sync_to_async def item_list_common( queryset: CollectionItemQuerySet, user: UserType, @@ -425,7 +423,7 @@ def item_list_common( @item_router.get("/item/", response_model=CollectionItemListResponse, dependencies=PERMISSIONS_READ) -async def item_list( +def item_list( queryset: CollectionItemQuerySet = Depends(get_item_queryset), stoken: t.Optional[str] = None, limit: int = 50, @@ -436,7 +434,7 @@ async def item_list( if not withCollection: queryset = queryset.filter(parent__isnull=True) - response = await item_list_common(queryset, user, stoken, limit, prefetch) + response = item_list_common(queryset, user, stoken, limit, prefetch) return response