Skip to content

Commit

Permalink
use sync_to_async
Browse files Browse the repository at this point in the history
  • Loading branch information
laggron42 committed Jan 14, 2025
1 parent f3d0d7e commit b96093c
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions admin_panel/admin_panel/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import TYPE_CHECKING, Literal

import aiohttp
from asgiref.sync import async_to_sync
from asgiref.sync import async_to_sync, sync_to_async
from bd_models.models import (
Ball,
BallInstance,
Expand Down Expand Up @@ -48,18 +48,18 @@ class Status(Enum):
OWNER = 4 # owns the application


def get_permissions(permissions: perm_dict) -> list[Permission]:
async def get_permissions(permissions: perm_dict) -> list[Permission]:
"""
Returns the list of permissions objects from a dictionnary mapping models to permission codes.
"""
result: list[Permission] = []
for model, perms in permissions.items():
content_type = ContentType.objects.get_for_model(model)
content_type = await sync_to_async(ContentType.objects.get_for_model)(model)
if perms == "*":
perms = ["add", "change", "delete", "view"]
for perm in perms:
result.append(
Permission.objects.get(
await Permission.objects.aget(
content_type=content_type, codename=f"{perm}_{model._meta.model_name}"
)
)
Expand Down Expand Up @@ -90,7 +90,7 @@ async def assign_status(request: "HttpRequest", response: dict, user: "User", st
Trade: ["view"],
TradeObject: ["view"],
}
await group.permissions.aadd(*get_permissions(perms))
await group.permissions.aadd(*await get_permissions(perms))
await user.groups.aadd(group)
message = "You were assigned the Staff status because of your Discord roles."
elif status == Status.ADMIN:
Expand All @@ -113,7 +113,7 @@ async def assign_status(request: "HttpRequest", response: dict, user: "User", st
Trade: ["view"],
TradeObject: ["view"],
}
await group.permissions.aadd(*get_permissions(perms))
await group.permissions.aadd(*await get_permissions(perms))
await user.groups.aadd(group)
message = "You were assigned the Admin status because of your Discord roles."
elif status == Status.TEAM_MEMBER:
Expand Down

0 comments on commit b96093c

Please sign in to comment.