# Path: students/serializers.py
# class ClassSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = Class
#         fields = ('id', 'number', 'letter')
# 
# class SubjectSerializer(serializers.ModelSerializer):
#     title = serializers.CharField(
#         max_length=50,
#         allow_blank=False,
#         validators=[
#             UniqueValidator(
#                 queryset=Subject.objects.all(),
#                 message='Subject with this title already exists.'
#             )
#         ]
#     )
# 
#     class Meta:
#         model = Subject
#         fields = ('id', 'title')
# 
# class TeacherAuthorSerializer(DefaultAuthorSerializer):
#     class Meta:
#         model = Teacher
#         fields = ('id', 'user', 'subject')
# 
# class StudentAuthorSerializer(DefaultAuthorSerializer):
#     clazz = ClassSerializer(read_only=True)
# 
#     class Meta:
#         model = Student
#         fields = ('id', 'user', 'clazz')
# 
# Path: students/utils.py
# def send_creation_email(user, model):
#     model_type = model.__class__.__name__.lower()
#     client_resource_link = '{model_type}s/{id}/'.format(model_type=model_type, id=model.id)
# 
#     template_context = {
#         'full_name': user.get_full_name(),
#         'type': model_type,
#         'model': model,
#         'author': model.author,
#         'link': BASE_CLIENT_URL + client_resource_link
#     }
#     html_content = render_to_string('utils/email.html', context=template_context)
#     text_content = strip_tags(html_content)
# 
#     subject = 'ELSYSER {resource} added'.format(resource=model_type)
# 
#     msg = EmailMultiAlternatives(
#         subject,
#         text_content,
#         settings.DEFAULT_FROM_EMAIL,
#         [user.email]
#     )
#     msg.attach_alternative(html_content, "text/html")
#     msg.send()
# 
# Path: homeworks/models.py
# class Homework(models.Model):
#     topic = models.CharField(default='Homework', max_length=50)
#     subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
#     clazz = models.ForeignKey(Class, on_delete=models.CASCADE)
#     deadline = models.DateField(auto_now=False)
#     details = models.TextField(max_length=256, blank=True)
#     author = models.ForeignKey(Teacher, null=True, related_name='homeworks', on_delete=models.CASCADE)
# 
#     def __str__(self):
#         return '{} ({}) - {}'.format(self.topic, self.subject, self.clazz)
# 
#     class Meta:
#         ordering = ['deadline', 'clazz', 'subject']
# 
# class Submission(BaseAbstractPost):
#     homework = models.ForeignKey(Homework, related_name='submissions', on_delete=models.CASCADE)
#     student = models.ForeignKey(Student, related_name='submissions', on_delete=models.CASCADE)
#     content = models.TextField(max_length=2048)
#     solution_url = models.URLField(blank=True)
#     checked = models.BooleanField(default=False)
# 
#     def __str__(self):
#         return '{} - {} ({})'.format(self.student, self.homework, self.posted_on)
# 
#     class Meta:
#         ordering = ['-posted_on', '-last_edited_on']


from django.contrib.auth.models import User
from rest_framework import serializers
from students.serializers import (
    ClassSerializer, SubjectSerializer, TeacherAuthorSerializer, StudentAuthorSerializer
)
from students.utils import send_creation_email
from .models import Homework, Submission
class HomeworkSerializer(serializers.ModelSerializer):
    details = serializers.CharField(max_length=256, allow_blank=True)

    class Meta:
        model = Homework
        fields = ('id', 'topic', 'subject', 'clazz', 'deadline', 'details', 'author')
        depth = 1

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

        author = request.user.teacher
        subject = author.subject
        clazz = self.context['clazz']

        homework = Homework.objects.create(
            subject=subject,
            author=author,
            clazz=clazz,
            **validated_data
        )

        recipient_list = User.objects.filter(student__clazz=clazz)
        for user in recipient_list:
            send_creation_email(user, model=homework)

        return homework


class HomeworkReadSerializer(HomeworkSerializer):

