Exemplo n.º 1
0
def confirm_autorisation_cancelation(request, usager_id, autorisation_id):
    aidant = request.user
    try:
        autorisation = aidant.get_active_autorisations_for_usager(
            usager_id).get(pk=autorisation_id)
    except Autorisation.DoesNotExist:
        django_messages.error(
            request, "Cette autorisation est introuvable ou inaccessible.")
        return redirect("espace_aidant_home")

    if request.method == "POST":
        form = request.POST

        if form:
            autorisation.revocation_date = timezone.now()
            autorisation.save(update_fields=["revocation_date"])

            Journal.log_autorisation_cancel(autorisation, aidant)

            return redirect(
                "autorisation_cancelation_success",
                usager_id=usager_id,
                autorisation_id=autorisation_id,
            )

    return render(
        request,
        "aidants_connect_web/mandat_auths_cancellation/"
        "confirm_autorisation_cancelation.html",
        {
            "aidant": aidant,
            "usager": aidant.get_usager(usager_id),
            "autorisation": autorisation,
        },
    )
Exemplo n.º 2
0
def usagers_mandats_autorisations_cancel_confirm(
    request, usager_id, mandat_id, autorisation_id
):
    aidant = request.user

    usager = aidant.get_usager(usager_id)
    if not usager:
        django_messages.error(request, "Cet usager est introuvable ou inaccessible.")
        return redirect("espace_aidant_home")

    autorisation = usager.get_autorisation(mandat_id, autorisation_id)
    if not autorisation:
        django_messages.error(
            request, "Cette autorisation est introuvable ou inaccessible."
        )
        return redirect("espace_aidant_home")
    if autorisation.is_revoked:
        django_messages.error(request, "L'autorisation a été révoquée")
        return redirect("espace_aidant_home")
    if autorisation.is_expired:
        django_messages.error(request, "L'autorisation a déjà expiré")
        return redirect("espace_aidant_home")

    if request.method == "POST":
        form = request.POST

        if form:
            autorisation.revocation_date = timezone.now()
            autorisation.save(update_fields=["revocation_date"])

            Journal.log_autorisation_cancel(autorisation, aidant)

            django_messages.success(
                request, "L'autorisation a été révoquée avec succès !"
            )
            return redirect("usager_details", usager_id=usager.id)

        else:
            return render(
                request,
                "aidants_connect_web/usagers_mandats_autorisations_cancel_confirm.html",
                {
                    "aidant": aidant,
                    "usager": usager,
                    "autorisation": autorisation,
                    "error": "Erreur lors de l'annulation de l'autorisation.",
                },
            )

    return render(
        request,
        "aidants_connect_web/usagers_mandats_autorisations_cancel_confirm.html",
        {
            "aidant": aidant,
            "usager": usager,
            "autorisation": autorisation,
            "error": "Erreur lors de l'annulation de l'autorisation.",
        },
    )
