BDS members CSV export

This commit is contained in:
Martin Pépin 2020-05-08 23:21:36 +02:00
parent e025ae2d96
commit 5f167f39a3
3 changed files with 40 additions and 0 deletions

View file

@ -9,6 +9,7 @@
<ul> <ul>
<li><a href="#todo">Chercher une personne</a></li> <li><a href="#todo">Chercher une personne</a></li>
<li><a href="{% url 'bds:members_emails' %}">Emails des adhérent⋅e⋅s</a></li> <li><a href="{% url 'bds:members_emails' %}">Emails des adhérent⋅e⋅s</a></li>
<li><a href="{% url 'bds:members_csv' %}">Export CSV des adhérent⋅e⋅s</a></li>
</ul> </ul>
</article> </article>
</section> </section>

View file

@ -7,4 +7,5 @@ app_name = "bds"
urlpatterns = [ urlpatterns = [
path("", views.home, name="home"), path("", views.home, name="home"),
path("members/emails", views.members_emails, name="members_emails"), path("members/emails", views.members_emails, name="members_emails"),
path("members/csv", views.members_csv, name="members_csv"),
] ]

View file

@ -1,3 +1,5 @@
import csv
from django.contrib.auth.decorators import permission_required from django.contrib.auth.decorators import permission_required
from django.contrib.auth.mixins import PermissionRequiredMixin from django.contrib.auth.mixins import PermissionRequiredMixin
from django.http import HttpResponse from django.http import HttpResponse
@ -20,3 +22,39 @@ def members_emails(request):
response = HttpResponse(content_type="text/plain") response = HttpResponse(content_type="text/plain")
response.write(", ".join(members.values_list("user__email", flat=True))) response.write(", ".join(members.values_list("user__email", flat=True)))
return response return response
@permission_required("bds:is_staff")
def members_csv(request):
response = HttpResponse(content_type="text/csv")
response["Content-Disposition"] = 'attachment; filename="members.csv"'
writer = csv.writer(response)
header = [
"nom",
"email",
"numéro de téléphone",
"occupation",
"département",
"date de naissance",
"Numéro ASPSL",
"Période de cotisation",
"Type de cotisation",
]
writer.writerow(header)
for profile in BDSProfile.objects.filter(is_member=True):
data = [
profile.user.get_full_name(),
profile.user.email,
profile.phone,
profile.occupation,
profile.departement,
profile.birthdate,
profile.ASPSL_number,
profile.cotisation_period,
profile.cotisation_type,
]
writer.writerow(map(lambda x: str(x) if x else "", data))
return response