Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,26 @@
# ##############################################################################
from abc import ABCMeta

from django.db.models import Exists, F, OuterRef
from django.db.models.functions import Now
from django.db.models.lookups import GreaterThanOrEqual
from django.db.models.query_utils import Q

from admission.ddd.admission.doctorat.preparation.domain.model.enums import (
STATUTS_PROPOSITION_DOCTORALE_SOUMISE,
ChoixStatutPropositionDoctorale,
ChoixTypeAdmission,
)
from admission.ddd.admission.doctorat.preparation.read_view.domain.enums.tableau_bord import IndicateurTableauBordEnum
from admission.ddd.admission.doctorat.preparation.read_view.domain.enums.tableau_bord import (
IndicateurTableauBordEnum,
)
from admission.ddd.admission.doctorat.preparation.read_view.repository.i_tableau_bord import (
ITableauBordRepositoryAdmissionMixin,
)
from admission.models import DoctorateAdmission
from admission.models.functions import AddMonths
from epc.models.enums.etat_inscription import EtatInscriptionFormation
from epc.models.inscription_programme_annuel import InscriptionProgrammeAnnuel


class TableauBordRepositoryAdmissionMixin(ITableauBordRepositoryAdmissionMixin, metaclass=ABCMeta):
Expand All @@ -47,8 +57,34 @@ class TableauBordRepositoryAdmissionMixin(ITableauBordRepositoryAdmissionMixin,
status=ChoixStatutPropositionDoctorale.INSCRIPTION_AUTORISEE.name,
type=ChoixTypeAdmission.PRE_ADMISSION.name,
),
# IndicateurTableauBordEnum.PRE_ADMISSION_PAS_EN_ORDRE_INSCRIPTION.name: Q(),
# IndicateurTableauBordEnum.PRE_ADMISSION_ECHEANCE_3_MOIS.name: Q(),
IndicateurTableauBordEnum.PRE_ADMISSION_PAS_EN_ORDRE_INSCRIPTION.name: Q(
type=ChoixTypeAdmission.PRE_ADMISSION.name,
status=ChoixStatutPropositionDoctorale.INSCRIPTION_AUTORISEE.name,
)
& Q(
~Exists(
InscriptionProgrammeAnnuel.objects.filter(
admission_uuid=OuterRef('uuid'),
etat_inscription=EtatInscriptionFormation.INSCRIT_AU_ROLE.name,
)
),
),
IndicateurTableauBordEnum.PRE_ADMISSION_ECHEANCE_3_MOIS.name: Q(
type=ChoixTypeAdmission.PRE_ADMISSION.name,
approved_by_cdd_at__isnull=False,
)
& Q(
GreaterThanOrEqual(
Now(),
AddMonths(F('approved_by_cdd_at'), months=9),
),
~Exists(
DoctorateAdmission.objects.filter(
related_pre_admission_id=OuterRef('pk'),
status__in=STATUTS_PROPOSITION_DOCTORALE_SOUMISE,
)
),
),
IndicateurTableauBordEnum.ADMISSION_DOSSIER_SOUMIS.name: Q(
status=ChoixStatutPropositionDoctorale.CONFIRMEE.name,
type=ChoixTypeAdmission.ADMISSION.name,
Expand All @@ -57,5 +93,16 @@ class TableauBordRepositoryAdmissionMixin(ITableauBordRepositoryAdmissionMixin,
status=ChoixStatutPropositionDoctorale.INSCRIPTION_AUTORISEE.name,
type=ChoixTypeAdmission.ADMISSION.name,
),
# IndicateurTableauBordEnum.ADMISSION_PAS_EN_ORDRE_INSCRIPTION.name: Q(),
IndicateurTableauBordEnum.ADMISSION_PAS_EN_ORDRE_INSCRIPTION.name: Q(
type=ChoixTypeAdmission.ADMISSION.name,
status=ChoixStatutPropositionDoctorale.INSCRIPTION_AUTORISEE.name,
)
& Q(
~Exists(
InscriptionProgrammeAnnuel.objects.filter(
admission_uuid=OuterRef('uuid'),
etat_inscription=EtatInscriptionFormation.INSCRIT_AU_ROLE.name,
)
),
),
}
15 changes: 14 additions & 1 deletion models/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# The core business involves the administration of students, teachers,
# courses, programs and so on.
#
# Copyright (C) 2015-2023 Université catholique de Louvain (http://www.uclouvain.be)
# Copyright (C) 2015-2025 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
Expand All @@ -33,3 +33,16 @@ class ToChar(models.Func):

class ArrayLength(models.Func):
function = 'CARDINALITY'


class AddMonths(models.Func):
"""
Adds a specific interval of months to a datetime value
"""

function = 'INTERVAL'
template = "%(expressions)s + %(function)s '%(months)s MONTH'"
output_field = models.DateTimeField()

def __init__(self, expression, months, **kwargs):
super().__init__(expression, months=months, **kwargs)
51 changes: 51 additions & 0 deletions tests/contrib/models/test_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# ##############################################################################
#
# 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-2025 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/.
#
# ##############################################################################
import datetime

from django.contrib.auth.models import User
from django.db.models import F
from django.test import TestCase

from admission.models.functions import AddMonths
from base.tests.factories.user import UserFactory


class FunctionsTestCase(TestCase):
def test_add_months(self):
created_user = UserFactory(
date_joined=datetime.datetime(2020, 1, 1),
)
user = User.objects.annotate(
date_joined_plus_0_month=AddMonths(F('date_joined'), months=0),
date_joined_plus_1_month=AddMonths(F('date_joined'), months=1),
date_joined_plus_5_months=AddMonths(F('date_joined'), months=5),
date_joined_plus_12_months=AddMonths(F('date_joined'), months=12),
).get(pk=created_user.pk)

self.assertEqual(user.date_joined_plus_0_month, datetime.datetime(2020, 1, 1))
self.assertEqual(user.date_joined_plus_1_month, datetime.datetime(2020, 2, 1))
self.assertEqual(user.date_joined_plus_5_months, datetime.datetime(2020, 6, 1))
self.assertEqual(user.date_joined_plus_12_months, datetime.datetime(2021, 1, 1))