# 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'
# 
# 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')
# 
# 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: exams/models.py
# class Exam(models.Model):
#     subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
#     date = models.DateField(auto_now=False, validators=[validate_date])
#     clazz = models.ForeignKey(Class, on_delete=models.CASCADE)
#     topic = models.CharField(max_length=60)
#     details = models.TextField(max_length=10000, blank=True)
#     author = models.ForeignKey(Teacher, null=True, related_name='exams', on_delete=models.CASCADE)
# 
#     def __str__(self):
#         return '{} - {} ({})'.format(self.subject, self.clazz, self.date)
# 
#     class Meta:
#         ordering = ['date', 'subject', 'clazz']


from django.contrib.auth.models import User
from rest_framework import serializers
from rest_framework.generics import get_object_or_404
from students.models import Class
from students.serializers import ClassSerializer, SubjectSerializer, TeacherAuthorSerializer
from students.utils import send_creation_email
from .models import Exam





class ExamSerializer(serializers.ModelSerializer):
    clazz = ClassSerializer()
    topic = serializers.CharField(required=True, max_length=60)
    details = serializers.CharField(max_length=10000, allow_blank=True, required=False)

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

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

        author = request.user.teacher
        subject = author.subject

# 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'
# 
# 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')
# 
# 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: exams/models.py
# class Exam(models.Model):
#     subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
#     date = models.DateField(auto_now=False, validators=[validate_date])
#     clazz = models.ForeignKey(Class, on_delete=models.CASCADE)
#     topic = models.CharField(max_length=60)
#     details = models.TextField(max_length=10000, blank=True)
#     author = models.ForeignKey(Teacher, null=True, related_name='exams', on_delete=models.CASCADE)
# 
#     def __str__(self):
#         return '{} - {} ({})'.format(self.subject, self.clazz, self.date)
# 
#     class Meta:
#         ordering = ['date', 'subject', 'clazz']


# Path: exams/serializers.py
from django.contrib.auth.models import User
from rest_framework import serializers
from rest_framework.generics import get_object_or_404
from students.models import Class
from students.serializers import ClassSerializer, SubjectSerializer, TeacherAuthorSerializer
from students.utils import send_creation_email
from .models import Exam






class ExamSerializer(serializers.ModelSerializer):
    clazz = ClassSerializer()
    topic = serializers.CharField(required=True, max_length=60)
    details = serializers.CharField(max_length=10000, allow_blank=True, required=False)

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

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

        author = request.user.teacher
        subject = author.subject

        clazz = get_object_or_404(Class, **validated_data.pop('clazz'))