# Path: rest_framework_extensions/mixins.py
# class NestedViewSetMixin:
#     def get_queryset(self):
#         return self.filter_queryset_by_parents_lookups(
#             super().get_queryset()
#         )
# 
#     def filter_queryset_by_parents_lookups(self, queryset):
#         parents_query_dict = self.get_parents_query_dict()
#         if parents_query_dict:
#             try:
#                 return queryset.filter(**parents_query_dict)
#             except ValueError:
#                 raise Http404
#         else:
#             return queryset
# 
#     def get_parents_query_dict(self):
#         result = {}
#         for kwarg_name, kwarg_value in self.kwargs.items():
#             if kwarg_name.startswith(extensions_api_settings.DEFAULT_PARENT_LOOKUP_KWARG_NAME_PREFIX):
#                 query_lookup = kwarg_name.replace(
#                     extensions_api_settings.DEFAULT_PARENT_LOOKUP_KWARG_NAME_PREFIX,
#                     '',
#                     1
#                 )
#                 query_value = kwarg_value
#                 result[query_lookup] = query_value
#         return result
# 
# Path: tests_app/tests/functional/routers/nested_router_mixin/models.py
# class NestedRouterMixinUserModel(models.Model):
#     email = models.EmailField(blank=True, null=True)
#     name = models.CharField(max_length=10)
#     groups = models.ManyToManyField(
#         'NestedRouterMixinGroupModel', related_name='user_groups')
# 
# class NestedRouterMixinGroupModel(models.Model):
#     name = models.CharField(max_length=10)
#     permissions = models.ManyToManyField('NestedRouterMixinPermissionModel')
# 
# class NestedRouterMixinPermissionModel(models.Model):
#     name = models.CharField(max_length=10)
# 
# class NestedRouterMixinTaskModel(models.Model):
#     title = models.CharField(max_length=30)
# 
# class NestedRouterMixinBookModel(models.Model):
#     title = models.CharField(max_length=30)
# 
# class NestedRouterMixinCommentModel(models.Model):
#     content_type = models.ForeignKey(
#         "contenttypes.ContentType",
#         blank=True,
#         null=True,
#         on_delete=models.CASCADE,
#     )
#     object_id = models.PositiveIntegerField(blank=True, null=True)
#     content_object = GenericForeignKey()
#     text = models.CharField(max_length=30)
# 
# Path: tests_app/tests/functional/routers/nested_router_mixin/serializers.py
# class UserSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = UserModel
#         fields = (
#             'id',
#             'name'
#         )
# 
# class GroupSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = GroupModel
#         fields = (
#             'id',
#             'name'
#         )
# 
# class PermissionSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = PermissionModel
#         fields = (
#             'id',
#             'name'
#         )
# 
# class TaskSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = TaskModel
#         fields = (
#             'id',
#             'title'
#         )
# 
# class BookSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = BookModel
#         fields = (
#             'id',
#             'title'
#         )
# 
# class CommentSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = CommentModel
#         fields = (
#             'id',
#             'content_type',
#             'object_id',
#             'text'
#         )


from django.contrib.contenttypes.models import ContentType
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from rest_framework_extensions.mixins import NestedViewSetMixin
from .models import (
    NestedRouterMixinUserModel as UserModel,
    NestedRouterMixinGroupModel as GroupModel,
    NestedRouterMixinPermissionModel as PermissionModel,
    NestedRouterMixinTaskModel as TaskModel,
    NestedRouterMixinBookModel as BookModel,
    NestedRouterMixinCommentModel as CommentModel
)
from .serializers import (
    UserSerializer,
    GroupSerializer,
    PermissionSerializer,
    TaskSerializer,
    BookSerializer,
    CommentSerializer
)

class TaskViewSet(NestedViewSetMixin, ModelViewSet):
    queryset = TaskModel.objects.all()
    serializer_class = TaskSerializer


class BookViewSet(NestedViewSetMixin, ModelViewSet):
    queryset = BookModel.objects.all()
    serializer_class = BookSerializer


class CommentViewSet(NestedViewSetMixin, ModelViewSet):
    queryset = CommentModel.objects.all()
    serializer_class = CommentSerializer


class TaskCommentViewSet(CommentViewSet):
    def get_queryset(self):
        return super().get_queryset().filter(
            content_type=ContentType.objects.get_for_model(TaskModel)
        )


class BookCommentViewSet(CommentViewSet):
    def get_queryset(self):
        return super().get_queryset().filter(
            content_type=ContentType.objects.get_for_model(BookModel)
        )



