Skip to content
Snippets Groups Projects
Commit 5eb56aae authored by Hangzhi Yu's avatar Hangzhi Yu
Browse files

Refactor predicates

parent d524ad07
No related branches found
No related tags found
1 merge request!49Resolve "Add rules and permissions"
...@@ -16,7 +16,9 @@ def is_lesson_teacher(user: User, obj: LessonPeriod) -> bool: ...@@ -16,7 +16,9 @@ def is_lesson_teacher(user: User, obj: LessonPeriod) -> bool:
Checks whether the person linked to the user is a teacher Checks whether the person linked to the user is a teacher
in the lesson linked to the given LessonPeriod. in the lesson linked to the given LessonPeriod.
""" """
return user.person in obj.lesson.teachers.all() if hasattr(obj, "lesson"):
return user.person in obj.lesson.teachers.all()
return True
@predicate @predicate
...@@ -26,7 +28,9 @@ def is_lesson_participant(user: User, obj: LessonPeriod) -> bool: ...@@ -26,7 +28,9 @@ def is_lesson_participant(user: User, obj: LessonPeriod) -> bool:
Checks whether the person linked to the user is a member in Checks whether the person linked to the user is a member in
the groups linked to the given LessonPeriod. the groups linked to the given LessonPeriod.
""" """
return obj.lesson.groups.filter(members=user.person).exists() if hasattr(obj, "lesson"):
return obj.lesson.groups.filter(members=user.person).exists()
return True
@predicate @predicate
...@@ -37,7 +41,9 @@ def is_lesson_parent_group_owner(user: User, obj: LessonPeriod) -> bool: ...@@ -37,7 +41,9 @@ def is_lesson_parent_group_owner(user: User, obj: LessonPeriod) -> bool:
Checks whether the person linked to the user is the owner of Checks whether the person linked to the user is the owner of
any parent groups of any groups of the given LessonPeriods lesson. any parent groups of any groups of the given LessonPeriods lesson.
""" """
return obj.lesson.groups.filter(parent_groups__owners=user.person).exists() if hasattr(obj, "lesson"):
return obj.lesson.groups.filter(parent_groups__owners=user.person).exists()
return True
@predicate @predicate
...@@ -106,9 +112,11 @@ def has_lesson_group_object_perm(perm: str): ...@@ -106,9 +112,11 @@ def has_lesson_group_object_perm(perm: str):
@predicate(name) @predicate(name)
def fn(user: User, obj: LessonPeriod) -> bool: def fn(user: User, obj: LessonPeriod) -> bool:
for group in obj.lesson.groups.all(): if hasattr(obj, "lesson"):
if check_object_permission(user, perm, group): for group in obj.lesson.groups.all():
return True if check_object_permission(user, perm, group):
return False return True
return False
return True
return fn return fn
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment