Skip to content
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

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

Open
wants to merge 1 commit into
base: feature/OS-300
Choose a base branch
from
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
15 changes: 15 additions & 0 deletions forms/training/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,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 @@ -140,6 +140,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 @@ -184,6 +184,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
16 changes: 11 additions & 5 deletions templates/parcours_doctoral/forms/training/paper.html
Original file line number Diff line number Diff line change
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 %}
80 changes: 73 additions & 7 deletions tests/views/cdd/test_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
from unittest.mock import patch

import freezegun
from base.tests.factories.academic_year import AcademicYearFactory
from base.tests.factories.learning_unit_year import LearningUnitYearFactory
from base.tests.factories.program_manager import ProgramManagerFactory
from django.conf import settings
from django.forms import Field
from django.shortcuts import resolve_url
Expand All @@ -41,9 +38,13 @@
from osis_notification.models import WebNotification
from rest_framework import status

from base.tests.factories.academic_year import AcademicYearFactory
from base.tests.factories.learning_unit_year import LearningUnitYearFactory
from base.tests.factories.program_manager import ProgramManagerFactory
from parcours_doctoral.ddd.formation.domain.model.enums import (
CategorieActivite,
ChoixComiteSelection,
ChoixTypeEpreuve,
ContexteFormation,
StatutActivite,
)
Expand Down Expand Up @@ -241,6 +242,67 @@ def test_ucl_course(self):
just_created = Activity.objects.first()
self.assertEqual(just_created.context, ContexteFormation.COMPLEMENTARY_TRAINING.name)

def test_paper(self):
create_url = resolve_url(
f'parcours_doctoral:doctoral-training:add',
uuid=self.parcours_doctoral.uuid,
category=CategorieActivite.PAPER.name,
)

# No paper has been created -> all types are available
response = self.client.get(create_url)

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

form = response.context['form']

self.assertCountEqual(form.fields['type'].choices, ((enum.name, enum.value) for enum in ChoixTypeEpreuve))

# Create a confirmation paper
response = self.client.post(
create_url,
data={
'type': ChoixTypeEpreuve.CONFIRMATION_PAPER.name,
'ects': 10,
'comment': 'comment A',
},
)

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

just_created = Activity.objects.first()

self.assertEqual(just_created.context, ContexteFormation.DOCTORAL_TRAINING.name)
self.assertEqual(just_created.type, ChoixTypeEpreuve.CONFIRMATION_PAPER.name)
self.assertEqual(just_created.ects, 10)
self.assertEqual(just_created.comment, 'comment A')

# A confirmation paper has been created -> its type is not available anymore for another paper
response = self.client.get(create_url)
self.assertEqual(response.status_code, status.HTTP_200_OK)

form = response.context['form']

self.assertCountEqual(
form.fields['type'].choices,
((enum.name, enum.value) for enum in [ChoixTypeEpreuve.PRIVATE_DEFENSE, ChoixTypeEpreuve.PUBLIC_DEFENSE]),
)

# A confirmation paper has been created -> its type is available if we want to edit it
update_url = resolve_url(
'parcours_doctoral:doctoral-training:edit',
uuid=self.parcours_doctoral.uuid,
activity_id=just_created.uuid,
)

response = self.client.get(update_url)

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

form = response.context['form']

self.assertCountEqual(form.fields['type'].choices, ((enum.name, enum.value) for enum in ChoixTypeEpreuve))

def test_missing_form(self):
add_url = resolve_url(f'{self.namespace}:add', uuid=self.parcours_doctoral.uuid, category='foobar')
response = self.client.get(add_url)
Expand All @@ -261,10 +323,14 @@ def test_parent(self):
response = self.client.get(f"{add_url}?parent={self.conference.uuid}")
self.assertEqual(response.status_code, status.HTTP_200_OK)

response = self.client.post(f"{add_url}?parent={self.conference.uuid}", {
'start_date_year': self.conference.start_date.year,
'start_date_month': self.conference.start_date.month,
}, follow=True)
response = self.client.post(
f"{add_url}?parent={self.conference.uuid}",
{
'start_date_year': self.conference.start_date.year,
'start_date_month': self.conference.start_date.month,
},
follow=True,
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_edit(self):
Expand Down