# Path: rest_framework_extensions/mixins.py
# class NestedViewSetMixin:
#     def get_queryset(self):
#         return self.filter_queryset_by_parents_lookups(
#             super().get_queryset()
#         )
# 
#     def filter_queryset_by_parents_lookups(self, queryset):
#         parents_query_dict = self.get_parents_query_dict()
#         if parents_query_dict:
#             try:
#                 return queryset.filter(**parents_query_dict)
#             except ValueError:
#                 raise Http404
#         else:
#             return queryset
# 
#     def get_parents_query_dict(self):
#         result = {}
#         for kwarg_name, kwarg_value in self.kwargs.items():
#             if kwarg_name.startswith(extensions_api_settings.DEFAULT_PARENT_LOOKUP_KWARG_NAME_PREFIX):
#                 query_lookup = kwarg_name.replace(
#                     extensions_api_settings.DEFAULT_PARENT_LOOKUP_KWARG_NAME_PREFIX,
#                     '',
#                     1
#                 )
#                 query_value = kwarg_value
#                 result[query_lookup] = query_value
#         return result
# 
# Path: tests_app/tests/functional/routers/nested_router_mixin/models.py
# class NestedRouterMixinUserModel(models.Model):
#     email = models.EmailField(blank=True, null=True)
#     name = models.CharField(max_length=10)
#     groups = models.ManyToManyField(
#         'NestedRouterMixinGroupModel', related_name='user_groups')
# 
# class NestedRouterMixinGroupModel(models.Model):
#     name = models.CharField(max_length=10)
#     permissions = models.ManyToManyField('NestedRouterMixinPermissionModel')
# 
# class NestedRouterMixinPermissionModel(models.Model):
#     name = models.CharField(max_length=10)
# 
# class NestedRouterMixinTaskModel(models.Model):
#     title = models.CharField(max_length=30)
# 
# class NestedRouterMixinBookModel(models.Model):
#     title = models.CharField(max_length=30)
# 
# class NestedRouterMixinCommentModel(models.Model):
#     content_type = models.ForeignKey(
#         "contenttypes.ContentType",
#         blank=True,
#         null=True,
#         on_delete=models.CASCADE,
#     )
#     object_id = models.PositiveIntegerField(blank=True, null=True)
#     content_object = GenericForeignKey()
#     text = models.CharField(max_length=30)
# 
# Path: tests_app/tests/functional/routers/nested_router_mixin/serializers.py
# class UserSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = UserModel
#         fields = (
#             'id',
#             'name'
#         )
# 
# class GroupSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = GroupModel
#         fields = (
#             'id',
#             'name'
#         )
# 
# class PermissionSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = PermissionModel
#         fields = (
#             'id',
#             'name'
#         )
# 
# class TaskSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = TaskModel
#         fields = (
#             'id',
#             'title'
#         )
# 
# class BookSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = BookModel
#         fields = (
#             'id',
#             'title'
#         )
# 
# class CommentSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = CommentModel
#         fields = (
#             'id',
#             'content_type',
#             'object_id',
#             'text'
#         )


# Path: tests_app/tests/functional/routers/nested_router_mixin/views.py
from django.contrib.contenttypes.models import ContentType
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from rest_framework_extensions.mixins import NestedViewSetMixin
from .models import (
    NestedRouterMixinUserModel as UserModel,
    NestedRouterMixinGroupModel as GroupModel,
    NestedRouterMixinPermissionModel as PermissionModel,
    NestedRouterMixinTaskModel as TaskModel,
    NestedRouterMixinBookModel as BookModel,
    NestedRouterMixinCommentModel as CommentModel
)
from .serializers import (
    UserSerializer,
    GroupSerializer,
    PermissionSerializer,
    TaskSerializer,
    BookSerializer,
    CommentSerializer
)


class TaskViewSet(NestedViewSetMixin, ModelViewSet):
    queryset = TaskModel.objects.all()
    serializer_class = TaskSerializer


class BookViewSet(NestedViewSetMixin, ModelViewSet):
    queryset = BookModel.objects.all()
    serializer_class = BookSerializer


class CommentViewSet(NestedViewSetMixin, ModelViewSet):
    queryset = CommentModel.objects.all()
    serializer_class = CommentSerializer


class TaskCommentViewSet(CommentViewSet):
    def get_queryset(self):
        return super().get_queryset().filter(
            content_type=ContentType.objects.get_for_model(TaskModel)
        )


class BookCommentViewSet(CommentViewSet):
    def get_queryset(self):
        return super().get_queryset().filter(
            content_type=ContentType.objects.get_for_model(BookModel)
        )



class UserViewSetWithEmailLookup(NestedViewSetMixin, ModelViewSet):