Disambiguation in kfet's permission handling

In some places we used to refer to permissions based on their codename
only (the part after the dot "." in the following examples) which can be
ambiguous. Typically, we might define permissions like "bds.is_team" or
"cof.is_team" in the near future ;)
This commit is contained in:
Martin Pépin 2019-12-21 16:26:59 +01:00
parent 67e28c704f
commit 64c792b11f
No known key found for this signature in database
GPG key ID: E7520278B1774448
5 changed files with 37 additions and 25 deletions

View file

@ -3,6 +3,7 @@ import heapq
import statistics
from collections import defaultdict
from decimal import Decimal
from typing import List
from urllib.parse import urlencode
from django.contrib import messages
@ -993,15 +994,19 @@ def kpsul_update_addcost(request):
return JsonResponse(data)
def get_missing_perms(required_perms, user):
missing_perms_codenames = [
(perm.split("."))[1] for perm in required_perms if not user.has_perm(perm)
]
missing_perms = list(
Permission.objects.filter(codename__in=missing_perms_codenames).values_list(
"name", flat=True
def get_missing_perms(required_perms: List[str], user: User) -> List[str]:
def get_perm_description(app_label: str, codename: str) -> str:
name = Permission.objects.values_list("name", flat=True).get(
codename=codename, content_type__app_label=app_label
)
)
return "[{}] {}".format(app_label, name)
missing_perms = [
get_perm_description(*perm.split("."))
for perm in required_perms
if not user.has_perm(perm)
]
return missing_perms