Skip to content

[OS-1380] Prevent to create several papers of the same type #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2025
Merged
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
6 changes: 3 additions & 3 deletions api/permissions.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-2024 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 @@ -23,9 +23,9 @@
# see http://www.gnu.org/licenses/.
#
# ##############################################################################
from osis_role.contrib.views import APIPermissionRequiredMixin
from rest_framework.permissions import BasePermission

from osis_role.contrib.views import APIPermissionRequiredMixin
from parcours_doctoral.models import ParcoursDoctoral
from parcours_doctoral.utils.cache import get_cached_parcours_doctoral_perm_obj

Expand All @@ -49,7 +49,7 @@ def has_permission(self, request, view):
class DoctorateAPIPermissionRequiredMixin(APIPermissionRequiredMixin):
@property
def doctorate_uuid(self):
return self.kwargs['uuid']
return self.kwargs.get('uuid')

def get_permission_object(self):
return get_cached_parcours_doctoral_perm_obj(self.doctorate_uuid)
2 changes: 1 addition & 1 deletion api/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from backoffice.settings.rest_framework.fields import ActionLinksField
from base.models.utils.utils import ChoiceEnum

PARCOURS_DOCTORAL_SDK_VERSION = "1.0.8"
PARCOURS_DOCTORAL_SDK_VERSION = "1.0.9"


class ParcoursDoctoralSchemaGenerator(SchemaGenerator):
Expand Down
24 changes: 24 additions & 0 deletions api/serializers/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from base.utils.serializers import DTOSerializer
from parcours_doctoral.ddd.formation.domain.model.enums import (
CategorieActivite,
ChoixTypeEpreuve,
ContexteFormation,
StatutActivite,
)
Expand Down Expand Up @@ -334,6 +335,7 @@ class UclCourseSerializer(ActivitySerializerBase):

class Meta:
form = activity_forms.UclCourseForm
exclude = ['academic_year']

def to_internal_value(self, data):
# Don't let DRF rework the data structure before calling UclCourseForm
Expand Down Expand Up @@ -448,7 +450,29 @@ class DoctoralTrainingAssentSerializer(serializers.Serializer):
commentaire = serializers.CharField(allow_blank=True)


class MultipleChoiceFieldFromMethod(serializers.SerializerMethodField, serializers.MultipleChoiceField):
"""A multiple choice field whose the value is computed via a method."""


class DoctoralTrainingConfigSerializer(serializers.ModelSerializer):
creatable_papers_types = MultipleChoiceFieldFromMethod(
choices=ChoixTypeEpreuve.choices(),
source='get_creatable_papers_types',
)

def get_creatable_papers_types(self, _):
# Only one paper by type can be created

doctorate = self.context.get('doctorate')
already_created_papers_types = []

if doctorate:
already_created_papers_types = doctorate.activity_set.filter(
category=CategorieActivite.PAPER.name,
).values_list('type', flat=True)

return ChoixTypeEpreuve.get_names_except(*already_created_papers_types)

class Meta:
model = CddConfiguration
exclude = ['id', 'cdd']
Expand Down
5 changes: 5 additions & 0 deletions api/views/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ def get_object(self):
management_entity_id = self.get_permission_object().training.management_entity_id
return CddConfiguration.objects.get_or_create(cdd_id=management_entity_id)[0]

def get_serializer_context(self):
context = super().get_serializer_context()
context['doctorate'] = self.get_permission_object() if self.doctorate_uuid else None
return context

def get(self, request, *args, **kwargs):
serializer = self.get_serializer(self.get_object())
return Response(serializer.data)
Expand Down
15 changes: 15 additions & 0 deletions forms/training/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,21 @@ class Meta:
'ects': forms.NumberInput(attrs={'min': '0', 'step': '0.5'}),
}

def __init__(self, parcours_doctoral: ParcoursDoctoral, *args, **kwargs):
super().__init__(parcours_doctoral, *args, **kwargs)

# Only one paper of each type can be created
paper_qs = parcours_doctoral.activity_set.filter(category=CategorieActivite.PAPER.name)

if self.instance:
paper_qs = paper_qs.exclude(type=self.instance.type)

unavailable_papers_types = paper_qs.values_list('type', flat=True)

self.fields['type'].choices = (
(enum.name, enum.value) for enum in ChoixTypeEpreuve if enum.name not in unavailable_papers_types
)


class UclCourseForm(ActivityFormMixin, forms.ModelForm):
template_name = "parcours_doctoral/forms/training/ucl_course.html"
Expand Down
3 changes: 3 additions & 0 deletions locale/en/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ msgstr ""
msgid "A non doctor member must have a justification."
msgstr ""

msgid "A paper of each type has already been created."
msgstr ""

msgid "A promoter can not be president."
msgstr ""

