# Path: news/views.py
# class NewsStudentsViewSet(NewsDefaultViewSet):
#     permission_classes_by_action = {
#         'list': (IsAuthenticated, IsStudent),
#         'retrieve': (IsAuthenticated, IsStudent),
#         'create': (IsAuthenticated, IsStudent),
#         'update': (IsAuthenticated, IsStudent, IsUserAuthor),
#         'destroy': (IsAuthenticated, IsStudent, IsUserAuthor)
#     }
#     filter_backends = (FullWordSearchFilter,)
#     word_fields = ('title', 'author__username')
# 
#     def get_permissions(self):
#         return [
#             permission()
#             for permission
#             in self.permission_classes_by_action[self.action]
#         ]
# 
#     def get_clazz_info(self):
#         clazz = self.request.user.student.clazz
# 
#         return {
#             'class_number': clazz.number,
#             'class_letter': clazz.letter
#         }
# 
# class NewsTeachersList(generics.ListAPIView):
#     permission_classes = (IsAuthenticated, IsTeacher)
#     serializer_class = NewsSerializer
#     queryset = News.objects.all()
#     filter_backends = (TeachersListFilterBackend, FullWordSearchFilter)
#     word_fields = ('title',)
# 
# class NewsTeachersClassNumberList(generics.ListCreateAPIView):
#     permission_classes = (IsAuthenticated, IsTeacher)
#     serializer_class = NewsSerializer
#     queryset = News.objects.all()
#     filter_backends = (TeachersListFilterBackend, ClassNumberFilterBackend, FullWordSearchFilter)
#     word_fields = ('title',)
# 
#     def get_serializer_context(self):
#         context = super().get_serializer_context()
#         context['class_number'] = self.kwargs['class_number']
# 
#         return context
# 
# class NewsTeachersViewSet(NewsDefaultViewSet):
#     permission_classes_by_action = {
#         'list': (IsAuthenticated, IsTeacher),
#         'retrieve': (IsAuthenticated, IsTeacher),
#         'create': (IsAuthenticated, IsTeacher),
#         'update': (IsAuthenticated, IsTeacher, IsUserAuthor),
#         'destroy': (IsAuthenticated, IsTeacher, IsUserAuthor)
#     }
#     filter_backends = (TeachersListFilterBackend, FullWordSearchFilter)
#     word_fields = ('title',)
# 
#     def get_permissions(self):
#         return [
#             permission()
#             for permission
#             in self.permission_classes_by_action[self.action]
#         ]
# 
#     def get_clazz_info(self):
#         return self.kwargs
# 
# class CommentsViewSet(viewsets.ModelViewSet):
#     permission_classes_by_action = {
#         'list': (IsAuthenticated,),
#         'retrieve': (IsAuthenticated,),
#         'create': (IsAuthenticated,),
#         'update': (IsAuthenticated, IsUserAuthor),
#         'destroy': (IsAuthenticated, IsUserAuthor)
#     }
# 
#     def get_permissions(self):
#         return [
#             permission()
#             for permission
#             in self.permission_classes_by_action[self.action]
#         ]
# 
#     def get_serializer_class(self):
#         return CommentReadSerializer if self.request.method in ('GET',) else CommentSerializer
# 
#     def get_news_pk(self):
#         return self.kwargs.get('students_news_pk', self.kwargs.get('teachers_news_pk'))
# 
#     def get_related_news(self):
#         return generics.get_object_or_404(News, id=self.get_news_pk())
# 
#     def get_serializer_context(self):
#         context = super().get_serializer_context()
#         context['news'] = self.get_related_news()
# 
#         return context
# 
#     def get_queryset(self):
#         news = self.get_related_news()
# 
#         return news.comments.all()


from django.conf.urls import url
from rest_framework_nested import routers
from .views import (
    NewsStudentsViewSet,
    NewsTeachersList, NewsTeachersClassNumberList, NewsTeachersViewSet,
    CommentsViewSet
)




app_name = 'news'

students_url_pattern = r'news/students'
teachers_default_url_pattern = r'news/teachers'
teachers_detail_url_pattern = r'(?P<class_number>[8]|[9]|1[0-2])/(?P<class_letter>[A-Z])'
teachers_url_pattern = r'{default_pattern}/{detail_pattern}'.format(
    default_pattern=teachers_default_url_pattern,
    detail_pattern=teachers_detail_url_pattern
)

students_router = routers.SimpleRouter()
students_router.register(students_url_pattern, NewsStudentsViewSet, base_name='students_news')

teachers_router = routers.SimpleRouter()
teachers_router.register(teachers_url_pattern, NewsTeachersViewSet, base_name='teachers_news')

students_comments_router = routers.NestedSimpleRouter(
    students_router, students_url_pattern, lookup='students_news'
)

