# Path: students/models.py
# class Class(models.Model):
#     CLASS_NUMBERS = (
#         (8, 8),
#         (9, 9),
#         (10, 10),
#         (11, 11),
#         (12, 12),
#     )
#     CLASS_NUMBER_VALIDATORS = [MinValueValidator(8), MaxValueValidator(12)]
# 
#     CLASS_LETTERS = (
#         ('A', 'A'),
#         ('B', 'B'),
#         ('V', 'V'),
#         ('G', 'G'),
#     )
# 
#     number = models.IntegerField(validators=CLASS_NUMBER_VALIDATORS, choices=CLASS_NUMBERS)
#     letter = models.CharField(max_length=1, choices=CLASS_LETTERS)
# 
#     def __str__(self):
#         return '{}{}'.format(self.number, self.letter)
# 
#     class Meta:
#         ordering = ['number', 'letter']
#         verbose_name_plural = 'classes'
# 
# class Subject(models.Model):
#     title = models.CharField(unique=True, max_length=50)
# 
#     def __str__(self):
#         return self.title
# 
#     class Meta:
#         ordering = ['title']
# 
# class Student(Account):
#     clazz = models.ForeignKey(Class, related_name='students', on_delete=models.CASCADE)
# 
#     def __str__(self):
#         return '{} ({})'.format(self.user.username, self.clazz)
# 
# class Teacher(Account):
#     subject = models.ForeignKey(Subject, related_name='teachers', on_delete=models.CASCADE)
# 
#     def __str__(self):
#         return '{} ({})'.format(self.user.username, self.subject)
# 
# 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
# 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
#         fields = AbstractPostSerializer.Meta.fields + (
#             'id', 'title', 'content', 'class_number', 'class_letter', 'comments'
#         )
# 
#     def create(self, validated_data):
#         author = self.context['request'].user
#         validated_data['class_number'] = self.context['class_number']
#         validated_data['class_letter'] = self.context.get('class_letter', '')
# 
#         news = News.objects.create(author=author, **validated_data)
# 
#         return news
# 
# class CommentSerializer(AbstractPostSerializer):
#     author_image = serializers.SerializerMethodField()
#     content = serializers.CharField(max_length=2048)
# 
#     class Meta:
#         model = Comment
#         fields = AbstractPostSerializer.Meta.fields + ('id', 'author_image', 'content')
# 
#     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)


from django.contrib.auth.models import User
from rest_framework.test import APITestCase, APIClient
from rest_framework.reverse import reverse
from rest_framework import status
from students.models import Class, Subject, Student, Teacher
from .models import News, Comment
from .serializers import NewsSerializer, CommentSerializer





class NewsStudentsViewSetTestCase(APITestCase):
    def setUp(self):
        self.client = APIClient()
        self.list_view_name = 'news:students_news-list'
        self.detail_view_name = 'news:students_news-detail'

        self.user = User(username='test', email='test@test.com')
        self.user.set_password('password123')
        self.user.save()

        self.clazz = Class.objects.create(number=10, letter='A')
        self.student = Student.objects.create(user=self.user, clazz=self.clazz)
        self.news = News.objects.create(
            title='test news',
            content='test news content',
            class_number=self.clazz.number,
            class_letter=self.clazz.letter,
            author=self.user,
        )

# Path: students/models.py
# class Class(models.Model):
#     CLASS_NUMBERS = (
#         (8, 8),
#         (9, 9),
#         (10, 10),
#         (11, 11),
#         (12, 12),
#     )
#     CLASS_NUMBER_VALIDATORS = [MinValueValidator(8), MaxValueValidator(12)]
# 
#     CLASS_LETTERS = (
#         ('A', 'A'),
#         ('B', 'B'),
#         ('V', 'V'),
#         ('G', 'G'),
#     )
# 
#     number = models.IntegerField(validators=CLASS_NUMBER_VALIDATORS, choices=CLASS_NUMBERS)
#     letter = models.CharField(max_length=1, choices=CLASS_LETTERS)
# 
#     def __str__(self):
#         return '{}{}'.format(self.number, self.letter)
# 
#     class Meta:
#         ordering = ['number', 'letter']
#         verbose_name_plural = 'classes'
# 
# class Subject(models.Model):
#     title = models.CharField(unique=True, max_length=50)
# 
#     def __str__(self):
#         return self.title
# 
#     class Meta:
#         ordering = ['title']
# 
# class Student(Account):
#     clazz = models.ForeignKey(Class, related_name='students', on_delete=models.CASCADE)
# 
#     def __str__(self):
#         return '{} ({})'.format(self.user.username, self.clazz)
# 
# class Teacher(Account):
#     subject = models.ForeignKey(Subject, related_name='teachers', on_delete=models.CASCADE)
# 
#     def __str__(self):
#         return '{} ({})'.format(self.user.username, self.subject)
# 
# 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
# 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
#         fields = AbstractPostSerializer.Meta.fields + (
#             'id', 'title', 'content', 'class_number', 'class_letter', 'comments'
#         )
# 
#     def create(self, validated_data):
#         author = self.context['request'].user
#         validated_data['class_number'] = self.context['class_number']
#         validated_data['class_letter'] = self.context.get('class_letter', '')
# 
#         news = News.objects.create(author=author, **validated_data)
# 
#         return news
# 
# class CommentSerializer(AbstractPostSerializer):
#     author_image = serializers.SerializerMethodField()
#     content = serializers.CharField(max_length=2048)
# 
#     class Meta:
#         model = Comment
#         fields = AbstractPostSerializer.Meta.fields + ('id', 'author_image', 'content')
# 
#     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)


# Path: news/tests.py
from django.contrib.auth.models import User
from rest_framework.test import APITestCase, APIClient
from rest_framework.reverse import reverse
from rest_framework import status
from students.models import Class, Subject, Student, Teacher
from .models import News, Comment
from .serializers import NewsSerializer, CommentSerializer






class NewsStudentsViewSetTestCase(APITestCase):
    def setUp(self):
        self.client = APIClient()
        self.list_view_name = 'news:students_news-list'
        self.detail_view_name = 'news:students_news-detail'

        self.user = User(username='test', email='test@test.com')
        self.user.set_password('password123')
        self.user.save()

        self.clazz = Class.objects.create(number=10, letter='A')
        self.student = Student.objects.create(user=self.user, clazz=self.clazz)
        self.news = News.objects.create(
            title='test news',
            content='test news content',
            class_number=self.clazz.number,
            class_letter=self.clazz.letter,
            author=self.user,
        )

        self.comment = Comment.objects.create(