# 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: news/models.py
# class News(AbstractPost):
#     title = models.CharField(max_length=100, blank=False)
#     content = models.TextField(max_length=10000, blank=False)
#     class_number = models.IntegerField(
#         default=8,
#         validators=[Class.CLASS_NUMBER_VALIDATORS],
#         choices=Class.CLASS_NUMBERS
#     )
#     class_letter = models.CharField(
#         max_length=1,
#         blank=True,
#         choices=Class.CLASS_LETTERS
#     )
# 
#     def __str__(self):
#         return '{} ({})'.format(self.title, self.posted_on.date())
# 
#     class Meta:
#         ordering = ['-last_edited_on']
#         verbose_name_plural = 'news'
# 
# class Comment(AbstractPost):
#     news = models.ForeignKey(News, related_name='comments', on_delete=models.CASCADE)
#     author_image = models.URLField(blank=True)
#     content = models.TextField(max_length=2048)
# 
#     def __str__(self):
#         return '{} - {}'.format(self.author, self.news)
# 
#     class Meta:
#         ordering = ['-posted_on']


from django.contrib.auth.models import User
from rest_framework import serializers
from students.serializers import UserInfoSerializer
from .models import News, Comment

    def get_author_image(self, obj):
        author = obj.author

        try:
            field = author.student
        except AttributeError:
            field = author.teacher

        return field.profile_image_url

    def create(self, validated_data):
        news = self.context['news']
        request = self.context['request']

        author = request.user

        return Comment.objects.create(news=news, author=author, **validated_data)


class CommentReadSerializer(CommentSerializer):
    author = UserInfoSerializer(read_only=True)


class NewsSerializer(AbstractPostSerializer):
    title = serializers.CharField(min_length=3, max_length=100)
    content = serializers.CharField(min_length=5, max_length=10000)
    comments = CommentSerializer(read_only=True, many=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: news/models.py
# class News(AbstractPost):
#     title = models.CharField(max_length=100, blank=False)
#     content = models.TextField(max_length=10000, blank=False)
#     class_number = models.IntegerField(
#         default=8,
#         validators=[Class.CLASS_NUMBER_VALIDATORS],
#         choices=Class.CLASS_NUMBERS
#     )
#     class_letter = models.CharField(
#         max_length=1,
#         blank=True,
#         choices=Class.CLASS_LETTERS
#     )
# 
#     def __str__(self):
#         return '{} ({})'.format(self.title, self.posted_on.date())
# 
#     class Meta:
#         ordering = ['-last_edited_on']
#         verbose_name_plural = 'news'
# 
# class Comment(AbstractPost):
#     news = models.ForeignKey(News, related_name='comments', on_delete=models.CASCADE)
#     author_image = models.URLField(blank=True)
#     content = models.TextField(max_length=2048)
# 
#     def __str__(self):
#         return '{} - {}'.format(self.author, self.news)
# 
#     class Meta:
#         ordering = ['-posted_on']


# Path: news/serializers.py
from django.contrib.auth.models import User
from rest_framework import serializers
from students.serializers import UserInfoSerializer
from .models import News, Comment


    def get_author_image(self, obj):
        author = obj.author

        try:
            field = author.student
        except AttributeError:
            field = author.teacher

        return field.profile_image_url

    def create(self, validated_data):
        news = self.context['news']
        request = self.context['request']

        author = request.user

        return Comment.objects.create(news=news, author=author, **validated_data)


class CommentReadSerializer(CommentSerializer):
    author = UserInfoSerializer(read_only=True)


class NewsSerializer(AbstractPostSerializer):
    title = serializers.CharField(min_length=3, max_length=100)
    content = serializers.CharField(min_length=5, max_length=10000)
    comments = CommentSerializer(read_only=True, many=True)

    class Meta:

        model = News