# Path: students/permissions.py
# class IsStudent(permissions.BasePermission):
#     message = ROLE_DEFAULT_MESSAGE.format('student')
# 
#     def has_permission(self, request, view):
#         return Student.objects.filter(user=request.user).exists()
# 
# 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 IsUserAuthor(permissions.BasePermission):
#     message = AUTHOR_DEFAULT_MESSAGE
# 
#     def has_object_permission(self, request, view, obj):
#         return obj.author == request.user
# 
# Path: news/models.py
# class News(AbstractPost):
#     title = models.CharField(max_length=100, blank=False)
#     content = models.TextField(max_length=10000, blank=False)
#     class_number = models.IntegerField(
#         default=8,
#         validators=[Class.CLASS_NUMBER_VALIDATORS],
#         choices=Class.CLASS_NUMBERS
#     )
#     class_letter = models.CharField(
#         max_length=1,
#         blank=True,
#         choices=Class.CLASS_LETTERS
#     )
# 
#     def __str__(self):
#         return '{} ({})'.format(self.title, self.posted_on.date())
# 
#     class Meta:
#         ordering = ['-last_edited_on']
#         verbose_name_plural = 'news'
# 
# Path: news/serializers.py
# class NewsSerializer(AbstractPostSerializer):
#     title = serializers.CharField(min_length=3, max_length=100)
#     content = serializers.CharField(min_length=5, max_length=10000)
#     comments = CommentSerializer(read_only=True, many=True)
# 
#     class Meta:
#         model = News
#         fields = AbstractPostSerializer.Meta.fields + (
#             'id', 'title', 'content', 'class_number', 'class_letter', 'comments'
#         )
# 
#     def create(self, validated_data):
#         author = self.context['request'].user
#         validated_data['class_number'] = self.context['class_number']
#         validated_data['class_letter'] = self.context.get('class_letter', '')
# 
#         news = News.objects.create(author=author, **validated_data)
# 
#         return news
# 
# class CommentSerializer(AbstractPostSerializer):
#     author_image = serializers.SerializerMethodField()
#     content = serializers.CharField(max_length=2048)
# 
#     class Meta:
#         model = Comment
#         fields = AbstractPostSerializer.Meta.fields + ('id', 'author_image', 'content')
# 
#     def get_author_image(self, obj):
#         author = obj.author
# 
#         try:
#             field = author.student
#         except AttributeError:
#             field = author.teacher
# 
#         return field.profile_image_url
# 
#     def create(self, validated_data):
#         news = self.context['news']
#         request = self.context['request']
# 
#         author = request.user
# 
#         return Comment.objects.create(news=news, author=author, **validated_data)
# 
# class CommentReadSerializer(CommentSerializer):
#     author = UserInfoSerializer(read_only=True)
# 
# Path: news/filters.py
# class TeachersListFilterBackend(filters.BaseFilterBackend):
#     def filter_queryset(self, request, queryset, view):
#         return queryset.filter(author=request.user)
# 
# class ClassNumberFilterBackend(filters.BaseFilterBackend):
#     def filter_queryset(self, request, queryset, view):
#         return queryset.filter(class_number=view.kwargs['class_number'])


from rest_framework import generics, viewsets
from rest_framework.permissions import IsAuthenticated
from rest_framework_word_filter import FullWordSearchFilter
from students.permissions import IsStudent, IsTeacher, IsUserAuthor
from .models import News
from .serializers import NewsSerializer, CommentSerializer, CommentReadSerializer
from .filters import TeachersListFilterBackend, ClassNumberFilterBackend





class NewsDefaultViewSet(viewsets.ModelViewSet):
    serializer_class = NewsSerializer

    def get_clazz_info(self):
        raise NotImplementedError()

    def get_serializer_context(self):
        context = super().get_serializer_context()

        return dict(context, **self.get_clazz_info())

    def get_queryset(self):
        class_info = self.get_clazz_info()
        class_number = class_info['class_number']
        class_letter = class_info['class_letter']

        common_news = News.objects.filter(class_number=class_number, class_letter='')
        news = News.objects.filter(class_number=class_number, class_letter=class_letter)

        return common_news | news


