# Path: students/models.py
# class Subject(models.Model):
#     title = models.CharField(unique=True, max_length=50)
# 
#     def __str__(self):
#         return self.title
# 
#     class Meta:
#         ordering = ['title']
# 
# 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: materials/serializers.py
# class MaterialSerializer(serializers.ModelSerializer):
#     title = serializers.CharField(required=False, min_length=3, max_length=150, allow_blank=True)
#     section = serializers.CharField(required=False, min_length=3, max_length=150, allow_blank=True)
#     content = serializers.CharField(allow_blank=False)
#     subject = SubjectSerializer(read_only=True)
#     video_url = serializers.URLField(required=False, allow_blank=True)
# 
#     class Meta:
#         model = Material
#         fields = (
#             'id', 'title', 'section', 'content', 'class_number', 'subject', 'video_url', 'author'
#         )
#         depth = 1
# 
#     def create(self, validated_data):
#         request = self.context['request']
#         subject = self.context['subject']
# 
#         author = request.user.teacher
# 
#         return Material.objects.create(subject=subject, author=author, **validated_data)
# 
# class MaterialReadSerializer(MaterialSerializer):
#     author = TeacherAuthorSerializer(read_only=True)
# 
# Path: materials/models.py
# class Material(models.Model):
#     title = models.CharField(max_length=150, blank=True)
#     section = models.CharField(max_length=150, blank=True)
#     content = models.TextField(blank=False)
#     class_number = models.IntegerField(
#         choices=Class.CLASS_NUMBERS,
#         validators=[Class.CLASS_NUMBER_VALIDATORS]
#     )
#     subject = models.ForeignKey(Subject, related_name='materials', on_delete=models.CASCADE)
#     video_url = models.URLField(blank=True)
#     author = models.ForeignKey(Teacher, null=True, on_delete=models.CASCADE)
# 
#     def __str__(self):
#         return '{} - {} ({} class) posted by {}'.format(
#             self.title, self.subject, self.class_number, self.author
#         )
# 
# Path: materials/filters.py
# class MaterialListFilterBackend(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(class_number=request.user.student.clazz.number)


from rest_framework import viewsets, mixins, 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.models import Subject
from students.permissions import IsTeacher, IsTeacherAuthor
from .serializers import MaterialSerializer, MaterialReadSerializer
from .models import Material
from .filters import MaterialListFilterBackend
        'list': (IsAuthenticated,),
        'retrieve': (IsAuthenticated,),
        'create': (IsAuthenticated, IsTeacher),
        'update': (IsAuthenticated, IsTeacher, IsTeacherAuthor),
        'destroy': (IsAuthenticated, IsTeacher, IsTeacherAuthor)
    }

    def get_serializer_class(self):
        return MaterialReadSerializer if self.request.method in ('GET',) else MaterialSerializer

    def get_permissions(self):
        return [
            permission()
            for permission
            in self.permission_classes_by_action[self.action]
        ]


class MaterialsListViewSet(mixins.ListModelMixin, MaterialsViewSet):
    queryset = Material.objects.all()
    filter_backends = (MaterialListFilterBackend, FullWordSearchFilter)
    word_fields = ('title', 'section')


class NestedMaterialsViewSet(mixins.RetrieveModelMixin,
                             mixins.CreateModelMixin,
                             mixins.UpdateModelMixin,
                             mixins.DestroyModelMixin,
                             MaterialsListViewSet):
    def get_related_subject(self):

# Path: students/models.py
# class Subject(models.Model):
#     title = models.CharField(unique=True, max_length=50)
# 
#     def __str__(self):
#         return self.title
# 
#     class Meta:
#         ordering = ['title']
# 
# 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: materials/serializers.py
# class MaterialSerializer(serializers.ModelSerializer):
#     title = serializers.CharField(required=False, min_length=3, max_length=150, allow_blank=True)
#     section = serializers.CharField(required=False, min_length=3, max_length=150, allow_blank=True)
#     content = serializers.CharField(allow_blank=False)
#     subject = SubjectSerializer(read_only=True)
#     video_url = serializers.URLField(required=False, allow_blank=True)
# 
#     class Meta:
#         model = Material
#         fields = (
#             'id', 'title', 'section', 'content', 'class_number', 'subject', 'video_url', 'author'
#         )
#         depth = 1
# 
#     def create(self, validated_data):
#         request = self.context['request']
#         subject = self.context['subject']
# 
#         author = request.user.teacher
# 
#         return Material.objects.create(subject=subject, author=author, **validated_data)
# 
# class MaterialReadSerializer(MaterialSerializer):
#     author = TeacherAuthorSerializer(read_only=True)
# 
# Path: materials/models.py
# class Material(models.Model):
#     title = models.CharField(max_length=150, blank=True)
#     section = models.CharField(max_length=150, blank=True)
#     content = models.TextField(blank=False)
#     class_number = models.IntegerField(
#         choices=Class.CLASS_NUMBERS,
#         validators=[Class.CLASS_NUMBER_VALIDATORS]
#     )
#     subject = models.ForeignKey(Subject, related_name='materials', on_delete=models.CASCADE)
#     video_url = models.URLField(blank=True)
#     author = models.ForeignKey(Teacher, null=True, on_delete=models.CASCADE)
# 
#     def __str__(self):
#         return '{} - {} ({} class) posted by {}'.format(
#             self.title, self.subject, self.class_number, self.author
#         )
# 
# Path: materials/filters.py
# class MaterialListFilterBackend(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(class_number=request.user.student.clazz.number)


# Path: materials/views.py
from rest_framework import viewsets, mixins, 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.models import Subject
from students.permissions import IsTeacher, IsTeacherAuthor
from .serializers import MaterialSerializer, MaterialReadSerializer
from .models import Material
from .filters import MaterialListFilterBackend

        'list': (IsAuthenticated,),
        'retrieve': (IsAuthenticated,),
        'create': (IsAuthenticated, IsTeacher),
        'update': (IsAuthenticated, IsTeacher, IsTeacherAuthor),
        'destroy': (IsAuthenticated, IsTeacher, IsTeacherAuthor)
    }

    def get_serializer_class(self):
        return MaterialReadSerializer if self.request.method in ('GET',) else MaterialSerializer

    def get_permissions(self):
        return [
            permission()
            for permission
            in self.permission_classes_by_action[self.action]
        ]


class MaterialsListViewSet(mixins.ListModelMixin, MaterialsViewSet):
    queryset = Material.objects.all()
    filter_backends = (MaterialListFilterBackend, FullWordSearchFilter)
    word_fields = ('title', 'section')


class NestedMaterialsViewSet(mixins.RetrieveModelMixin,
                             mixins.CreateModelMixin,
                             mixins.UpdateModelMixin,
                             mixins.DestroyModelMixin,
                             MaterialsListViewSet):
    def get_related_subject(self):

        return get_object_or_404(Subject, id=self.kwargs['subject_pk'])