def __attestation_visualisation( request, template: str, usager: Usager, aidant: Aidant, attestation_date: date, demarches: Collection[str], duree: str, modified: bool = False, ): return render( request, "aidants_connect_web/attestation.html", { "usager": usager, "aidant": aidant, "date": formats.date_format(attestation_date, "l j F Y"), "demarches": [humanize_demarche_names(demarche) for demarche in demarches], "duree": duree, "current_mandat_template": template, "final": True, "modified": modified, }, )
def autorisation_cancelation_success(request, usager_id, autorisation_id): aidant: Aidant = request.user try: authorization = aidant.get_inactive_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 not authorization.is_revoked: django_messages.error(request, "Cette autorisation est encore active.") return redirect("espace_aidant_home") return render( request, "aidants_connect_web/mandat_auths_cancellation/" "authorization_cancellation_success.html", { "aidant": aidant, "humanized_auth": humanize_demarche_names(authorization.demarche), "usager": aidant.get_usager(usager_id), "authorization": authorization, }, )
def attestation_projet(request): connection_id = request.session.get("connection") if not connection_id: log.error("No connection id found in session") return redirect("espace_aidant_home") connection = Connection.objects.get(pk=connection_id) aidant = request.user usager = connection.usager demarches = connection.demarches # Django magic : # https://docs.djangoproject.com/en/3.0/ref/models/instances/#django.db.models.Model.get_FOO_display duree = connection.get_duree_keyword_display() return render( request, "aidants_connect_web/attestation.html", { "usager": usager, "aidant": aidant, "date": formats.date_format(date.today(), "l j F Y"), "demarches": [humanize_demarche_names(demarche) for demarche in demarches], "duree": duree, "current_mandat_template": settings.MANDAT_TEMPLATE_PATH, }, )
def recap(request): connection = Connection.objects.get(pk=request.session["connection"]) aidant = request.user usager = connection.usager duree = "1 jour" if connection.duree == 1 else "1 an" demarches_description = [ humanize_demarche_names(demarche) for demarche in connection.demarches ] if request.method == "GET": return render( request, "aidants_connect_web/new_mandat/recap.html", { "aidant": aidant, "usager": usager, "demarches": demarches_description, "duree": duree, }, ) else: form = request.POST if form.get("personal_data") and form.get("brief"): for demarche in connection.demarches: try: Mandat.objects.update_or_create( aidant=aidant, usager=usager, demarche=demarche, defaults={ "duree": connection.duree, "modified_by_access_token": connection.access_token, }, ) except IntegrityError as e: log.error("Error happened in Recap") log.error(e) messages.error(request, f"No Usager was given : {e}") return redirect("dashboard") messages.success(request, "Le mandat a été créé avec succès !") return redirect("dashboard") else: return render( request, "aidants_connect_web/new_mandat/recap.html", { "aidant": aidant, "usager": usager, "demarche": demarches_description, "duree": duree, "error": "Vous devez accepter les conditions du mandat.", }, )
def new_mandat_preview(request): connection = Connection.objects.get(pk=request.session["connection"]) aidant = request.user usager = connection.usager demarches = connection.demarches duree = "1 jour" if connection.duree == 1 else "1 an" return render( request, "aidants_connect_web/new_mandat/new_mandat_preview.html", { "usager": f"{usager.given_name} {usager.family_name}", "aidant": f"{aidant.first_name} {aidant.last_name.upper()}", "profession": aidant.profession, "organisation": aidant.organisation.name, "lieu": aidant.organisation.address, "date": formats.date_format(date.today(), "l j F Y"), "demarches": [humanize_demarche_names(demarche) for demarche in demarches], "duree": duree, }, )
def attestation_final(request): connection = Connection.objects.get(pk=request.session["connection"]) aidant = request.user usager = connection.usager demarches = connection.demarches # Django magic : # https://docs.djangoproject.com/en/3.0/ref/models/instances/#django.db.models.Model.get_FOO_display duree = connection.get_duree_keyword_display() return render( request, "aidants_connect_web/attestation.html", { "usager": usager, "aidant": aidant, "date": formats.date_format(date.today(), "l j F Y"), "demarches": [humanize_demarche_names(demarche) for demarche in demarches], "duree": duree, "final": True, }, )
def generate_mandat_pdf(request): connection = Connection.objects.get(pk=request.session["connection"]) aidant = request.user usager = connection.usager demarches = connection.demarches duree = "1 jour" if connection.duree == 1 else "1 an" html_string = render_to_string( "aidants_connect_web/new_mandat/pdf_mandat.html", { "usager": f"{usager.given_name} {usager.family_name}", "aidant": f"{aidant.first_name} {aidant.last_name.upper()}", "profession": aidant.profession, "organisme": aidant.organisme, "lieu": aidant.ville, "date": formats.date_format(date.today(), "l j F Y"), "demarches": [humanize_demarche_names(demarche) for demarche in demarches], "duree": duree, }, ) html = HTML(string=html_string) html.write_pdf(target="/tmp/mandat_aidants_connect.pdf") fs = FileSystemStorage("/tmp") with fs.open("mandat_aidants_connect.pdf") as pdf: response = HttpResponse(pdf, content_type="application/pdf") response[ "Content-Disposition"] = "inline; filename='mandat_aidants_connect.pdf'" return response
def autorisation_cancelation_attestation(request, usager_id, autorisation_id): aidant: Aidant = request.user try: autorisation = aidant.get_inactive_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") mandat: Mandat = autorisation.mandat if not autorisation.is_revoked: django_messages.error(request, "Cette autorisation est encore active.") return redirect("espace_aidant_home") if not autorisation.was_separately_revoked: return redirect("mandat_cancellation_attestation", mandat_id=mandat.id) user = aidant.get_usager(usager_id) return render( request, "aidants_connect_web/mandat_auths_cancellation/" "authorization_cancellation_attestation.html", { "aidant": aidant, "authorization": humanize_demarche_names(autorisation.demarche), "user": user, "organisation": autorisation.mandat.organisation, "creation_date": autorisation.mandat.creation_date.strftime("%d/%m/%Y à %Hh%M"), "revocation_date": autorisation.revocation_date.strftime("%d/%m/%Y à %Hh%M"), }, )
def new_mandat_recap(request): connection_id = request.session.get("connection") if not connection_id: log.error("No connection id found in session") return redirect("espace_aidant_home") connection = Connection.objects.get(pk=connection_id) 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() durationkw = connection.duree_keyword mandat_expiration_date = AuthorizationDurations.expiration( durationkw, now) mandat_duree = AuthorizationDurations.duration(durationkw, now) try: connection.demarches.sort() # 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, ) # Add a Journal 'create_attestation' action 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), mandat=mandat, ) # 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.revoke(aidant=aidant, revocation_date=now) # 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, }, )
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, }, )
def new_mandat_recap(request): connection = Connection.objects.get(pk=request.session["connection"]) aidant = request.user usager = connection.usager duree = "1 jour" if connection.duree == 1 else "1 an" demarches_description = [ humanize_demarche_names(demarche) for demarche in connection.demarches ] 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, "form": form, }, ) else: form = RecapMandatForm(aidant=aidant, data=request.POST) if form.is_valid(): # The loop below creates one Mandat object per Démarche selected in the form for demarche in connection.demarches: try: Mandat.objects.update_or_create( aidant=aidant, usager=usager, demarche=demarche, defaults={ "expiration_date": timezone.now() + timedelta(days=connection.duree), "last_mandat_renewal_date": timezone.now(), "last_mandat_renewal_token": connection.access_token, }, ) except IntegrityError as e: log.error("Error happened in Recap") log.error(e) messages.error(request, f"No Usager was given : {e}") return redirect("dashboard") messages.success(request, "Le mandat a été créé avec succès !") return redirect("dashboard") else: return render( request, "aidants_connect_web/new_mandat/new_mandat_recap.html", { "aidant": aidant, "usager": usager, "demarche": demarches_description, "duree": duree, "form": form, "error": form.errors, }, )
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, }, )