serializers.py 3.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
from rest_framework import serializers, status

from courses.workspaces.serializers import SimpleWorkspaceSerializer

from courses.certification.models import (
    CertificateTemplate,
    CertificationProcess,
    Evaluation,
    CourseCertification,
)


class CertificateTemplateSerializer(serializers.ModelSerializer):
    workspace = SimpleWorkspaceSerializer(read_only=True)
    associate = serializers.SerializerMethodField()
16
    course_name = serializers.SerializerMethodField(read_only=True,)
17 18

    # TODO: Legacy compat field, remove in the future
Bruno Martin's avatar
Bruno Martin committed
19
    contract = serializers.SerializerMethodField(read_only=True)
20 21

    class Meta:
22
        model = CertificateTemplate
23
        fields = ('id', 'text', 'base_logo_url', 'cert_logo_url', 'signature_name', 'role', 'name',
24
                  'course_name', 'contract', 'associate', 'workspace', 'document_type',
25
                  'organization_name', 'site_logo_url', 'signature_url', 'course',
Bruno Martin's avatar
Bruno Martin committed
26 27
                  'second_signature_url', 'second_name', 'second_role',
                  'display_background_image',)
28 29

    def get_associate(self, obj):
30 31 32 33 34 35 36 37
        filters = {
            'document_type' : 'certificate' if obj.document_type == 'receipt' else 'receipt',
            'course': obj.course,
            'workspace': obj.workspace
        }
        if CertificateTemplate.objects.filter(**filters).exists():
            return CertificateTemplate.objects.filter(**filters).first().id
        return None
38 39

    def get_contract(self, obj):
40 41 42
        if obj.workspace:
            return SimpleWorkspaceSerializer(obj.workspace).data
        return None
43

44 45
    def get_course_name(self, obj):
        return obj.course.name
46

47 48 49 50

class CertificateTemplateCreateSerializer(serializers.ModelSerializer):
    class Meta:
        model = CertificateTemplate
51 52 53 54 55 56
        fields = (
            'name',
            'course',
            'document_type',
            'workspace',
        )
57 58 59 60 61

class CertificateTemplateImageSerializer(serializers.ModelSerializer):

    class Meta:
        model = CertificateTemplate
Bruno Martin's avatar
Bruno Martin committed
62 63 64 65 66 67 68 69
        fields = (
            'base_logo',
            'cert_logo',
            'signature',
            'site_logo',
            'second_signature',
            'background_image',
        )
70 71


72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
class CertificationProcessSerializer(serializers.ModelSerializer):
    course_certification = serializers.SlugRelatedField(slug_field="link_hash", read_only=True)

    class Meta:
        model = CertificationProcess


class BaseEvaluationSerializer(serializers.ModelSerializer):

    class Meta:
        model = Evaluation


class BaseCertificationProcessSerializer(serializers.ModelSerializer):
    evaluation = BaseEvaluationSerializer()

    class Meta:
        model = CertificationProcess


class BaseCourseCertificationSerializer(serializers.ModelSerializer):

    class Meta:
        model = CourseCertification


class CourseCertificationSerializer(serializers.ModelSerializer):
    processes = BaseCertificationProcessSerializer(many=True, read_only=True)
    approved = BaseCertificationProcessSerializer(source='get_approved_process', read_only=True)
    course = serializers.SerializerMethodField()
    url = serializers.ReadOnlyField(source='get_absolute_url')

    class Meta:
        model = CourseCertification
        fields = ('link_hash', 'created_date', 'is_valid', 'processes', 'type',
                  'approved', 'course', 'url')

    @staticmethod
    def get_course(obj):
        return obj.course.id


class EvaluationSerializer(serializers.ModelSerializer):
    processes = serializers.PrimaryKeyRelatedField(many=True, read_only=True)

    class Meta:
        model = Evaluation