From d219e09c453eadcfee70e4646518e9999b56faae Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Wed, 14 Feb 2018 19:08:03 +0100 Subject: [PATCH] Export some more fields in Candidate export --- candidats/admin.py | 28 ++++++++++++++++++++++------ candidats/tests.py | 20 ++++++++++++++++++++ 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/candidats/admin.py b/candidats/admin.py index 24b5f0d..e47c782 100644 --- a/candidats/admin.py +++ b/candidats/admin.py @@ -7,7 +7,10 @@ from django.utils.html import format_html from stages.exports import OpenXMLExport from .forms import CandidateForm -from .models import Candidate, Interview, GENDER_CHOICES +from .models import ( + Candidate, Interview, GENDER_CHOICES, DIPLOMA_CHOICES, DIPLOMA_STATUS_CHOICES, + SECTION_CHOICES, OPTION_CHOICES, AES_ACCORDS_CHOICES, RESIDENCE_PERMITS_CHOICES, +) def export_candidates(modeladmin, request, queryset): @@ -16,20 +19,33 @@ def export_candidates(modeladmin, request, queryset): """ export_fields = OrderedDict([ (getattr(f, 'verbose_name', f.name), f.name) - for f in Candidate._meta.get_fields() if f.name != 'ID' + for f in Candidate._meta.get_fields() if f.name not in ('ID', 'interview') ]) - boolean_fields = [f.name for f in Candidate._meta.get_fields() if isinstance(f, BooleanField)] export_fields['Employeur'] = 'corporation__name' - export_fields['Employeur_localite'] = 'corporation__city' export_fields['FEE/FPP'] = 'instructor__last_name' + export_fields['Prof. entretien'] = 'interview__teacher_int__abrev' + export_fields['Correct. dossier'] = 'examination_teacher__abrev' + export_fields['Prof. dossier'] = 'interview__teacher_file__abrev' + export_fields['Date entretien'] = 'interview__date' + export_fields['Salle entretien'] = 'interview__room' + boolean_fields = [f.name for f in Candidate._meta.get_fields() if isinstance(f, BooleanField)] + choice_fields = { + 'gender': dict(GENDER_CHOICES), + 'section': dict(SECTION_CHOICES), + 'option': dict(OPTION_CHOICES), + 'diploma': dict(DIPLOMA_CHOICES), + 'diploma_status': dict(DIPLOMA_STATUS_CHOICES), + 'aes_accords': dict(AES_ACCORDS_CHOICES), + 'residence_permits': dict(RESIDENCE_PERMITS_CHOICES), + } export = OpenXMLExport('Exportation') export.write_line(export_fields.keys(), bold=True) for cand in queryset.values_list(*export_fields.values()): values = [] for value, field_name in zip(cand, export_fields.values()): - if field_name == 'gender': - value = dict(GENDER_CHOICES)[value] + if value != '' and field_name in choice_fields: + value = choice_fields[field_name][value] if field_name in boolean_fields: value = 'Oui' if value else '' values.append(value) diff --git a/candidats/tests.py b/candidats/tests.py index f0f297d..044814b 100644 --- a/candidats/tests.py +++ b/candidats/tests.py @@ -6,6 +6,7 @@ from django.core import mail from django.test import TestCase from django.urls import reverse +from stages.exports import openxml_contenttype from stages.models import Section, Teacher from .models import Candidate, Interview @@ -314,3 +315,22 @@ tél. 032 886 33 00 ) self.assertEqual(response['Content-Type'], 'application/pdf') self.assertGreater(len(response.content), 200) + + def test_export_candidates(self): + ede = Section.objects.create(name='EDE') + Candidate.objects.create( + first_name='Henri', last_name='Dupond', gender='M', section=ede, + email='henri@example.org', deposite_date=date.today() + ) + Candidate.objects.create( + first_name='Joé', last_name='Glatz', gender='F', section=ede, + email='joe@example.org', deposite_date=date.today() + ) + + change_url = reverse('admin:candidats_candidate_changelist') + self.client.login(username='me', password='mepassword') + response = self.client.post(change_url, { + 'action': 'export_candidates', + '_selected_action': Candidate.objects.values_list('pk', flat=True) + }, follow=True) + self.assertEqual(response['Content-Type'], openxml_contenttype)