Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
users changed
Browse files Browse the repository at this point in the history
  • Loading branch information
ReinhardDP committed Mar 11, 2024
1 parent 2d00aab commit a0f5a92
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 122 deletions.
30 changes: 15 additions & 15 deletions backend/pigeonhole/apps/courses/permissions.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
from rest_framework import permissions
from backend.pigeonhole.apps.users.models import Student, Teacher
from backend.pigeonhole.apps.users.models import User


class CourseUserPermissions(permissions.BasePermission):
def has_permission(self, request, view):
if request.user.is_superuser:
return True
if isinstance(request.user, Teacher):
return True

if isinstance(request.user, Student):
return view.action in ['list', 'retrieve']
# if isinstance(request.user, Teacher):
# return True
#
# if isinstance(request.user, Student):
# return view.action in ['list', 'retrieve']

return False

def has_object_permission(self, request, view, obj):
if request.user.is_superuser:
return True
if isinstance(request.user, Teacher):
if request.user.is_admin:
return True
elif Teacher.objects.filter(id=request.user.id, course=obj).exists():
return True
return view.action in ['list', 'retrieve']

if isinstance(request.user, Student):
return view.action in ['list', 'retrieve']
# if isinstance(request.user, Teacher):
# if request.user.is_admin:
# return True
# elif Teacher.objects.filter(id=request.user.id, course=obj).exists():
# return True
# return view.action in ['list', 'retrieve']
#
# if isinstance(request.user, Student):
# return view.action in ['list', 'retrieve']

return False
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 5.0.2 on 2024-03-11 13:17

from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('groups', '0004_alter_group_final_score'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.RemoveField(
model_name='group',
name='student',
),
migrations.AddField(
model_name='group',
name='user',
field=models.ManyToManyField(to=settings.AUTH_USER_MODEL),
),
]
6 changes: 3 additions & 3 deletions backend/pigeonhole/apps/groups/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
from rest_framework import serializers

from backend.pigeonhole.apps.projects.models import Project
from backend.pigeonhole.apps.users.models import Student
from backend.pigeonhole.apps.users.models import User


class Group(models.Model):
group_id = models.BigAutoField(primary_key=True)
group_nr = models.IntegerField(blank=True, null=True)
project_id = models.ForeignKey(Project, on_delete=models.CASCADE)
student = models.ManyToManyField(Student)
user = models.ManyToManyField(User)
feedback = models.TextField(null=True)
final_score = models.IntegerField(null=True, blank=True)

Expand All @@ -28,4 +28,4 @@ def save(self, *args, **kwargs):
class GroupSerializer(serializers.ModelSerializer):
class Meta:
model = Group
fields = ["group_id", "group_nr", "final_score", "project_id", "student", "feedback"]
fields = ["group_id", "group_nr", "final_score", "project_id", "user", "feedback"]
24 changes: 12 additions & 12 deletions backend/pigeonhole/apps/projects/permissions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from rest_framework import permissions
from backend.pigeonhole.apps.users.models import Teacher, Student
from backend.pigeonhole.apps.users.models import User


class CanAccessProject(permissions.BasePermission):
Expand All @@ -9,15 +9,15 @@ def has_permission(self, request, view):
user = request.user
subject_id = view.kwargs.get('course_id')
# If the user is a teacher, grant access.
if isinstance(user, Teacher):
if user.course.filter(id=subject_id).exists():
return True
elif isinstance(user, Teacher) and user.is_admin:
return True
# If the user is a student, grant access only to their own projects.
elif isinstance(user, Student):
if user.course.filter(id=subject_id).exists():
return True
elif request.user.is_superuser:
return True
# if isinstance(user, Teacher):
# if user.course.filter(id=subject_id).exists():
# return True
# elif isinstance(user, Teacher) and user.is_admin:
# return True
# # If the user is a student, grant access only to their own projects.
# elif isinstance(user, Student):
# if user.course.filter(id=subject_id).exists():
# return True
# elif request.user.is_superuser:
# return True
return False
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Generated by Django 5.0.2 on 2024-03-11 13:17

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('courses', '0001_initial'),
('groups', '0005_remove_group_student_group_user'),
('users', '0002_rename_is_assistent_teacher_is_assistant'),
]

