Skip to content
Snippets Groups Projects

Performance optimizations

Merged Jonathan Weth requested to merge feature/performance-optimizations into master
Compare and Show latest version
5 files
+ 68
20
Compare changes
  • Side-by-side
  • Inline
Files
5
from typing import Union
from django.contrib.auth.models import User, Permission
from django.contrib.contenttypes.models import ContentType
from guardian.models import UserObjectPermission
from django.contrib.auth.models import Permission, User
from guardian.models import UserObjectPermission
from guardian.shortcuts import get_objects_for_user
from rules import predicate
from aleksis.apps.chronos.models import LessonPeriod
from aleksis.core.models import Group, Person
from aleksis.core.util.core_helpers import get_site_preferences
from aleksis.core.util.predicates import check_object_permission
from aleksis.core.util.core_helpers import get_content_type_by_perm, get_site_preferences
from ..models import PersonalNote
@@ -38,7 +36,10 @@ def is_lesson_participant(user: User, obj: LessonPeriod) -> bool:
the groups linked to the given LessonPeriod.
"""
if hasattr(obj, "lesson"):
return obj.lesson.groups.filter(members=user.person).exists()
for group in obj.lesson.groups.all():
if user.person in list(group.members.all()):
return True
return False
return True
@@ -51,7 +52,11 @@ def is_lesson_parent_group_owner(user: User, obj: LessonPeriod) -> bool:
any parent groups of any groups of the given LessonPeriods lesson.
"""
if hasattr(obj, "lesson"):
return obj.lesson.groups.filter(parent_groups__owners=user.person).exists()
for group in obj.lesson.groups.all():
for parent_group in group.parent_groups.all():
if user.person in list(parent_group.owners.all()):
return True
return False
return True
@@ -63,7 +68,7 @@ def is_group_owner(user: User, obj: Union[Group, Person]) -> bool:
If there isn't provided a group, it will return `False`.
"""
if isinstance(obj, Group):
if obj.owners.filter(pk=user.person.pk).exists():
if user.person in obj.owners.all():
return True
return False
@@ -102,13 +107,18 @@ def has_person_group_object_perm(perm: str):
"""
name = f"has_person_group_object_perm:{perm}"
ct = ContentType.objects.get_for_model(Group)
ct = get_content_type_by_perm(perm)
permissions = Permission.objects.filter(content_type=ct, codename=perm)
@predicate(name)
def fn(user: User, obj: Person) -> bool:
groups = obj.member_of.all()
qs = UserObjectPermission.objects.filter(object_pk__in=groups.values_list("pk", flat=True), content_type=ct, user=user, permission__in=permissions)
qs = UserObjectPermission.objects.filter(
object_pk__in=list(groups.values_list("pk", flat=True)),
content_type=ct,
user=user,
permission__in=permissions,
)
return qs.exists()
return fn
@@ -135,14 +145,19 @@ def has_lesson_group_object_perm(perm: str):
"""
name = f"has_lesson_group_object_perm:{perm}"
ct = ContentType.objects.get_for_model(Group)
ct = get_content_type_by_perm(perm)
permissions = Permission.objects.filter(content_type=ct, codename=perm)
@predicate(name)
def fn(user: User, obj: LessonPeriod) -> bool:
if hasattr(obj, "lesson"):
groups = obj.lesson.groups.all()
qs = UserObjectPermission.objects.filter(object_pk__in=groups.values_list("pk", flat=True), content_type=ct, user=user, permission__in=permissions)
qs = UserObjectPermission.objects.filter(
object_pk__in=list(groups.values_list("pk", flat=True)),
content_type=ct,
user=user,
permission__in=permissions,
)
return qs.exists()
return False
@@ -156,14 +171,19 @@ def has_personal_note_group_perm(perm: str):
"""
name = f"has_personal_note_person_or_group_perm:{perm}"
ct = ContentType.objects.get_for_model(Person)
ct = get_content_type_by_perm(perm)
permissions = Permission.objects.filter(content_type=ct, codename=perm)
@predicate(name)
def fn(user: User, obj: PersonalNote) -> bool:
if hasattr(obj, "person"):
groups = obj.person.member_of.all()
qs = UserObjectPermission.objects.filter(object_pk__in=groups.values_list("pk", flat=True), content_type=ct, user=user, permission__in=permissions)
qs = UserObjectPermission.objects.filter(
object_pk__in=list(groups.values_list("pk", flat=True)),
content_type=ct,
user=user,
permission__in=permissions,
)
return qs.exists()
return False
Loading