class NewsStudentsViewSet(NewsDefaultViewSet):
    permission_classes_by_action = {

# Path: students/permissions.py
# class IsStudent(permissions.BasePermission):
#     message = ROLE_DEFAULT_MESSAGE.format('student')
# 
#     def has_permission(self, request, view):
#         return Student.objects.filter(user=request.user).exists()
# 
# 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 IsUserAuthor(permissions.BasePermission):
#     message = AUTHOR_DEFAULT_MESSAGE
# 
#     def has_object_permission(self, request, view, obj):
#         return obj.author == request.user
# 
# Path: news/models.py
# class News(AbstractPost):
#     title = models.CharField(max_length=100, blank=False)
#     content = models.TextField(max_length=10000, blank=False)
#     class_number = models.IntegerField(
#         default=8,
#         validators=[Class.CLASS_NUMBER_VALIDATORS],
#         choices=Class.CLASS_NUMBERS
#     )
#     class_letter = models.CharField(
#         max_length=1,
#         blank=True,
#         choices=Class.CLASS_LETTERS
#     )
# 
#     def __str__(self):
#         return '{} ({})'.format(self.title, self.posted_on.date())
# 
#     class Meta:
#         ordering = ['-last_edited_on']
#         verbose_name_plural = 'news'
# 
# Path: news/serializers.py
# class NewsSerializer(AbstractPostSerializer):
#     title = serializers.CharField(min_length=3, max_length=100)
#     content = serializers.CharField(min_length=5, max_length=10000)
#     comments = CommentSerializer(read_only=True, many=True)
# 
#     class Meta:
#         model = News
#         fields = AbstractPostSerializer.Meta.fields + (
#             'id', 'title', 'content', 'class_number', 'class_letter', 'comments'
#         )
# 
#     def create(self, validated_data):
#         author = self.context['request'].user
#         validated_data['class_number'] = self.context['class_number']
#         validated_data['class_letter'] = self.context.get('class_letter', '')
# 
#         news = News.objects.create(author=author, **validated_data)
# 
#         return news
# 
# class CommentSerializer(AbstractPostSerializer):
#     author_image = serializers.SerializerMethodField()
#     content = serializers.CharField(max_length=2048)
# 
#     class Meta:
#         model = Comment
#         fields = AbstractPostSerializer.Meta.fields + ('id', 'author_image', 'content')
# 
#     def get_author_image(self, obj):
#         author = obj.author
# 
#         try:
#             field = author.student
#         except AttributeError:
#             field = author.teacher
# 
#         return field.profile_image_url
# 
#     def create(self, validated_data):
#         news = self.context['news']
#         request = self.context['request']
# 
#         author = request.user
# 
#         return Comment.objects.create(news=news, author=author, **validated_data)
# 
# class CommentReadSerializer(CommentSerializer):
#     author = UserInfoSerializer(read_only=True)
# 
# Path: news/filters.py
# class TeachersListFilterBackend(filters.BaseFilterBackend):
#     def filter_queryset(self, request, queryset, view):
#         return queryset.filter(author=request.user)
# 
# class ClassNumberFilterBackend(filters.BaseFilterBackend):
#     def filter_queryset(self, request, queryset, view):
#         return queryset.filter(class_number=view.kwargs['class_number'])


# Path: news/views.py
from rest_framework import generics, viewsets
from rest_framework.permissions import IsAuthenticated
from rest_framework_word_filter import FullWordSearchFilter
from students.permissions import IsStudent, IsTeacher, IsUserAuthor
from .models import News
from .serializers import NewsSerializer, CommentSerializer, CommentReadSerializer
from .filters import TeachersListFilterBackend, ClassNumberFilterBackend






class NewsDefaultViewSet(viewsets.ModelViewSet):
    serializer_class = NewsSerializer

    def get_clazz_info(self):
        raise NotImplementedError()

    def get_serializer_context(self):
        context = super().get_serializer_context()

        return dict(context, **self.get_clazz_info())

    def get_queryset(self):
        class_info = self.get_clazz_info()
        class_number = class_info['class_number']
        class_letter = class_info['class_letter']

        common_news = News.objects.filter(class_number=class_number, class_letter='')
        news = News.objects.filter(class_number=class_number, class_letter=class_letter)

        return common_news | news


class NewsStudentsViewSet(NewsDefaultViewSet):
    permission_classes_by_action = {

        'list': (IsAuthenticated, IsStudent),