Exemplo n.º 3
0
    def test_updating_revoked_autorisation_for_same_organisation(self):
        # first session : creating the autorisation
        self.client.force_login(self.aidant_thierry)
        mandat_builder_1 = Connection.objects.create(
            usager=self.test_usager, demarches=["papiers"], duree_keyword="SHORT"
        )
        session = self.client.session
        session["connection"] = mandat_builder_1.id
        session.save()

        # trigger the mandat creation
        self.client.post(
            "/creation_mandat/recapitulatif/",
            data={"personal_data": True, "brief": True, "otp_token": "123456"},
        )

        self.assertEqual(Autorisation.objects.count(), 1)
        last_journal_entry = Journal.objects.last()
        self.assertEqual(last_journal_entry.action, "create_autorisation")

        # revoke
        self.client.post(
            "/creation_mandat/recapitulatif/",
            data={"personal_data": True, "brief": True, "otp_token": "123456"},
        )

        last_autorisation = Autorisation.objects.last()
        last_autorisation.revocation_date = timezone.now()
        last_autorisation.save(update_fields=["revocation_date"])

        Journal.log_autorisation_cancel(last_autorisation, self.aidant_thierry)

        # second session : 'updating' the autorisation
        self.client.force_login(self.aidant_thierry)
        mandat_builder_2 = Connection.objects.create(
            usager=self.test_usager,
            demarches=["papiers", "logement"],
            duree_keyword="LONG",
        )
        session = self.client.session
        session["connection"] = mandat_builder_2.id
        session.save()

        # trigger the mandat creation
        self.client.post(
            "/creation_mandat/recapitulatif/",
            data={"personal_data": True, "brief": True, "otp_token": "223456"},
        )

        self.assertEqual(Autorisation.objects.count(), 3)

        last_usager_organisation_papiers_autorisations = Autorisation.objects.filter(
            demarche="papiers",
            mandat__usager=self.test_usager,
            mandat__organisation=self.aidant_thierry.organisation,
        ).order_by("-mandat__creation_date")
        new_papiers_autorisation = last_usager_organisation_papiers_autorisations[0]
        old_papiers_autorisation = last_usager_organisation_papiers_autorisations[1]
        self.assertEqual(new_papiers_autorisation.duration_for_humans, 365)
        self.assertFalse(old_papiers_autorisation.is_expired)
        self.assertTrue(old_papiers_autorisation.is_revoked)

        last_journal_entries = Journal.objects.all().order_by("-id")
        self.assertEqual(last_journal_entries[0].action, "create_autorisation")
        self.assertEqual(last_journal_entries[0].demarche, "papiers")
        self.assertEqual(last_journal_entries[1].action, "create_autorisation")
        self.assertEqual(last_journal_entries[1].demarche, "logement")
        self.assertEqual(last_journal_entries[2].action, "create_attestation")
        self.assertEqual(last_journal_entries[2].demarche, "logement,papiers")

        self.assertEqual(
            len(self.aidant_thierry.get_active_demarches_for_usager(self.test_usager)),
            2,
        )
Exemplo n.º 4
0
 def test_log_autorisation_cancel_complete(self):
     entry = Journal.log_autorisation_cancel(
         autorisation=self.first_autorisation, aidant=self.aidant_thierry)
     self.assertEqual(len(Journal.objects.all()), 3)
     self.assertEqual(entry.action, "cancel_autorisation")
Exemplo n.º 5
0
def confirm_mandat_cancelation(request, mandat_id):
    aidant: Aidant = request.user
    try:
        mandat = Mandat.objects.get(pk=mandat_id,
                                    organisation=aidant.organisation)
    except Mandat.DoesNotExist:
        django_messages.error(request,
                              "Ce mandat est introuvable ou inaccessible.")
        return redirect("espace_aidant_home")

    usager = mandat.usager
    remaining_autorisations = []

    if mandat.is_active:
        for autorisation in mandat.autorisations.filter(revocation_date=None):
            remaining_autorisations.append(
                humanize_demarche_names(autorisation.demarche))

        if request.method == "POST":
            if request.POST:
                autorisation_in_mandat = Autorisation.objects.filter(
                    mandat=mandat)
                for autorisation in autorisation_in_mandat:
                    if not autorisation.revocation_date:
                        autorisation.revocation_date = (
                            autorisation.revocation_date) = timezone.now()
                        autorisation.save(update_fields=["revocation_date"])
                        Journal.log_autorisation_cancel(autorisation, aidant)
                Journal.log_mandat_cancel(mandat, aidant)
                return redirect("mandat_cancelation_success",
                                mandat_id=mandat.id)
            else:
                return render(
                    request,
                    "aidants_connect_web/mandat_auths_cancellation/"
                    "confirm_mandat_cancellation.html",
                    {
                        "aidant":
                        aidant,
                        "usager_name":
                        usager.get_full_name(),
                        "usager_id":
                        usager.id,
                        "mandat":
                        mandat,
                        "remaining_autorisations":
                        remaining_autorisations,
                        "error":
                        "Une erreur s'est produite lors "
                        "de la révocation du mandat",
                    },
                )

    return render(
        request,
        "aidants_connect_web/mandat_auths_cancellation/"
        "confirm_mandat_cancellation.html",
        {
            "aidant": aidant,
            "usager_name": usager.get_full_name(),
            "usager_id": usager.id,
            "mandat": mandat,
            "remaining_autorisations": remaining_autorisations,
        },
    )
