uclouvain/osis-dissertation

View on GitHub
api/serializers/dissertation.py

Summary

Maintainability
A
0 mins
Test Coverage
##############################################################################
#
#    OSIS stands for Open Student Information System. It's an application
#    designed to manage the core business of higher education institutions,
#    such as universities, faculties, institutes and professional schools.
#    The core business involves the administration of students, teachers,
#    courses, programs and so on.
#
#    Copyright (C) 2015-2021 Université catholique de Louvain (http://www.uclouvain.be)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    A copy of this license - GNU General Public License - is available
#    at the root of the source code of this program.  If not,
#    see http://www.gnu.org/licenses/.
#
##############################################################################
from django.db import models
from django.utils import timezone
from rest_framework import serializers

from base.models.education_group_year import EducationGroupYear as EducationGroupYearDB
from dissertation.models import dissertation_role
from dissertation.models.dissertation import Dissertation
from dissertation.models.dissertation_document_file import DissertationDocumentFile
from dissertation.models.dissertation_location import DissertationLocation as DissertationLocationDB
from dissertation.models.adviser import Adviser as AdviserDB
from dissertation.models.enums.defend_periodes import DefendPeriodes
from dissertation.models.enums.dissertation_role_status import DissertationRoleStatus
from dissertation.models.proposition_dissertation import PropositionDissertation as PropositionDissertationDB


class DissertationListSerializer(serializers.Serializer):
    uuid = serializers.CharField(read_only=True)
    title = serializers.CharField(default='', read_only=True)
    author = serializers.CharField(default='', read_only=True)
    status = serializers.CharField(read_only=True)
    status_text = serializers.CharField(source='get_status_display', read_only=True)
    offer_acronym = serializers.CharField(source='education_group_year.acronym', read_only=True)
    start_year = serializers.IntegerField(source='education_group_year.academic_year.year', read_only=True)
    dissertation_subject = serializers.CharField(source='proposition_dissertation', read_only=True)


class DissertationCreateSerializer(serializers.Serializer):
    proposition_dissertation_uuid = serializers.CharField(required=True)
    title = serializers.CharField(required=True)
    description = serializers.CharField(required=True, allow_blank=True)
    defend_year = serializers.IntegerField(required=True)
    defend_period = serializers.ChoiceField(required=True, choices=DefendPeriodes.choices())
    location_uuid = serializers.CharField(required=True)
    acronym = serializers.CharField(required=True)
    year = serializers.IntegerField(required=True)

    def validate_proposition_dissertation_uuid(self, proposition_dissertation_uuid: str):
        try:
            obj = PropositionDissertationDB.objects.get(uuid=proposition_dissertation_uuid)
            return obj.pk
        except PropositionDissertationDB.DoesNotExist:
            raise serializers.ValidationError("Not found")

    def validate_location_uuid(self, location_uuid: str):
        try:
            obj = DissertationLocationDB.objects.get(uuid=location_uuid)
            return obj.pk
        except DissertationLocationDB.DoesNotExist:
            raise serializers.ValidationError("Not found")

    def validate_education_group_year_uuid(self, education_group_year_uuid: str):
        try:
            obj = EducationGroupYearDB.objects.get(uuid=education_group_year_uuid)
            return obj.pk
        except EducationGroupYearDB.DoesNotExist:
            raise serializers.ValidationError("Not found")


class DissertationUpdateSerializer(serializers.Serializer):
    title = serializers.CharField(required=True)
    description = serializers.CharField(required=True, allow_blank=True)
    defend_year = serializers.IntegerField(required=True)
    defend_period = serializers.ChoiceField(required=True, choices=DefendPeriodes.choices())
    location_uuid = serializers.CharField(required=True)

    def validate_location_uuid(self, location_uuid: str):
        try:
            obj = DissertationLocationDB.objects.get(uuid=location_uuid)
            return obj.pk
        except DissertationLocationDB.DoesNotExist:
            raise serializers.ValidationError("Not found")


class DissertationAuthorSerializer(serializers.Serializer):
    first_name = serializers.CharField(default='', read_only=True)
    last_name = serializers.CharField(default='', read_only=True)
    middle_name = serializers.CharField(default='', read_only=True)


class DissertationLocation(serializers.Serializer):
    uuid = serializers.CharField(read_only=True)
    name = serializers.CharField(read_only=True)