Expand Down
3 changes: 3 additions & 0 deletions locale/fr_BE/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ msgid "A non doctor member must have a justification."
msgstr ""
"Un membre non porteur du titre de docteur doit avoir une justification."

msgid "A paper of each type has already been created."
msgstr "Une épreuve de chaque type a déjà été créée."

msgid "A promoter can not be president."
msgstr "Un promoteur ne peut pas être président."

Expand Down
11 changes: 10 additions & 1 deletion schema.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
openapi: 3.0.0
info:
title: Parcours Doctoral API
version: 1.0.8
version: 1.0.9
description: This API delivers data for the Parcours Doctoral project.
contact:
name: UCLouvain - OSIS
Expand Down Expand Up @@ -2622,6 +2622,15 @@ components:
DoctoralTrainingConfig:
type: object
properties:
creatable_papers_types:
type: array
items:
enum:
- CONFIRMATION_PAPER
- PRIVATE_DEFENSE
- PUBLIC_DEFENSE
type: string
readOnly: true
is_complementary_training_enabled:
type: boolean
category_labels:
Expand Down
18 changes: 12 additions & 6 deletions templates/parcours_doctoral/forms/training/paper.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* The core business involves the administration of students, teachers,
* courses, programs and so on.
*
* Copyright (C) 2015-2024 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 Down Expand Up @@ -36,8 +36,14 @@ <h2>

<p>{% lorem %}</p>

<div class="row">
{% bootstrap_field form.type wrapper_class="form-group col-md-4 required_field" %}
{% bootstrap_field_with_tooltip form.ects classes="form-group col-md-4 required_field" %}
</div>
{% bootstrap_field form.comment %}
{% if form.fields.type.choices %}
<div class="row">
{% bootstrap_field form.type wrapper_class="form-group col-md-4 required_field" %}
{% bootstrap_field_with_tooltip form.ects classes="form-group col-md-4 required_field" %}
</div>
{% bootstrap_field form.comment %}
{% else %}
<div class="alert alert-danger">
{% translate 'A paper of each type has already been created.' %}
</div>
{% endif %}
62 changes: 61 additions & 1 deletion tests/api/views/test_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from parcours_doctoral.ddd.domain.model.enums import ChoixStatutParcoursDoctoral
from parcours_doctoral.ddd.formation.domain.model.enums import (
CategorieActivite,
ChoixTypeEpreuve,
ContexteFormation,
StatutActivite,
)
Expand All @@ -45,6 +46,7 @@
ConferenceCommunicationFactory,
ConferenceFactory,
CourseFactory,
PaperFactory,
ServiceFactory,
UclCourseFactory,
)
Expand Down Expand Up @@ -260,10 +262,68 @@ def test_training_update_with_student(self):

def test_training_config(self):
self.client.force_authenticate(user=self.student.user)
config_url = resolve_url("parcours_doctoral_api_v1:training-config", uuid=self.parcours_doctoral.uuid)

doctorate = ParcoursDoctoralFactory(student=self.student)

config_url = resolve_url("parcours_doctoral_api_v1:training-config", uuid=doctorate.uuid)

response = self.client.get(config_url)

self.assertEqual(response.status_code, status.HTTP_200_OK)

json_response = response.json()

self.assertCountEqual(
json_response['creatable_papers_types'],
[
ChoixTypeEpreuve.CONFIRMATION_PAPER.name,
ChoixTypeEpreuve.PRIVATE_DEFENSE.name,
ChoixTypeEpreuve.PUBLIC_DEFENSE.name,
],
)

PaperFactory(
parcours_doctoral=doctorate,
type=ChoixTypeEpreuve.CONFIRMATION_PAPER.name,
)

response = self.client.get(config_url)

self.assertEqual(response.status_code, status.HTTP_200_OK)

json_response = response.json()

self.assertCountEqual(
json_response['creatable_papers_types'],
[ChoixTypeEpreuve.PRIVATE_DEFENSE.name, ChoixTypeEpreuve.PUBLIC_DEFENSE.name],
)

PaperFactory(
parcours_doctoral=doctorate,
type=ChoixTypeEpreuve.PUBLIC_DEFENSE.name,
)

response = self.client.get(config_url)

self.assertEqual(response.status_code, status.HTTP_200_OK)

json_response = response.json()

self.assertCountEqual(json_response['creatable_papers_types'], [ChoixTypeEpreuve.PRIVATE_DEFENSE.name])

PaperFactory(
parcours_doctoral=doctorate,
type=ChoixTypeEpreuve.PRIVATE_DEFENSE.name,
)

response = self.client.get(config_url)

self.assertEqual(response.status_code, status.HTTP_200_OK)

json_response = response.json()

self.assertCountEqual(json_response['creatable_papers_types'], [])

def test_training_should_delete_unsubmitted(self):
service = ServiceFactory(parcours_doctoral=self.parcours_doctoral)
self.client.force_authenticate(user=self.student.user)
Expand Down
Loading