# Path: students/serializers.py
# class ClassSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = Class
#         fields = ('id', 'number', 'letter')
# 
# class SubjectSerializer(serializers.ModelSerializer):
#     title = serializers.CharField(
#         max_length=50,
#         allow_blank=False,
#         validators=[
#             UniqueValidator(
#                 queryset=Subject.objects.all(),
#                 message='Subject with this title already exists.'
#             )
#         ]
#     )
# 
#     class Meta:
#         model = Subject
#         fields = ('id', 'title')
# 
# class TeacherAuthorSerializer(DefaultAuthorSerializer):
#     class Meta:
#         model = Teacher
#         fields = ('id', 'user', 'subject')
# 
# class StudentAuthorSerializer(DefaultAuthorSerializer):
#     clazz = ClassSerializer(read_only=True)
# 
#     class Meta:
#         model = Student
#         fields = ('id', 'user', 'clazz')
# 
# Path: students/utils.py
# def send_creation_email(user, model):
#     model_type = model.__class__.__name__.lower()
#     client_resource_link = '{model_type}s/{id}/'.format(model_type=model_type, id=model.id)
# 
#     template_context = {
#         'full_name': user.get_full_name(),
#         'type': model_type,
#         'model': model,
#         'author': model.author,
#         'link': BASE_CLIENT_URL + client_resource_link
#     }
#     html_content = render_to_string('utils/email.html', context=template_context)
#     text_content = strip_tags(html_content)
# 
#     subject = 'ELSYSER {resource} added'.format(resource=model_type)
# 
#     msg = EmailMultiAlternatives(
#         subject,
#         text_content,
#         settings.DEFAULT_FROM_EMAIL,
#         [user.email]
#     )
#     msg.attach_alternative(html_content, "text/html")
#     msg.send()
# 
# Path: homeworks/models.py
# class Homework(models.Model):
#     topic = models.CharField(default='Homework', max_length=50)
#     subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
#     clazz = models.ForeignKey(Class, on_delete=models.CASCADE)
#     deadline = models.DateField(auto_now=False)
#     details = models.TextField(max_length=256, blank=True)
#     author = models.ForeignKey(Teacher, null=True, related_name='homeworks', on_delete=models.CASCADE)
# 
#     def __str__(self):
#         return '{} ({}) - {}'.format(self.topic, self.subject, self.clazz)
# 
#     class Meta:
#         ordering = ['deadline', 'clazz', 'subject']
# 
# class Submission(BaseAbstractPost):
#     homework = models.ForeignKey(Homework, related_name='submissions', on_delete=models.CASCADE)
#     student = models.ForeignKey(Student, related_name='submissions', on_delete=models.CASCADE)
#     content = models.TextField(max_length=2048)
#     solution_url = models.URLField(blank=True)
#     checked = models.BooleanField(default=False)
# 
#     def __str__(self):
#         return '{} - {} ({})'.format(self.student, self.homework, self.posted_on)
# 
#     class Meta:
#         ordering = ['-posted_on', '-last_edited_on']


# Path: homeworks/serializers.py
from django.contrib.auth.models import User
from rest_framework import serializers
from students.serializers import (
    ClassSerializer, SubjectSerializer, TeacherAuthorSerializer, StudentAuthorSerializer
)
from students.utils import send_creation_email
from .models import Homework, Submission

class HomeworkSerializer(serializers.ModelSerializer):
    details = serializers.CharField(max_length=256, allow_blank=True)

    class Meta:
        model = Homework
        fields = ('id', 'topic', 'subject', 'clazz', 'deadline', 'details', 'author')
        depth = 1

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

        author = request.user.teacher
        subject = author.subject
        clazz = self.context['clazz']

        homework = Homework.objects.create(
            subject=subject,
            author=author,
            clazz=clazz,
            **validated_data
        )

        recipient_list = User.objects.filter(student__clazz=clazz)
        for user in recipient_list:
            send_creation_email(user, model=homework)

        return homework


class HomeworkReadSerializer(HomeworkSerializer):

    subject = SubjectSerializer(read_only=True)