Skip to content
Snippets Groups Projects
serializers.py 2.86 KiB
Newer Older
Bodor Máté's avatar
Bodor Máté committed
from rest_framework import serializers
from django.utils import timezone
from account.models import Profile
Bodor Máté's avatar
Bodor Máté committed
from . import models
Bodor Máté's avatar
Bodor Máté committed
from common import email
from common.middleware import CurrentUserMiddleware
Bodor Máté's avatar
Bodor Máté committed


class TaskSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Task
        read_only_fields = ('created_by', 'created_at', 'updated_at')
        fields = '__all__'
Bodor Máté's avatar
Bodor Máté committed

Bodor Mate's avatar
Bodor Mate committed
    def validate(self, data):
        if timezone.now() >= data['deadline']:
            raise serializers.ValidationError('Please, enter appropriate deadline.')
        return data

    def create(self, validated_data):
Bodor Máté's avatar
Bodor Máté committed
        profiles = Profile.objects.filter(role="Student").exclude(user__email='')
        for profile in profiles:
            email.new_homework(profile.user, validated_data.get('deadline'))
        return self.Meta.model.objects.create(**validated_data)

Bodor Máté's avatar
Bodor Máté committed

class SolutionSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Solution
        read_only_fields = ('created_by', 'created_at', 'updated_at', 'ready', 'files')
Bodor Máté's avatar
Bodor Máté committed
            'id',
            'task',
            'created_at',
            'updated_at',
            'accepted',
            'files',
            'created_by',
            'corrected',
            'note',
        )
    def validate_task(self, value):
        if timezone.now() > value.deadline:
            raise serializers.ValidationError('You late.')
Bodor Máté's avatar
Bodor Máté committed
    def validate_accepted(self, value):
        profile = CurrentUserMiddleware.get_current_user_profile()
Bodor Máté's avatar
Bodor Máté committed
        if profile.role != 'Staff' and value:
            raise serializers.ValidationError("You don't have permission to modify accepted!")
        return value

    def validate_corrected(self, value):
        profile = CurrentUserMiddleware.get_current_user_profile()
        if profile.role != 'Staff' and value:
            raise serializers.ValidationError("You don't have permission to modify corrected!")
        return value

    def validate_note(self, value):
        profile = CurrentUserMiddleware.get_current_user_profile()
        if profile.role != 'Staff' and value != '':
            raise serializers.ValidationError("You don't have permission to create note!")
        return value

    def update(self, instance, validated_data):
        if instance.corrected == False and validated_data.get('corrected', instance.corrected) == True:
Bodor Máté's avatar
Bodor Máté committed
            email.homework_corrected(
                instance.created_by.user,
                instance.task.title,
                validated_data.get('accepted', instance.accepted)
            )
        return super().update(instance, validated_data)

    def create(self, validated_data):
        profile = CurrentUserMiddleware.get_current_user_profile()
        models.Solution.objects.filter(created_by=profile, task=validated_data['task']).delete()
        return super().create(validated_data)
Bodor Máté's avatar
Bodor Máté committed