# Path: news/views.py
# class NewsStudentsViewSet(NewsDefaultViewSet):
#     permission_classes_by_action = {
#         'list': (IsAuthenticated, IsStudent),
#         'retrieve': (IsAuthenticated, IsStudent),
#         'create': (IsAuthenticated, IsStudent),
#         'update': (IsAuthenticated, IsStudent, IsUserAuthor),
#         'destroy': (IsAuthenticated, IsStudent, IsUserAuthor)
#     }
#     filter_backends = (FullWordSearchFilter,)
#     word_fields = ('title', 'author__username')
# 
#     def get_permissions(self):
#         return [
#             permission()
#             for permission
#             in self.permission_classes_by_action[self.action]
#         ]
# 
#     def get_clazz_info(self):
#         clazz = self.request.user.student.clazz
# 
#         return {
#             'class_number': clazz.number,
#             'class_letter': clazz.letter
#         }
# 
# class NewsTeachersList(generics.ListAPIView):
#     permission_classes = (IsAuthenticated, IsTeacher)
#     serializer_class = NewsSerializer
#     queryset = News.objects.all()
#     filter_backends = (TeachersListFilterBackend, FullWordSearchFilter)
#     word_fields = ('title',)
# 
# class NewsTeachersClassNumberList(generics.ListCreateAPIView):
#     permission_classes = (IsAuthenticated, IsTeacher)
#     serializer_class = NewsSerializer
#     queryset = News.objects.all()
#     filter_backends = (TeachersListFilterBackend, ClassNumberFilterBackend, FullWordSearchFilter)
#     word_fields = ('title',)
# 
#     def get_serializer_context(self):
#         context = super().get_serializer_context()
#         context['class_number'] = self.kwargs['class_number']
# 
#         return context
# 
# class NewsTeachersViewSet(NewsDefaultViewSet):
#     permission_classes_by_action = {
#         'list': (IsAuthenticated, IsTeacher),
#         'retrieve': (IsAuthenticated, IsTeacher),
#         'create': (IsAuthenticated, IsTeacher),
#         'update': (IsAuthenticated, IsTeacher, IsUserAuthor),
#         'destroy': (IsAuthenticated, IsTeacher, IsUserAuthor)
#     }
#     filter_backends = (TeachersListFilterBackend, FullWordSearchFilter)
#     word_fields = ('title',)
# 
#     def get_permissions(self):
#         return [
#             permission()
#             for permission
#             in self.permission_classes_by_action[self.action]
#         ]
# 
#     def get_clazz_info(self):
#         return self.kwargs
# 
# class CommentsViewSet(viewsets.ModelViewSet):
#     permission_classes_by_action = {
#         'list': (IsAuthenticated,),
#         'retrieve': (IsAuthenticated,),
#         'create': (IsAuthenticated,),
#         'update': (IsAuthenticated, IsUserAuthor),
#         'destroy': (IsAuthenticated, IsUserAuthor)
#     }
# 
#     def get_permissions(self):
#         return [
#             permission()
#             for permission
#             in self.permission_classes_by_action[self.action]
#         ]
# 
#     def get_serializer_class(self):
#         return CommentReadSerializer if self.request.method in ('GET',) else CommentSerializer
# 
#     def get_news_pk(self):
#         return self.kwargs.get('students_news_pk', self.kwargs.get('teachers_news_pk'))
# 
#     def get_related_news(self):
#         return generics.get_object_or_404(News, id=self.get_news_pk())
# 
#     def get_serializer_context(self):
#         context = super().get_serializer_context()
#         context['news'] = self.get_related_news()
# 
#         return context
# 
#     def get_queryset(self):
#         news = self.get_related_news()
# 
#         return news.comments.all()


# Path: news/urls.py
from django.conf.urls import url
from rest_framework_nested import routers
from .views import (
    NewsStudentsViewSet,
    NewsTeachersList, NewsTeachersClassNumberList, NewsTeachersViewSet,
    CommentsViewSet
)





app_name = 'news'

students_url_pattern = r'news/students'
teachers_default_url_pattern = r'news/teachers'
teachers_detail_url_pattern = r'(?P<class_number>[8]|[9]|1[0-2])/(?P<class_letter>[A-Z])'
teachers_url_pattern = r'{default_pattern}/{detail_pattern}'.format(
    default_pattern=teachers_default_url_pattern,
    detail_pattern=teachers_detail_url_pattern
)

students_router = routers.SimpleRouter()
students_router.register(students_url_pattern, NewsStudentsViewSet, base_name='students_news')

teachers_router = routers.SimpleRouter()
teachers_router.register(teachers_url_pattern, NewsTeachersViewSet, base_name='teachers_news')

students_comments_router = routers.NestedSimpleRouter(
    students_router, students_url_pattern, lookup='students_news'
)

students_comments_router.register(r'comments', CommentsViewSet, base_name='students_news_comments')