operations = [
migrations.RemoveField(
model_name='teacher',
name='course',
),
migrations.RemoveField(
model_name='teacher',
name='id',
),
migrations.AlterModelManagers(
name='user',
managers=[
],
),
migrations.AddField(
model_name='user',
name='course',
field=models.ManyToManyField(to='courses.course'),
),
migrations.AddField(
model_name='user',
name='role',
field=models.IntegerField(choices=[(1, 'Superuser'), (2, 'Teacher'), (3, 'Student')], default=3),
),
migrations.AlterField(
model_name='user',
name='email',
field=models.EmailField(max_length=254, unique=True),
),
migrations.AlterField(
model_name='user',
name='first_name',
field=models.CharField(max_length=30),
),
migrations.AlterField(
model_name='user',
name='id',
field=models.BigAutoField(primary_key=True, serialize=False),
),
migrations.AlterField(
model_name='user',
name='last_name',
field=models.CharField(max_length=150),
),
migrations.DeleteModel(
name='Student',
),
migrations.DeleteModel(
name='Teacher',
),
]
58 changes: 27 additions & 31 deletions backend/pigeonhole/apps/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,43 @@
from backend.pigeonhole.apps.courses.models import Course


class User(AbstractUser):
class Meta(AbstractUser.Meta):
db_table = "auth_user"

@property
def name(self):
return f"{self.first_name.strip()} {self.last_name.strip()}"
class Roles(models.IntegerChoices):
SUPERUSER = 1
TEACHER = 2
STUDENT = 3


class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ['id', 'email', 'first_name', 'last_name']


class Student(models.Model):
id = models.ForeignKey(User, on_delete=models.CASCADE, primary_key=True)
number = models.IntegerField()
class User(AbstractUser):
id = models.BigAutoField(primary_key=True)
email = models.EmailField(unique=True)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=150)
course = models.ManyToManyField(Course)
role = models.IntegerField(choices=Roles.choices, default=Roles.STUDENT)

objects = models.Manager()

class Meta(AbstractUser.Meta):
db_table = "auth_user"

class StudentSerializer(serializers.ModelSerializer):
id = serializers.PrimaryKeyRelatedField(queryset=User.objects.all())

class Meta:
model = Student
fields = ['number', 'course', 'id']
@property
def name(self):
return f"{self.first_name.strip()} {self.last_name.strip()}"

@property
def is_super(self):
return self.role == Roles.SUPERUSER

class Teacher(models.Model):
id = models.ForeignKey(User, on_delete=models.CASCADE, primary_key=True)
course = models.ManyToManyField(Course)
is_admin = models.BooleanField(default=False)
is_assistant = models.BooleanField(default=False)
@property
def is_teacher(self):
return self.role == Roles.TEACHER

objects = models.Manager()
@property
def is_student(self):
return self.role == Roles.STUDENT


class TeacherSerializer(serializers.ModelSerializer):
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = Teacher
fields = ['course', 'id', 'is_admin', 'is_assistent']
model = User
fields = ['id', 'email', 'first_name', 'last_name', 'course', 'role']
24 changes: 1 addition & 23 deletions backend/pigeonhole/apps/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,7 @@
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response

from backend.pigeonhole.apps.users.models import Student, StudentSerializer, User, UserSerializer


class StudentViewSet(viewsets.ModelViewSet):
queryset = Student.objects.all()
serializer_class = StudentSerializer
permission_classes = [IsAuthenticated]

def create(self, request, *args, **kwargs):
serializer = StudentSerializer(data=request.data)
print(serializer)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

def perform_create(self, serializer):
serializer.save(user=self.request.user)

