# Path: students/permissions.py
# class IsUserAuthor(permissions.BasePermission):
#     message = AUTHOR_DEFAULT_MESSAGE
# 
#     def has_object_permission(self, request, view, obj):
#         return obj.author == request.user
# 
# Path: talks/serializers.py
# class MeetupSerializer(serializers.ModelSerializer):
#     description = serializers.CharField(required=False, max_length=10000)
#     talks = TalkSerializer(many=True, read_only=True)
# 
#     class Meta:
#         model = Meetup
#         fields = ('id', 'date', 'description', 'talks')
# 
# class TalkSerializer(serializers.ModelSerializer):
#     author = UserInfoSerializer(read_only=True)
#     topic = serializers.CharField(required=True, min_length=3, max_length=500)
#     description = serializers.CharField(required=True, min_length=5, max_length=10000)
#     votes_count = serializers.SerializerMethodField()
#     has_voted = serializers.SerializerMethodField()
# 
#     class Meta:
#         model = Talk
#         fields = ('id', 'author', 'topic', 'description', 'video_url', 'votes_count', 'has_voted') 
# 
#     def get_votes_count(self, obj):
#         return obj.votes.count()
# 
#     def get_has_voted(self, obj):
#         return obj.votes.exists(self.context['request'].user.id)
# 
#     def create(self, validated_data):
#         request = self.context['request']
#         author = request.user
# 
#         meetup = self.context['meetup']
# 
#         return Talk.objects.create(meetup=meetup, author=author, **validated_data)
# 
# Path: talks/filters.py
# class MeetupsFilterBackend(filters.BaseFilterBackend):
#     def filter_queryset(self, request, queryset, view):
#         only = request.query_params.get('only')
#         now = datetime.now()
# 
#         if only == 'past':
#             return queryset.filter(date__lte=now)
#         elif only == 'upcoming':
#             return queryset.filter(date__gte=now)
# 
#         return queryset
# 
# Path: talks/models.py
# class Meetup(models.Model):
#     date = models.DateTimeField()
#     description = models.CharField(max_length=10000, blank=True, null=True)
# 
#     def __str__(self):
#         return '{} ({})'.format(self.__class__.__name__, self.date)
# 
#     class Meta:
#         ordering = ['date']


from rest_framework import viewsets, status, generics
from rest_framework.response import Response
from rest_framework.decorators import detail_route
from rest_framework.permissions import IsAuthenticated, IsAdminUser
from rest_framework_word_filter import FullWordSearchFilter
from students.permissions import IsUserAuthor
from .serializers import MeetupSerializer, TalkSerializer
from .filters import MeetupsFilterBackend
from .models import Meetup



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,),

# Path: students/permissions.py
# class IsUserAuthor(permissions.BasePermission):
#     message = AUTHOR_DEFAULT_MESSAGE
# 
#     def has_object_permission(self, request, view, obj):
#         return obj.author == request.user
# 
# Path: talks/serializers.py
# class MeetupSerializer(serializers.ModelSerializer):
#     description = serializers.CharField(required=False, max_length=10000)
#     talks = TalkSerializer(many=True, read_only=True)
# 
#     class Meta:
#         model = Meetup
#         fields = ('id', 'date', 'description', 'talks')
# 
# class TalkSerializer(serializers.ModelSerializer):
#     author = UserInfoSerializer(read_only=True)
#     topic = serializers.CharField(required=True, min_length=3, max_length=500)
#     description = serializers.CharField(required=True, min_length=5, max_length=10000)
#     votes_count = serializers.SerializerMethodField()
#     has_voted = serializers.SerializerMethodField()
# 
#     class Meta:
#         model = Talk
#         fields = ('id', 'author', 'topic', 'description', 'video_url', 'votes_count', 'has_voted') 
# 
#     def get_votes_count(self, obj):
#         return obj.votes.count()
# 
#     def get_has_voted(self, obj):
#         return obj.votes.exists(self.context['request'].user.id)
# 
#     def create(self, validated_data):
#         request = self.context['request']
#         author = request.user
# 
#         meetup = self.context['meetup']
# 
#         return Talk.objects.create(meetup=meetup, author=author, **validated_data)
# 
# Path: talks/filters.py
# class MeetupsFilterBackend(filters.BaseFilterBackend):
#     def filter_queryset(self, request, queryset, view):
#         only = request.query_params.get('only')
#         now = datetime.now()
# 
#         if only == 'past':
#             return queryset.filter(date__lte=now)
#         elif only == 'upcoming':
#             return queryset.filter(date__gte=now)
# 
#         return queryset
# 
# Path: talks/models.py
# class Meetup(models.Model):
#     date = models.DateTimeField()
#     description = models.CharField(max_length=10000, blank=True, null=True)
# 
#     def __str__(self):
#         return '{} ({})'.format(self.__class__.__name__, self.date)
# 
#     class Meta:
#         ordering = ['date']


# Path: talks/views.py
from rest_framework import viewsets, status, generics
from rest_framework.response import Response
from rest_framework.decorators import detail_route
from rest_framework.permissions import IsAuthenticated, IsAdminUser
from rest_framework_word_filter import FullWordSearchFilter
from students.permissions import IsUserAuthor
from .serializers import MeetupSerializer, TalkSerializer
from .filters import MeetupsFilterBackend
from .models import Meetup




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),