From f3728719809bca5d0418070e56327751d8f7a3aa Mon Sep 17 00:00:00 2001 From: ThomasBidot <77505438+ThomasBidot@users.noreply.github.com> Date: Fri, 12 Mar 2021 10:39:10 +0100 Subject: [PATCH] Sortie thomas (export CSV sorties) (#33) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Updated student admin filters (#28) * Password reset feature (#8) * Add Django Rest auth module * Try to make the send reset password email work * Modified template mail for reset * Add Django Rest auth module * Try to make the send reset password email work * Modified template mail for reset * test * Added utf-8 support to exported csv and switched delimiter from , to ; in admin interface * Disabled emails while in dev * Added multi selection filter in admin * Fixed mail settings * Added year field to Tutor serializer * Fixed year updated before registration form filled * commit for automatic deploy * Testing CI * Added filtering in admin for registration validation * Added filter to student admin Co-authored-by: chiahetcho <44137047+chiahetcho@users.noreply.github.com> Co-authored-by: florimondmanca <florimond.manca@gmail.com> Co-authored-by: Arthur Guédon <arthur.guedon@student-cs.fr> Co-authored-by: Arthur Guédon <60623551+arthurgdn@users.noreply.github.com> * Ajout filtre sur register * Ajout filtre participation * Ajout de 3 filtres par etablissements * Ajout 3 filtres pour etablissement V2 * Ajout export CSV sorties Co-authored-by: Seon82 <46298009+Seon82@users.noreply.github.com> Co-authored-by: chiahetcho <44137047+chiahetcho@users.noreply.github.com> Co-authored-by: florimondmanca <florimond.manca@gmail.com> Co-authored-by: Arthur Guédon <arthur.guedon@student-cs.fr> Co-authored-by: Arthur Guédon <60623551+arthurgdn@users.noreply.github.com> Co-authored-by: Bidot-Naude Thomas <thomas.bidotnaude@student-cs.fr> --- register/admin.py | 19 +++++++++++++++++++ visits/admin.py | 30 ++++++++++++++++++++++-------- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/register/admin.py b/register/admin.py index 95a4138..482cba4 100644 --- a/register/admin.py +++ b/register/admin.py @@ -5,6 +5,25 @@ from .models import Registration from profiles.models import Student +class SchoolFilter(admin.SimpleListFilter): + title = 'établissement' + parameter_name = 'profiles__school' + + def lookups(self, request, model_admin): + list_of_school = [] + query = Student.objects.values_list( + "school", flat=True).distinct() + for school in query: + list_of_school.append((school, school)) + return list_of_school + + def queryset(self, request, queryset): + if self.value(): + emails = Student.objects.filter( + school=self.value()).values_list("user__email", flat=True) + return queryset.filter(email__in=emails) + + class SchoolFilter(admin.SimpleListFilter): title = 'établissement' parameter_name = 'profiles__school' diff --git a/visits/admin.py b/visits/admin.py index a7b6392..7d49e11 100644 --- a/visits/admin.py +++ b/visits/admin.py @@ -9,6 +9,8 @@ from django.http import HttpResponse import csv from .models import Participation, Place, Visit from profiles.models import Student +from users.models import User +import codecs # Register your models here. @@ -154,8 +156,6 @@ class ParticipationAdmin(admin.ModelAdmin): list_display = ('submitted', 'visit', 'user_link', 'accepted', 'present') list_filter = (SchoolFilter, 'submitted', 'accepted', 'present') - - actions = [accept_selected_participations, reject_selected_participations] def user_link(self, participation: Participation): @@ -179,17 +179,30 @@ class ParticipationAdmin(admin.ModelAdmin): def export_as_csv(self, request, queryset): meta = self.model._meta field_names = [field.name for field in meta.fields] - response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename={}.csv'.format( meta) - writer = csv.writer(response) - writer.writerow(field_names) + response.write(codecs.BOM_UTF8) # force response to be UTF-8 + writer = csv.writer(response, delimiter=';') + + writer.writerow(['first_name', 'last_name', 'school', + 'phone_number', 'scholarship'] + field_names) + + list_email = queryset.values_list("user__email", flat=True) + nb_user = 0 for obj in queryset: - row = writer.writerow([getattr(obj, field) - for field in field_names]) + + name = User.objects.filter( + email=str(list_email[nb_user])).values('first_name', 'last_name', 'phone_number') + school = Student.objects.filter( + user__email=str(list_email[nb_user])).values('school', 'scholarship') + + row = writer.writerow([name[0]['first_name'], name[0]['last_name'], school[0]['school'], name[0]['phone_number'], school[0]['scholarship']] + [getattr(obj, field) + for field in field_names]) + nb_user += 1 return response - export_as_csv.short_description = "Exporter au format CSV" + + export_as_csv.short_description = "Exporter sélection (en .csv)" @@ -226,6 +239,7 @@ class VisitAdmin(admin.ModelAdmin): return obj.participants.count() num_participants.short_description = 'Participants' + @admin.register(Place) class PlaceAdmin(admin.ModelAdmin): """Admin panel for places.""" -- GitLab