# Path: students/serializers.py
# class UserInfoSerializer(serializers.ModelSerializer):
#     username = serializers.CharField(min_length=3, max_length=30)
#     first_name = serializers.CharField(min_length=3, max_length=30)
#     last_name = serializers.CharField(min_length=3, max_length=30)
#     email = serializers.EmailField(read_only=True, max_length=100)
# 
#     class Meta:
#         model = User
#         fields = ('id', 'username', 'first_name', 'last_name', 'email')
# 
#     def validate_username(self, value):
#         if self.instance and User.objects.exclude(pk=self.instance.pk).filter(username=value):
#             raise serializers.ValidationError('User with this username already exists.')
# 
#         return value
# 
# 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']
# 
# class Talk(VoteModel):
#     meetup = models.ForeignKey(Meetup, related_name='talks', null=True, on_delete=models.CASCADE)
#     author = models.ForeignKey(User, related_name='talks', on_delete=models.CASCADE)
#     topic = models.CharField(max_length=500)
#     description = models.CharField(max_length=10000)
#     video_url = models.URLField(blank=True, null=True)
# 
#     def __str__(self):
#         return '{} - {}'.format(self.__class__.__name__, self.topic)
# 
#     class Meta:
#         ordering = ['-vote_score']


from rest_framework import serializers
from students.serializers import UserInfoSerializer
from .models import Meetup, Talk
    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)


class MeetupSerializer(serializers.ModelSerializer):
    description = serializers.CharField(required=False, max_length=10000)
    talks = TalkSerializer(many=True, read_only=True)

    class Meta:

# Path: students/serializers.py
# class UserInfoSerializer(serializers.ModelSerializer):
#     username = serializers.CharField(min_length=3, max_length=30)
#     first_name = serializers.CharField(min_length=3, max_length=30)
#     last_name = serializers.CharField(min_length=3, max_length=30)
#     email = serializers.EmailField(read_only=True, max_length=100)
# 
#     class Meta:
#         model = User
#         fields = ('id', 'username', 'first_name', 'last_name', 'email')
# 
#     def validate_username(self, value):
#         if self.instance and User.objects.exclude(pk=self.instance.pk).filter(username=value):
#             raise serializers.ValidationError('User with this username already exists.')
# 
#         return value
# 
# 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']
# 
# class Talk(VoteModel):
#     meetup = models.ForeignKey(Meetup, related_name='talks', null=True, on_delete=models.CASCADE)
#     author = models.ForeignKey(User, related_name='talks', on_delete=models.CASCADE)
#     topic = models.CharField(max_length=500)
#     description = models.CharField(max_length=10000)
#     video_url = models.URLField(blank=True, null=True)
# 
#     def __str__(self):
#         return '{} - {}'.format(self.__class__.__name__, self.topic)
# 
#     class Meta:
#         ordering = ['-vote_score']


# Path: talks/serializers.py
from rest_framework import serializers
from students.serializers import UserInfoSerializer
from .models import Meetup, Talk

    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)


class MeetupSerializer(serializers.ModelSerializer):
    description = serializers.CharField(required=False, max_length=10000)
    talks = TalkSerializer(many=True, read_only=True)

    class Meta:

        model = Meetup