Skip to content
Snippets Groups Projects
serializers.py 3.68 KiB
Newer Older
  • Learn to ignore specific revisions
  • from rest_framework import serializers
    
    Bodor Máté's avatar
    Bodor Máté committed
    from account import models
    
    from common.middleware import CurrentUserMiddleware
    
    Bodor Máté's avatar
    Bodor Máté committed
    class ChoiceSerializer(serializers.ModelSerializer):
        class Meta:
            model = models.GroupChoice
    
            fields = ('choice', 'profile')
    
    class ProfileSerializer_User(serializers.ModelSerializer):
    
        groups = serializers.SlugRelatedField(many=True, slug_field='choice', queryset=models.GroupChoice.objects.all())
    
        updated_at = serializers.DateTimeField(read_only=True)
    
        full_name = serializers.SerializerMethodField()
    
    Bodor Máté's avatar
    Bodor Máté committed
            model = models.Profile
    
            read_only_fields = ('id', 'join_date', 'updated_at', 'full_name', )
    
            fields = (
                'id',
                'join_date',
                'updated_at',
                'nick',
                'signed',
                'groups',
                'motivation_about',
                'motivation_profession',
                'motivation_exercise',
    
    Bodor Máté's avatar
    Bodor Máté committed
                'full_name',
                'role',
    
    rlacko's avatar
    rlacko committed
                'score',
    
        def validate_updated_at(self, value):
    
            deadline = models.Deadline.get_solo().deadline
    
            if deadline is not None and value > deadline:
    
                raise serializers.ValidationError("You cannot join after the deadline")
    
            return value
    
        def validate_role(self, value):
    
            modifier_role = CurrentUserMiddleware.get_current_user_profile().role
    
            if value != modifier_role:
    
                raise serializers.ValidationError("You don't have permission change role")
            return value
    
        def validate_signed(self, value):
    
    Bodor Máté's avatar
    Bodor Máté committed
            modifier = CurrentUserMiddleware.get_current_user_profile()
    
            if value is False:
    
                raise serializers.ValidationError("You cannot join without signed")
            return value
    
    
        def update(self, instance, validated_data):
            new_role = validated_data.get('role', instance.role)
            if instance.role != new_role:
                if new_role == 'Student':
    
    Bodor Máté's avatar
    Bodor Máté committed
                    email.admitted(instance.user)
    
    Bodor Máté's avatar
    Bodor Máté committed
                    email.denied(instance.user)
    
            return super().update(instance, validated_data)
    
    
        def get_full_name(self, obj):
            return obj.full_name
    
    
    class ProfileSerializer_Staff(serializers.ModelSerializer):
        groups = serializers.SlugRelatedField(many=True, slug_field='choice', queryset=models.GroupChoice.objects.all())
        updated_at = serializers.DateTimeField(read_only=True)
        full_name = serializers.SerializerMethodField()
    
        class Meta:
            model = models.Profile
            read_only_fields = ('id', 'join_date', 'updated_at', 'full_name', )
            fields = (
                'id',
                'join_date',
                'updated_at',
                'nick',
                'signed',
                'groups',
                'motivation_about',
                'motivation_profession',
                'motivation_exercise',
                'full_name',
                'role',
    
    rlacko's avatar
    rlacko committed
                'score',
    
            )
    
        def validate_updated_at(self, value):
            deadline = models.Deadline.get_solo().deadline
            if deadline is not None and value > deadline:
                raise serializers.ValidationError("You cannot join after the deadline")
            return value
    
        def validate_role(self, value):
            return value
    
        def validate_signed(self, value):
            return value
    
        def update(self, instance, validated_data):
            new_role = validated_data.get('role', instance.role)
            if instance.role != new_role:
                if new_role == 'Student':
                    email.admitted(instance.user)
                if new_role == 'Denied':
                    email.denied(instance.user)
            return super().update(instance, validated_data)
    
        def get_full_name(self, obj):
            return obj.full_name