Add export admin action for candidates
This commit is contained in:
parent
c0bd7a70da
commit
a0a35a4b35
1 changed files with 34 additions and 1 deletions
|
|
@ -1,7 +1,39 @@
|
|||
from collections import OrderedDict
|
||||
|
||||
from django import forms
|
||||
from django.contrib import admin
|
||||
from django.db.models import BooleanField
|
||||
|
||||
from .models import Candidate
|
||||
from stages.exports import OpenXMLExport
|
||||
from .models import Candidate, GENDER_CHOICES
|
||||
|
||||
|
||||
def export_candidates(modeladmin, request, queryset):
|
||||
"""
|
||||
Export all candidates fields.
|
||||
"""
|
||||
export_fields = OrderedDict(
|
||||
[(f.verbose_name, f.name) for f in Candidate._meta.get_fields() if f.name != 'ID']
|
||||
)
|
||||
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 = 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 field_name in boolean_fields:
|
||||
value = 'Oui' if value else ''
|
||||
values.append(value)
|
||||
export.write_line(values)
|
||||
return export.get_http_response('candidats_export')
|
||||
|
||||
export_candidates.short_description = "Exporter les candidats sélectionnés"
|
||||
|
||||
|
||||
class CandidateAdminForm(forms.ModelForm):
|
||||
|
|
@ -19,6 +51,7 @@ class CandidateAdmin(admin.ModelAdmin):
|
|||
list_display = ('last_name', 'first_name', 'section', 'confirm_email')
|
||||
list_filter = ('section', 'option')
|
||||
readonly_fields = ('total_result_points', 'total_result_mark', 'date_confirmation_mail')
|
||||
actions = [export_candidates]
|
||||
fieldsets = (
|
||||
(None, {
|
||||
'fields': (('first_name', 'last_name', 'gender'),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue