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.
master
Tom Hacohen 4 years ago
parent 473448246f
commit 64be7f10bd

@ -7,7 +7,6 @@ import nacl.encoding
import nacl.hash import nacl.hash
import nacl.secret import nacl.secret
import nacl.signing import nacl.signing
from asgiref.sync import sync_to_async
from django.conf import settings from django.conf import settings
from django.contrib.auth import user_logged_out, user_logged_in from django.contrib.auth import user_logged_out, user_logged_in
from django.core import exceptions as django_exceptions from django.core import exceptions as django_exceptions
@ -145,7 +144,6 @@ def save_changed_password(data: ChangePassword, user: UserType):
user_info.save() user_info.save()
@sync_to_async
def validate_login_request( def validate_login_request(
validated_data: LoginResponse, validated_data: LoginResponse,
challenge_sent_to_user: Authentication, 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) @authentication_router.post("/login/", response_model=LoginOut)
async def login(data: Login, request: Request): def login(data: Login, request: Request):
user = await sync_to_async(get_login_user)(request, LoginChallengeIn(username=data.response_data.username)) user = get_login_user(request, LoginChallengeIn(username=data.response_data.username))
host = request.headers.get("Host") host = request.headers.get("Host")
await validate_login_request(data.response_data, data, user, "login", host) validate_login_request(data.response_data, data, user, "login", host)
data = await sync_to_async(LoginOut.from_orm)(user) ret = LoginOut.from_orm(user)
await sync_to_async(user_logged_in.send)(sender=user.__class__, request=None, user=user) user_logged_in.send(sender=user.__class__, request=None, user=user)
return data return ret
@authentication_router.post("/logout/", status_code=status.HTTP_204_NO_CONTENT, responses=permission_responses) @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) @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") host = request.headers.get("Host")
await validate_login_request(data.response_data, data, user, "changePassword", host) validate_login_request(data.response_data, data, user, "changePassword", host)
await sync_to_async(save_changed_password)(data, user) save_changed_password(data, user)
@authentication_router.post("/dashboard_url/", responses=permission_responses) @authentication_router.post("/dashboard_url/", responses=permission_responses)

@ -188,7 +188,6 @@ class ItemBatchIn(BaseModel):
) )
@sync_to_async
def collection_list_common( def collection_list_common(
queryset: CollectionQuerySet, queryset: CollectionQuerySet,
user: UserType, user: UserType,
@ -248,7 +247,7 @@ def has_write_access(
response_model_exclude_unset=True, response_model_exclude_unset=True,
dependencies=PERMISSIONS_READ, dependencies=PERMISSIONS_READ,
) )
async def list_multi( def list_multi(
data: ListMulti, data: ListMulti,
stoken: t.Optional[str] = None, stoken: t.Optional[str] = None,
limit: int = 50, limit: int = 50,
@ -261,18 +260,18 @@ async def list_multi(
Q(members__collectionType__uid__in=data.collectionTypes) | Q(members__collectionType__isnull=True) 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) @collection_router.get("/", response_model=CollectionListResponse, dependencies=PERMISSIONS_READ)
async def collection_list( def collection_list(
stoken: t.Optional[str] = None, stoken: t.Optional[str] = None,
limit: int = 50, limit: int = 50,
prefetch: Prefetch = PrefetchQuery, prefetch: Prefetch = PrefetchQuery,
user: UserType = Depends(get_authenticated_user), user: UserType = Depends(get_authenticated_user),
queryset: CollectionQuerySet = Depends(get_collection_queryset), 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): 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) @collection_router.post("/", status_code=status.HTTP_201_CREATED, dependencies=PERMISSIONS_READWRITE)
async def create(data: CollectionIn, user: UserType = Depends(get_authenticated_user)): def create(data: CollectionIn, user: UserType = Depends(get_authenticated_user)):
await sync_to_async(_create)(data, user) _create(data, user)
@collection_router.get("/{collection_uid}/", response_model=CollectionOut, dependencies=PERMISSIONS_READ) @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)) return CollectionItemOut.from_orm_context(obj, Context(user, prefetch))
@sync_to_async
def item_list_common( def item_list_common(
queryset: CollectionItemQuerySet, queryset: CollectionItemQuerySet,
user: UserType, user: UserType,
@ -425,7 +423,7 @@ def item_list_common(
@item_router.get("/item/", response_model=CollectionItemListResponse, dependencies=PERMISSIONS_READ) @item_router.get("/item/", response_model=CollectionItemListResponse, dependencies=PERMISSIONS_READ)
async def item_list( def item_list(
queryset: CollectionItemQuerySet = Depends(get_item_queryset), queryset: CollectionItemQuerySet = Depends(get_item_queryset),
stoken: t.Optional[str] = None, stoken: t.Optional[str] = None,
limit: int = 50, limit: int = 50,
@ -436,7 +434,7 @@ async def item_list(
if not withCollection: if not withCollection:
queryset = queryset.filter(parent__isnull=True) 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 return response

Loading…
Cancel
Save