def list(self, request, *args, **kwargs):
serializer = StudentSerializer(self.queryset, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
from backend.pigeonhole.apps.users.models import User, UserSerializer


class UserViewSet(viewsets.ModelViewSet):
Expand Down
6 changes: 3 additions & 3 deletions backend/pigeonhole/tests/test_models/test_conditions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.test import TestCase
from backend.pigeonhole.apps.users.models import User, Student, Teacher
from backend.pigeonhole.apps.users.models import User
from backend.pigeonhole.apps.courses.models import Course
from backend.pigeonhole.apps.projects.models import Project, Conditions, AllowedExtension, ForbiddenExtension

Expand All @@ -22,8 +22,8 @@ def setUp(self):
)

# Create teacher and student using the created users
teacher = Teacher.objects.create(id=teacher_user)
student = Student.objects.create(id=student_user, number=1234)
teacher = User.objects.create(id=teacher_user)
student = User.objects.create(id=student_user, number=1234)

# Create course
course = Course.objects.create(name="Math", description="Mathematics")
Expand Down
10 changes: 5 additions & 5 deletions backend/pigeonhole/tests/test_models/test_course.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.test import TestCase
from backend.pigeonhole.apps.courses.models import Course
from backend.pigeonhole.apps.users.models import User, Student, Teacher
from backend.pigeonhole.apps.users.models import User
from django.db.utils import DataError


Expand All @@ -25,24 +25,24 @@ def setUp(self):
)

# Create teacher and student using the created users
teacher = Teacher.objects.create(id=teacher_user)
student = Student.objects.create(id=student_user, number=1234)
teacher = User.objects.create(id=teacher_user)
student = User.objects.create(id=student_user, number=1234)

# Create course
course = Course.objects.create(name="Math", description="Mathematics")
teacher.course.add(course)
student.course.add(course)

def test_course_teacher_relationship(self):
teacher = Teacher.objects.get(id__email="[email protected]")
teacher = User.objects.get(id__email="[email protected]")
course = Course.objects.get(name="Math")
self.assertIn(course, teacher.course.all())
course_alter_ego = teacher.course.get(name="Math")
self.assertEqual(course, course_alter_ego)
self.assertTrue(course_alter_ego, "Mathematics")

def test_course_students_relationship(self):
student = Student.objects.get(id__email="[email protected]")
student = User.objects.get(id__email="[email protected]")
course = Course.objects.get(name="Math")
self.assertIn(course, student.course.all())
course_alter_ego = student.course.get(name="Math")
Expand Down
12 changes: 6 additions & 6 deletions backend/pigeonhole/tests/test_models/test_groups.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.test import TestCase
from backend.pigeonhole.apps.courses.models import Course
from backend.pigeonhole.apps.users.models import User, Student, Teacher
from backend.pigeonhole.apps.users.models import User
from backend.pigeonhole.apps.groups.models import Group
from backend.pigeonhole.apps.projects.models import Project

Expand Down Expand Up @@ -34,9 +34,9 @@ def setUp(self):
)

# Create teacher and student using the created users
teacher = Teacher.objects.create(id=teacher_user)
student = Student.objects.create(id=student_user, number=1234)
student2 = Student.objects.create(id=student_user2, number=5678)
teacher = User.objects.create(id=teacher_user)
student = User.objects.create(id=student_user, number=1234)
student2 = User.objects.create(id=student_user2, number=5678)

# Create course
course = Course.objects.create(name="Math", description="Mathematics")
Expand Down Expand Up @@ -67,8 +67,8 @@ def test_group_project_relation(self):

def test_group_student_relation(self):
group = Group.objects.get(group_nr=1)
student = Student.objects.get(id__email="[email protected]")
student2 = Student.objects.get(id__email="[email protected]")
student = User.objects.get(id__email="[email protected]")
student2 = User.objects.get(id__email="[email protected]")
self.assertIn(student, group.student.all())
self.assertIn(student2, group.student.all())

Expand Down
Loading

0 comments on commit a0f5a92

Please sign in to comment.