# Path: talks/views.py
# class MeetupsViewSet(viewsets.ModelViewSet):
#     permission_classes_by_action = {
#         'list': (IsAuthenticated,),
#         'retrieve': (IsAuthenticated,),
#         'create': (IsAdminUser,),
#         'update': (IsAdminUser,),
#         'destroy': (IsAdminUser,),
#     }
#     queryset = Meetup.objects.all()
#     filter_backends = (MeetupsFilterBackend,)
#     serializer_class = MeetupSerializer
# 
#     def get_permissions(self):
#         return [
#             permission()
#             for permission
#             in self.permission_classes_by_action[self.action]
#         ]
# 
# class TalksViewSet(viewsets.ModelViewSet):
#     permission_classes_by_action = {
#         'list': (IsAuthenticated,),
#         'retrieve': (IsAuthenticated,),
#         'create': (IsAuthenticated,),
#         'update': (IsAuthenticated, IsUserAuthor),
#         'upvote': (IsAuthenticated,),
#         'downvote': (IsAuthenticated,),
#         'destroy': (IsAdminUser,),
#     }
#     serializer_class = TalkSerializer
#     filter_backends = (FullWordSearchFilter,)
#     word_filters = ('topic', 'author__username')
# 
#     def get_permissions(self):
#         return [
#             permission()
#             for permission
#             in self.permission_classes_by_action[self.action]
#         ]
# 
#     def get_related_meetup(self):
#         return generics.get_object_or_404(Meetup, id=self.kwargs['meetups_pk'])
# 
#     def get_queryset(self):
#         meetup = self.get_related_meetup()
#         return meetup.talks.all()
# 
#     def get_object(self):
#         return generics.get_object_or_404(self.get_queryset(), id=self.kwargs['pk'])
# 
#     def create(self, request, *args, **kwargs):
#         context = {
#             'request': request,
#             'meetup': self.get_related_meetup()
#         }
# 
#         serializer = self.get_serializer_class()(data=request.data, context=context)
#         serializer.is_valid(raise_exception=True)
#         self.perform_create(serializer)
# 
#         headers = self.get_success_headers(serializer.data)
# 
#         return Response(serializer.validated_data, status=status.HTTP_201_CREATED, headers=headers)
# 
#     def update(self, request, *args, **kwargs):
#         talk = self.get_object()
#         self.check_object_permissions(request, talk)
# 
#         serializer = self.get_serializer(talk, data=request.data, partial=True)
#         serializer.is_valid(raise_exception=True)
#         self.perform_update(serializer)
# 
#         return Response(serializer.data, status=status.HTTP_200_OK)
# 
#     def vote(self, request, up=True):
#         talk = self.get_object()
# 
#         if up:
#             talk.votes.up(request.user.id)
#         else:
#             talk.votes.down(request.user.id)
# 
#         return Response({'votes_count': talk.votes.count()}, status=status.HTTP_200_OK)
# 
#     @detail_route(methods=['put'])
#     def upvote(self, request, *args, **kwargs):
#         return self.vote(request, up=True)
# 
#     @detail_route(methods=['put'])
#     def downvote(self, request, *args, **kwargs):
#         return self.vote(request, up=False)


from django.conf.urls import url
from rest_framework_nested import routers
from .views import MeetupsViewSet, TalksViewSet




app_name = 'talks'

meetups_router = routers.SimpleRouter()
meetups_router.register(r'meetups', MeetupsViewSet, base_name='meetups')

talks_router = routers.NestedSimpleRouter(
    parent_router=meetups_router,
    parent_prefix=r'meetups',
    lookup='meetups'
)

# Path: talks/views.py
# class MeetupsViewSet(viewsets.ModelViewSet):
#     permission_classes_by_action = {
#         'list': (IsAuthenticated,),
#         'retrieve': (IsAuthenticated,),
#         'create': (IsAdminUser,),
#         'update': (IsAdminUser,),
#         'destroy': (IsAdminUser,),
#     }
#     queryset = Meetup.objects.all()
#     filter_backends = (MeetupsFilterBackend,)
#     serializer_class = MeetupSerializer
# 
#     def get_permissions(self):
#         return [
#             permission()
#             for permission
#             in self.permission_classes_by_action[self.action]
#         ]
# 
# class TalksViewSet(viewsets.ModelViewSet):
#     permission_classes_by_action = {
#         'list': (IsAuthenticated,),
#         'retrieve': (IsAuthenticated,),
#         'create': (IsAuthenticated,),
#         'update': (IsAuthenticated, IsUserAuthor),
#         'upvote': (IsAuthenticated,),
#         'downvote': (IsAuthenticated,),
#         'destroy': (IsAdminUser,),
#     }
#     serializer_class = TalkSerializer
#     filter_backends = (FullWordSearchFilter,)
#     word_filters = ('topic', 'author__username')
# 
#     def get_permissions(self):
#         return [
#             permission()
#             for permission
#             in self.permission_classes_by_action[self.action]
#         ]
# 
#     def get_related_meetup(self):
#         return generics.get_object_or_404(Meetup, id=self.kwargs['meetups_pk'])
# 
#     def get_queryset(self):
#         meetup = self.get_related_meetup()
#         return meetup.talks.all()
# 
#     def get_object(self):
#         return generics.get_object_or_404(self.get_queryset(), id=self.kwargs['pk'])
# 
#     def create(self, request, *args, **kwargs):
#         context = {
#             'request': request,
#             'meetup': self.get_related_meetup()
#         }
# 
#         serializer = self.get_serializer_class()(data=request.data, context=context)
#         serializer.is_valid(raise_exception=True)
#         self.perform_create(serializer)
# 
#         headers = self.get_success_headers(serializer.data)
# 
#         return Response(serializer.validated_data, status=status.HTTP_201_CREATED, headers=headers)
# 
#     def update(self, request, *args, **kwargs):
#         talk = self.get_object()
#         self.check_object_permissions(request, talk)
# 
#         serializer = self.get_serializer(talk, data=request.data, partial=True)
#         serializer.is_valid(raise_exception=True)
#         self.perform_update(serializer)
# 
#         return Response(serializer.data, status=status.HTTP_200_OK)
# 
#     def vote(self, request, up=True):
#         talk = self.get_object()
# 
#         if up:
#             talk.votes.up(request.user.id)
#         else:
#             talk.votes.down(request.user.id)
# 
#         return Response({'votes_count': talk.votes.count()}, status=status.HTTP_200_OK)
# 
#     @detail_route(methods=['put'])
#     def upvote(self, request, *args, **kwargs):
#         return self.vote(request, up=True)
# 
#     @detail_route(methods=['put'])
#     def downvote(self, request, *args, **kwargs):
#         return self.vote(request, up=False)


# Path: talks/urls.py
from django.conf.urls import url
from rest_framework_nested import routers
from .views import MeetupsViewSet, TalksViewSet





app_name = 'talks'

meetups_router = routers.SimpleRouter()
meetups_router.register(r'meetups', MeetupsViewSet, base_name='meetups')

talks_router = routers.NestedSimpleRouter(
    parent_router=meetups_router,
    parent_prefix=r'meetups',
    lookup='meetups'
)

talks_router.register(r'talks', TalksViewSet, base_name='talks')