Exemplo n.º 6
0
def new_mandat_recap(request):
    connection = Connection.objects.get(pk=request.session["connection"])
    aidant = request.user
    usager = connection.usager
    demarches_description = [
        humanize_demarche_names(demarche) for demarche in connection.demarches
    ]
    duree = connection.get_duree_keyword_display()
    is_remote = connection.mandat_is_remote

    if request.method == "GET":
        form = RecapMandatForm(aidant)
        return render(
            request,
            "aidants_connect_web/new_mandat/new_mandat_recap.html",
            {
                "aidant": aidant,
                "usager": usager,
                "demarches": demarches_description,
                "duree": duree,
                "is_remote": is_remote,
                "form": form,
            },
        )

    else:
        form = RecapMandatForm(aidant=aidant, data=request.POST)

        if form.is_valid():
            now = timezone.now()
            expiration_date = {
                "SHORT": now + timedelta(days=1),
                "LONG": now + timedelta(days=365),
                "EUS_03_20": settings.ETAT_URGENCE_2020_LAST_DAY,
            }
            mandat_expiration_date = expiration_date.get(
                connection.duree_keyword)
            days_before_expiration_date = {
                "SHORT": 1,
                "LONG": 365,
                "EUS_03_20":
                1 + (settings.ETAT_URGENCE_2020_LAST_DAY - now).days,
            }
            mandat_duree = days_before_expiration_date.get(
                connection.duree_keyword)

            try:
                # Add a Journal 'create_attestation' action
                connection.demarches.sort()
                Journal.log_attestation_creation(
                    aidant=aidant,
                    usager=usager,
                    demarches=connection.demarches,
                    duree=mandat_duree,
                    is_remote_mandat=connection.mandat_is_remote,
                    access_token=connection.access_token,
                    attestation_hash=generate_attestation_hash(
                        aidant, usager, connection.demarches,
                        mandat_expiration_date),
                )

                # Create a mandat
                mandat = Mandat.objects.create(
                    organisation=aidant.organisation,
                    usager=usager,
                    duree_keyword=connection.duree_keyword,
                    expiration_date=mandat_expiration_date,
                    is_remote=connection.mandat_is_remote,
                )

                # This loop creates one `autorisation` object per `démarche` in the form
                for demarche in connection.demarches:
                    # Revoke existing demarche autorisation(s)
                    similar_active_autorisations = Autorisation.objects.active(
                    ).filter(
                        mandat__organisation=aidant.organisation,
                        mandat__usager=usager,
                        demarche=demarche,
                    )
                    for similar_active_autorisation in similar_active_autorisations:
                        similar_active_autorisation.revocation_date = now
                        similar_active_autorisation.save(
                            update_fields=["revocation_date"])
                        Journal.log_autorisation_cancel(
                            similar_active_autorisation, aidant)

                    # Create new demarche autorisation
                    autorisation = Autorisation.objects.create(
                        mandat=mandat,
                        demarche=demarche,
                        last_renewal_token=connection.access_token,
                    )
                    Journal.log_autorisation_creation(autorisation, aidant)

            except AttributeError as error:
                log.error("Error happened in Recap")
                log.error(error)
                django_messages.error(
                    request, f"Error with Usager attribute : {error}")
                return redirect("espace_aidant_home")

            except IntegrityError as error:
                log.error("Error happened in Recap")
                log.error(error)
                django_messages.error(request,
                                      f"No Usager was given : {error}")
                return redirect("espace_aidant_home")

            return redirect("new_mandat_success")

        else:
            return render(
                request,
                "aidants_connect_web/new_mandat/new_mandat_recap.html",
                {
                    "aidant": aidant,
                    "usager": usager,
                    "demarche": demarches_description,
                    "duree": duree,
                    "form": form,
                },
            )