# Path: students/permissions.py
# class IsTeacher(permissions.BasePermission):
#     message = ROLE_DEFAULT_MESSAGE.format('teacher')
# 
#     def has_permission(self, request, view):
#         return Teacher.objects.filter(user=request.user).exists()
# 
# class IsTeacherAuthor(permissions.BasePermission):
#     message = AUTHOR_DEFAULT_MESSAGE
# 
#     def has_object_permission(self, request, view, obj):
#         return obj.author == request.user.teacher
# 
# Path: exams/serializers.py
# class ExamSerializer(serializers.ModelSerializer):
#     clazz = ClassSerializer()
#     topic = serializers.CharField(required=True, max_length=60)
#     details = serializers.CharField(max_length=10000, allow_blank=True, required=False)
# 
#     class Meta:
#         model = Exam
#         fields = ('id', 'subject', 'date', 'clazz', 'topic', 'details', 'author')
#         depth = 1
# 
#     def create(self, validated_data):
#         request = self.context['request']
# 
#         author = request.user.teacher
#         subject = author.subject
#         clazz = get_object_or_404(Class, **validated_data.pop('clazz'))
# 
#         exam = Exam.objects.create(subject=subject, author=author, clazz=clazz, **validated_data)
# 
#         recipient_list = User.objects.filter(student__clazz=clazz)
#         for user in recipient_list:
#             send_creation_email(user, model=exam)
# 
#         return exam
# 
#     def update(self, instance, validated_data):
#         instance.__dict__.update(**validated_data)
#         instance.save()
# 
#         return instance
# 
# class ExamReadSerializer(ExamSerializer):
#     subject = SubjectSerializer(read_only=True)
#     clazz = ClassSerializer(read_only=True)
#     author = TeacherAuthorSerializer(read_only=True)
# 
# Path: exams/models.py
# class Exam(models.Model):
#     subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
#     date = models.DateField(auto_now=False, validators=[validate_date])
#     clazz = models.ForeignKey(Class, on_delete=models.CASCADE)
#     topic = models.CharField(max_length=60)
#     details = models.TextField(max_length=10000, blank=True)
#     author = models.ForeignKey(Teacher, null=True, related_name='exams', on_delete=models.CASCADE)
# 
#     def __str__(self):
#         return '{} - {} ({})'.format(self.subject, self.clazz, self.date)
# 
#     class Meta:
#         ordering = ['date', 'subject', 'clazz']
# 
# Path: exams/filters.py
# class ExamsFilterBackend(filters.BaseFilterBackend):
#     def filter_queryset(self, request, queryset, view):
#         if IsTeacher().has_permission(request, self):
#             return queryset.filter(subject=request.user.teacher.subject)
# 
#         return queryset.filter(clazz=request.user.student.clazz)


from datetime import datetime
from rest_framework import viewsets, status
from rest_framework.generics import get_object_or_404
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from rest_framework_word_filter import FullWordSearchFilter
from students.permissions import IsTeacher, IsTeacherAuthor
from .serializers import ExamSerializer, ExamReadSerializer
from .models import Exam
from .filters import ExamsFilterBackend






class ExamsViewSet(viewsets.ModelViewSet):
    permission_classes_by_action = {
        'list': (IsAuthenticated,),
        'retrieve': (IsAuthenticated,),
        'create': (IsAuthenticated, IsTeacher),
        'update': (IsAuthenticated, IsTeacher, IsTeacherAuthor),
        'destroy': (IsAuthenticated, IsTeacher, IsTeacherAuthor)
    }
    queryset = Exam.objects.filter(date__gte=datetime.now())

# Path: students/permissions.py
# class IsTeacher(permissions.BasePermission):
#     message = ROLE_DEFAULT_MESSAGE.format('teacher')
# 
#     def has_permission(self, request, view):
#         return Teacher.objects.filter(user=request.user).exists()
# 
# class IsTeacherAuthor(permissions.BasePermission):
#     message = AUTHOR_DEFAULT_MESSAGE
# 
#     def has_object_permission(self, request, view, obj):
#         return obj.author == request.user.teacher
# 
# Path: exams/serializers.py
# class ExamSerializer(serializers.ModelSerializer):
#     clazz = ClassSerializer()
#     topic = serializers.CharField(required=True, max_length=60)
#     details = serializers.CharField(max_length=10000, allow_blank=True, required=False)
# 
#     class Meta:
#         model = Exam
#         fields = ('id', 'subject', 'date', 'clazz', 'topic', 'details', 'author')
#         depth = 1
# 
#     def create(self, validated_data):
#         request = self.context['request']
# 
#         author = request.user.teacher
#         subject = author.subject
#         clazz = get_object_or_404(Class, **validated_data.pop('clazz'))
# 
#         exam = Exam.objects.create(subject=subject, author=author, clazz=clazz, **validated_data)
# 
#         recipient_list = User.objects.filter(student__clazz=clazz)
#         for user in recipient_list:
#             send_creation_email(user, model=exam)
# 
#         return exam
# 
#     def update(self, instance, validated_data):
#         instance.__dict__.update(**validated_data)
#         instance.save()
# 
#         return instance
# 
# class ExamReadSerializer(ExamSerializer):
#     subject = SubjectSerializer(read_only=True)
#     clazz = ClassSerializer(read_only=True)
#     author = TeacherAuthorSerializer(read_only=True)
# 
# Path: exams/models.py
# class Exam(models.Model):
#     subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
#     date = models.DateField(auto_now=False, validators=[validate_date])
#     clazz = models.ForeignKey(Class, on_delete=models.CASCADE)
#     topic = models.CharField(max_length=60)
#     details = models.TextField(max_length=10000, blank=True)
#     author = models.ForeignKey(Teacher, null=True, related_name='exams', on_delete=models.CASCADE)
# 
#     def __str__(self):
#         return '{} - {} ({})'.format(self.subject, self.clazz, self.date)
# 
#     class Meta:
#         ordering = ['date', 'subject', 'clazz']
# 
# Path: exams/filters.py
# class ExamsFilterBackend(filters.BaseFilterBackend):
#     def filter_queryset(self, request, queryset, view):
#         if IsTeacher().has_permission(request, self):
#             return queryset.filter(subject=request.user.teacher.subject)
# 
#         return queryset.filter(clazz=request.user.student.clazz)


# Path: exams/views.py
from datetime import datetime
from rest_framework import viewsets, status
from rest_framework.generics import get_object_or_404
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from rest_framework_word_filter import FullWordSearchFilter
from students.permissions import IsTeacher, IsTeacherAuthor
from .serializers import ExamSerializer, ExamReadSerializer
from .models import Exam
from .filters import ExamsFilterBackend







class ExamsViewSet(viewsets.ModelViewSet):
    permission_classes_by_action = {
        'list': (IsAuthenticated,),
        'retrieve': (IsAuthenticated,),
        'create': (IsAuthenticated, IsTeacher),
        'update': (IsAuthenticated, IsTeacher, IsTeacherAuthor),
        'destroy': (IsAuthenticated, IsTeacher, IsTeacherAuthor)
    }
    queryset = Exam.objects.filter(date__gte=datetime.now())

    filter_backends = (ExamsFilterBackend, FullWordSearchFilter)