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

Choix date signature #1586

Merged
merged 11 commits into from
Oct 1, 2024
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
63 changes: 59 additions & 4 deletions conventions/services/recapitulatif.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from comments.models import Comment, CommentStatut
from conventions.forms.avenant import CompleteforavenantForm
from conventions.forms.convention_form_dates import ConventionDateSignatureForm
from conventions.forms.convention_number import ConventionNumberForm
from conventions.forms.notification import NotificationForm
from conventions.forms.programme_number import ProgrammeNumberForm
Expand All @@ -20,7 +21,9 @@
from conventions.services.conventions import ConventionService
from conventions.services.file import ConventionFileService
from conventions.tasks import task_generate_and_send
from core.request import AuthenticatedHttpRequest
from core.services import EmailService, EmailTemplateID
from core.stepper import Stepper
from programmes.models import Annexe, Programme
from siap.exceptions import SIAPException
from siap.siap_client.client import SIAPClient
Expand Down Expand Up @@ -597,15 +600,67 @@ def save(self):
upform = UploadForm(self.request.POST, self.request.FILES)
if upform.is_valid():
ConventionFileService.upload_convention_file(
self.convention, self.request.FILES["file"]
self.convention, self.request.FILES["file"], update_statut=False
)
self.result_status = utils.ReturnStatus.SUCCESS

self.return_status = utils.ReturnStatus.SUCCESS
return {
"success": self.result_status,
"success": self.return_status,
"convention": self.convention,
"upform": upform,
"extra_forms": {
"upform": upform,
},
}


class ConventionUploadSignedService(ConventionService):

def __init__(
self,
convention: Convention,
request: AuthenticatedHttpRequest,
step_number: int = 1,
):
super().__init__(convention, request)
self.stepper = Stepper(
steps=[
"Prévisualiser le document",
"Indiquer la date de signature",
]
)
self.step_number = step_number

def get(self):
return {
"convention": self.convention,
"signature_date_form": ConventionDateSignatureForm(
initial={
"televersement_convention_signee_le": datetime.date.today().strftime(
"%Y-%m-%d"
)
}
),
"form_step": self.stepper.get_form_step(step_number=self.step_number),
}

def save(self):
form = ConventionDateSignatureForm(self.request.POST)
if form.is_valid():
self.convention.televersement_convention_signee_le = form.cleaned_data[
"televersement_convention_signee_le"
]
self.convention.statut = ConventionStatut.SIGNEE.label
self.convention.save()
self.return_status = utils.ReturnStatus.SUCCESS

return {
"success": self.return_status,
"convention": self.convention,
"form": form,
}

def get_success_message(self):
date_signature = self.convention.televersement_convention_signee_le.strftime(
"%d/%m/%Y"
)
return f"Convention signée avec succès le {date_signature}"
89 changes: 89 additions & 0 deletions conventions/tests/services/test_recapitulatif_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@
from unittest import mock
from unittest.mock import patch

import pytest
import time_machine
from django.contrib.sessions.middleware import SessionMiddleware
from django.http import HttpRequest
from django.test import RequestFactory, TestCase

from comments.models import Comment, CommentStatut
from conventions.forms.convention_form_dates import ConventionDateSignatureForm
from conventions.forms.convention_number import ConventionNumberForm
from conventions.forms.programme_number import ProgrammeNumberForm
from conventions.forms.upload import UploadForm
from conventions.models import Convention
from conventions.models.choices import ConventionStatut
from conventions.services import recapitulatif, utils
from conventions.services.utils import ReturnStatus
from conventions.tests.factories import ConventionFactory
from programmes.models import Lot, Programme
from siap.exceptions import SIAPException
from siap.siap_client.client import SIAPClient
Expand Down Expand Up @@ -403,3 +408,87 @@ def test_collect_instructeur_cerbere_user_raise_siapexception(self):

self.assertEqual([], instructeur_emails)
self.assertEqual(ReturnStatus.WARNING, submitted)


@pytest.mark.django_db
class TestConventionSentService:
def test_get(self):
convention = ConventionFactory()
request = RequestFactory().get("/")
service = recapitulatif.ConventionSentService(
convention=convention, request=request
)

result = service.get()

assert len(result) == 3
assert result["convention"] == convention
assert isinstance(result["upform"], UploadForm)
assert result["upform"] == result["extra_forms"]["upform"]

def test_save_error(self):
convention = ConventionFactory()
request = RequestFactory().post("/", data={"file": "myfile"})
service = recapitulatif.ConventionSentService(
convention=convention, request=request
)

