def remove_card_from_aidant(request, aidant_id):
    responsable: Aidant = request.user
    aidant = get_object_or_404(Aidant, pk=aidant_id)
    if not responsable.can_see_aidant(aidant):
        raise Http404

    if not aidant.has_a_carte_totp:
        return redirect(
            "espace_responsable_aidant",
            aidant_id=aidant.id,
        )

    form = RemoveCardFromAidantForm(request.POST)

    if not form.is_valid():
        raise Exception("Invalid form for card/aidant dissociation")

    data = form.cleaned_data
    reason = data.get("reason")
    if reason == "autre":
        reason = data.get("other_reason")
    sn = aidant.carte_totp.serial_number
    with transaction.atomic():
        carte = CarteTOTP.objects.get(serial_number=sn)
        try:
            device = TOTPDevice.objects.get(key=carte.seed, user=aidant)
            device.delete()
        except TOTPDevice.DoesNotExist:
            pass
        carte.aidant = None
        carte.save()
        Journal.log_card_dissociation(responsable, aidant, sn, reason)

    django_messages.success(
        request,
        (
            f"Tout s’est bien passé, la carte {sn} a été séparée du compte "
            f"de l’aidant {aidant.get_full_name()}."
        ),
    )

    return redirect(
        "espace_responsable_aidant",
        aidant_id=aidant.id,
    )
Exemplo n.º 2
0
    def __dissociate_from_aidant_post(self, request, object_id):
        try:
            object = CarteTOTP.objects.get(id=object_id)
            aidant = object.aidant
            if aidant is None:
                self.message_user(
                    request,
                    f"Aucun aidant n’est associé à la carte {object.serial_number}.",
                    messages.ERROR,
                )
                return HttpResponseRedirect(
                    reverse(
                        "otpadmin:aidants_connect_web_cartetotp_changelist"))

            totp_devices = TOTPDevice.objects.filter(user=aidant,
                                                     key=object.seed)
            for d in totp_devices:
                d.delete()
            object.aidant = None
            object.save()

            Journal.log_card_dissociation(request.user, aidant,
                                          object.serial_number, "Admin action")

            self.message_user(request, "Tout s'est bien passé.")
            return HttpResponseRedirect(
                reverse(
                    "otpadmin:aidants_connect_web_cartetotp_change",
                    kwargs={"object_id": object_id},
                ))
        except Exception:
            logger.exception(
                "An error occured while trying to dissociate an aidant"
                "from their carte TOTP")

            self.message_user(
                request,
                "Quelque chose s’est mal passé durant l'opération.",
                messages.ERROR,
            )

        return HttpResponseRedirect(
            reverse("otpadmin:aidants_connect_web_cartetotp_changelist"))