/
new_mandat.py
151 lines (124 loc) · 4.72 KB
/
new_mandat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import logging
from django.utils import formats
from datetime import date
from weasyprint import HTML
from django.db import IntegrityError
from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.core.files.storage import FileSystemStorage
from django.contrib import messages
from django.template.loader import render_to_string
from aidants_connect_web.models import Mandat, Connection
from aidants_connect_web.forms import MandatForm
from aidants_connect_web.views.service import humanize_demarche_names
logging.basicConfig(level=logging.INFO)
log = logging.getLogger()
@login_required
def new_mandat(request):
aidant = request.user
form = MandatForm()
if request.method == "GET":
return render(
request,
"aidants_connect_web/new_mandat/new_mandat.html",
{"aidant": aidant, "form": form},
)
else:
form = MandatForm(request.POST)
if form.is_valid():
data = form.cleaned_data
duree = 1 if data["duree"] == "short" else 365
connection = Connection.objects.create(
demarches=data["perimeter"], duree=duree
)
request.session["connection"] = connection.pk
return redirect("fc_authorize")
else:
return render(
request,
"aidants_connect_web/new_mandat/new_mandat.html",
{"aidant": aidant, "form": form},
)
@login_required
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.",
},
)
@login_required
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