Skip to content

Commit

Permalink
chore: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
BramMeir committed Mar 9, 2024
1 parent e7b93bf commit d6dd675
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
18 changes: 8 additions & 10 deletions backend/api/permissions/group_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@ def has_object_permission(self, request: Request, view: ViewSet, group) -> bool:
"""Check if user has permission to view a detailed group endpoint"""
user: User = request.user
course = group.project.course
teacher_or_assitant = is_teacher(user) and user.teacher.courses.filter(id=course.id).exists() or \
is_assistant(user) and user.assistant.courses.filter(id=course.id).exists()

if request.method in SAFE_METHODS:
# Users that are linked to the course can view the group.
return is_teacher(user) and user.teacher.courses.filter(id=course.id).exists() or \
is_assistant(user) and user.assistant.courses.filter(id=course.id).exists() or \
is_student(user) and user.student.courses.filter(id=course.id).exists()
return teacher_or_assitant or (is_student(user) and user.student.courses.filter(id=course.id).exists())

# We only allow teachers and assistants to modify specified groups.
return is_teacher(user) and user.teacher.courses.filter(id=course.id).exists() or \
is_assistant(user) and user.assistant.courses.filter(id=course.id).exists()
return teacher_or_assitant


class GroupStudentPermission(BasePermission):
Expand All @@ -42,18 +41,17 @@ class GroupStudentPermission(BasePermission):
def has_object_permission(self, request: Request, view: ViewSet, group) -> bool:
user: User = request.user
course = group.project.course
teacher_or_assitant = is_teacher(user) and user.teacher.courses.filter(id=course.id).exists() or \
is_assistant(user) and user.assistant.courses.filter(id=course.id).exists()

if request.method in SAFE_METHODS:
# Users related to the course can view the students of the group.
return is_teacher(user) and user.teacher.courses.filter(id=course.id).exists() or \
is_assistant(user) and user.assistant.courses.filter(id=course.id).exists() or \
is_student(user) and user.student.courses.filter(id=course.id).exists()
return teacher_or_assitant or (is_student(user) and user.student.courses.filter(id=course.id).exists())

# Students can only add and remove themselves from a group.
if is_student(user) and request.data.get("student_id") == user.id:
# Make sure the student is actually part of the course.
return user.student.courses.filter(id=course.id).exists()

# Teachers and assistants can add and remove any student from a group
return is_teacher(user) and user.teacher.courses.filter(id=course.id).exists() or \
is_assistant(user) and user.assistant.courses.filter(id=course.id).exists()
return teacher_or_assitant
18 changes: 8 additions & 10 deletions backend/api/permissions/project_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ def has_object_permission(self, request: Request, view: ViewSet, project) -> boo
"""Check if user has permission to view a detailed project endpoint"""
user: User = request.user
course = project.course
teacher_or_assistant = is_teacher(user) and user.teacher.courses.filter(id=course.id).exists() or \
is_assistant(user) and user.assistant.courses.filter(id=course.id).exists()

if request.method in SAFE_METHODS:
# Users that are linked to the course can view the project.
return is_teacher(user) and user.teacher.courses.filter(id=course.id).exists() or \
is_assistant(user) and user.assistant.courses.filter(id=course.id).exists() or \
is_student(user) and user.student.courses.filter(id=course.id).exists()
return teacher_or_assistant or (is_student(user) and user.student.courses.filter(id=course.id).exists())

# We only allow teachers and assistants to modify specified projects.
return is_teacher(user) and user.teacher.courses.filter(id=course.id).exists() or \
is_assistant(user) and user.assistant.courses.filter(id=course.id).exists()
return teacher_or_assistant


class ProjectGroupPermission(BasePermission):
Expand All @@ -43,13 +42,12 @@ class ProjectGroupPermission(BasePermission):
def has_object_permission(self, request: Request, view: ViewSet, project) -> bool:
user: User = request.user
course = project.course
teacher_or_assistant = is_teacher(user) and user.teacher.courses.filter(id=course.id).exists() or \
is_assistant(user) and user.assistant.courses.filter(id=course.id).exists()

if request.method in SAFE_METHODS:
# Users that are linked to the course can view the group.
return is_teacher(user) and user.teacher.courses.filter(id=course.id).exists() or \
is_assistant(user) and user.assistant.courses.filter(id=course.id).exists() or \
is_student(user) and user.student.courses.filter(id=course.id).exists()
return teacher_or_assistant or (is_student(user) and user.student.courses.filter(id=course.id).exists())

# We only allow teachers and assistants to create new groups.
return is_teacher(user) and user.teacher.courses.filter(id=course.id).exists() or \
is_assistant(user) and user.assistant.courses.filter(id=course.id).exists()
return teacher_or_assistant
1 change: 0 additions & 1 deletion backend/api/views/project_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def groups(self, request, **_):
def _create_groups(self, request, **_):
"""Create a number of groups for the project"""
project = self.get_object()
num_groups = request.data.get("number_groups")

serializer = TeacherCreateGroupSerializer(
data=request.data, context={"project": project}
Expand Down

0 comments on commit d6dd675

Please sign in to comment.