class DissertationJurySerializer(serializers.Serializer):
    uuid = serializers.CharField(read_only=True)
    status = serializers.CharField(read_only=True)
    status_text = serializers.CharField(source='get_status_display', read_only=True)
    adviser = serializers.CharField(default='', read_only=True)


class DissertationLinkSerializer(serializers.Serializer):
    document_url = serializers.SerializerMethodField()
    delete_document_url = serializers.SerializerMethodField()

    def get_document_url(self, obj) -> str:
        return ''

    def get_delete_document_url(self, obj) -> str:
        return ''


class DissertationDetailSerializer(serializers.Serializer):
    uuid = serializers.CharField(read_only=True)
    proposition_uuid = serializers.CharField(read_only=True, source="proposition_dissertation.uuid")
    title = serializers.CharField(default='', read_only=True)
    description = serializers.CharField(default='', read_only=True)
    author = DissertationAuthorSerializer(source='author.person')
    status = serializers.CharField(read_only=True)
    status_text = serializers.CharField(source='get_status_display', read_only=True)
    defend_period = serializers.CharField(read_only=True, source='defend_periode')
    defend_period_text = serializers.CharField(source='get_defend_periode_display', read_only=True)
    defend_year = serializers.IntegerField(read_only=True)
    location = DissertationLocation()
    jury = serializers.SerializerMethodField()
    link = DissertationLinkSerializer(source='*')

    def get_jury(self, obj: Dissertation):
        results = DissertationJurySerializer(obj.dissertationrole_set.all(), many=True).data
        if results:
            return results

        results = DissertationJurySerializer(obj.proposition_dissertation.propositionrole_set.all(), many=True).data
        if results:
            # TODO: Remove logic inside - must be done at creation
            for role in obj.proposition_dissertation.propositionrole_set.all():
                dissertation_role.add(role.status, role.adviser, obj)
            return results

        if not results:
            # TODO: Remove logic inside - must be done at creation
            dissertation_role.add(DissertationRoleStatus.PROMOTEUR.name, obj.proposition_dissertation.author, obj)
            return [{
                'status': DissertationRoleStatus.PROMOTEUR.name,
                'status_text': DissertationRoleStatus.PROMOTEUR.value,
                'adviser': str(obj.author)
            }]
        return results


class DissertationHistoryListSerializer(serializers.Serializer):
    status_from = serializers.CharField(read_only=True)
    status_from_text = serializers.CharField(read_only=True, source="get_status_from_display")
    status_to = serializers.CharField(read_only=True)
    status_to_text = serializers.CharField(read_only=True, source="get_status_to_display")
    author = serializers.CharField(read_only=True, source="person")
    created_at = serializers.DateTimeField(read_only=True, format="%d-%m-%Y %H:%M:%S", source="created")
    justification = serializers.CharField(read_only=True)


class DissertationJuryAddSerializer(serializers.Serializer):
    adviser_uuid = serializers.CharField(required=True)

    def validate_adviser_uuid(self, adviser_uuid: str):
        try:
            obj = AdviserDB.objects.get(uuid=adviser_uuid)
            return obj.pk
        except AdviserDB.DoesNotExist:
            raise serializers.ValidationError("Not found")


class DissertationSubmitSerializer(serializers.Serializer):
    justification = serializers.CharField(read_only=True)


class DissertationBackToDraftSerializer(serializers.Serializer):
    justification = serializers.CharField(read_only=True)


class DissertationCanManageJurySerializer(serializers.Serializer):
    can_manage_jury_members = serializers.BooleanField(read_only=True, source="student_can_manage_readers")


class DissertationCanEditDissertationSerializer(serializers.Serializer):
    can_edit_dissertation = serializers.SerializerMethodField(read_only=True)

    @staticmethod
    def get_can_edit_dissertation(obj):
        if obj:
            return obj.start_edit_title <= timezone.now().date() <= obj.end_edit_title
        return False


class DissertationFileSerializer(serializers.ModelSerializer):
    serializer_field_mapping = serializers.ModelSerializer.serializer_field_mapping
    serializer_field_mapping[models.UUIDField] = serializers.CharField

    class Meta:
        model = Dissertation
        fields = [
            'dissertation_file',
        ]