result = service.save()

assert len(result) == 4
assert result["success"] == ReturnStatus.ERROR
assert result["convention"] == convention
assert isinstance(result["upform"], UploadForm)
assert result["upform"] == result["extra_forms"]["upform"]


@pytest.mark.django_db
class TestConventionUploadSignedService:
def test_get(self):
convention = ConventionFactory()
request = RequestFactory().get("/")
service = recapitulatif.ConventionUploadSignedService(
convention=convention, request=request
)
with time_machine.travel("2024-06-21"):
result = service.get()

assert len(result) == 3
assert result["convention"] == convention
assert isinstance(result["form_step"], dict)
assert isinstance(
result["signature_date_form"], ConventionDateSignatureForm
)
assert result["signature_date_form"].initial == {
"televersement_convention_signee_le": "2024-06-21"
}

def test_save(self):
convention = ConventionFactory()
request = RequestFactory().post(
"/", data={"televersement_convention_signee_le": "15/04/2023"}
)
service = recapitulatif.ConventionUploadSignedService(
convention=convention, request=request
)

result = service.save()

assert len(result) == 3
assert result["convention"] == convention
assert isinstance(result["form"], ConventionDateSignatureForm)
assert result["success"] == ReturnStatus.SUCCESS

def test_get_success_message(self):
convention = ConventionFactory()
request = RequestFactory().post(
"/", data={"televersement_convention_signee_le": "15/04/2023"}
)
service = recapitulatif.ConventionUploadSignedService(
convention=convention, request=request
)

service.save()
message = service.get_success_message()

assert message == "Convention signée avec succès le 15/04/2023"
85 changes: 85 additions & 0 deletions conventions/tests/views/test_upload_signed_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import datetime

import pytest
from django.contrib.messages.storage.fallback import FallbackStorage
from django.test import RequestFactory
from django.urls import reverse

from conventions.tests.factories import ConventionFactory
from conventions.views.conventions import (
ConventionDateUploadSignedView,
ConventionPreviewUploadSignedView,
)
from users.tests.factories import UserFactory


@pytest.mark.django_db
class TestConventionPreviewUploadSignedView:
def test_get(self):
convention = ConventionFactory()
url = reverse(
"conventions:preview_upload_signed",
kwargs={"convention_uuid": convention.uuid},
)
request = RequestFactory().get(url)
user = UserFactory(is_superuser=True)
request.user = user
request.session = "session"

response = ConventionPreviewUploadSignedView.as_view()(
request, convention_uuid=convention.uuid
)

assert response.status_code == 200


@pytest.mark.django_db
class TestConventionDateUploadSignedView:
def test_get(self):
convention = ConventionFactory()
url = reverse(
"conventions:preview_upload_signed",
kwargs={"convention_uuid": convention.uuid},
)
request = RequestFactory().get(url)
user = UserFactory(is_superuser=True)
request.user = user
request.session = "session"
messages = FallbackStorage(request)
request._messages = messages

response = ConventionDateUploadSignedView.as_view()(
request, convention_uuid=convention.uuid
)

assert response.status_code == 200

def test_post(self):
convention = ConventionFactory()
url = reverse(
"conventions:preview_upload_signed",
kwargs={"convention_uuid": convention.uuid},
)
request = RequestFactory().post(
url, data={"televersement_convention_signee_le": "09/08/2024"}
)
user = UserFactory(is_superuser=True)
request.user = user
request.session = "session"
messages = FallbackStorage(request)
request._messages = messages

response = ConventionDateUploadSignedView.as_view()(
request, convention_uuid=convention.uuid
)

convention.refresh_from_db()
assert response.status_code == 302
assert (
response.headers["Location"]
== f"/conventions/post_action/{convention.uuid}"
)
assert convention.televersement_convention_signee_le == datetime.date(
2024, 8, 9
)
assert convention.statut == "5. Signée"
10 changes: 10 additions & 0 deletions conventions/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,16 @@
views.ConventionSentView.as_view(),
name="sent",
),
path(
"upload_signed/preview/<convention_uuid>",
views.ConventionPreviewUploadSignedView.as_view(),
name="preview_upload_signed",
),
path(
"upload_signed/date/<convention_uuid>",
views.ConventionDateUploadSignedView.as_view(),
name="date_upload_signed",
),
path(
"display_pdf/<convention_uuid>",
views.display_pdf,
Expand Down
Loading