From d4072450fafd9a5e3f64b47ecb447c786f29a6bd Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Wed, 7 Nov 2012 14:06:15 +0100 Subject: [PATCH] Added export functionality --- common/urls.py | 1 + requirements.txt | 1 + stages/views.py | 35 ++++++++++++++++ templates/admin/index.html | 86 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 123 insertions(+) create mode 100644 requirements.txt create mode 100644 templates/admin/index.html diff --git a/common/urls.py b/common/urls.py index 0235cb9..693382a 100644 --- a/common/urls.py +++ b/common/urls.py @@ -16,6 +16,7 @@ urlpatterns = patterns('', url(r'^admin/', include(admin.site.urls)), url(r'^attribution/$', views.AttributionView.as_view(), name='attribution'), + url(r'^stages/export/$', 'stages.views.stages_export', name='stages_export'), # AJAX/JSON urls url(r'^section/(?P\d+)/periods/', 'stages.views.section_periods'), diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..794cc3d --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +openpyxl diff --git a/stages/views.py b/stages/views.py index 9f21fa9..8b603fb 100644 --- a/stages/views.py +++ b/stages/views.py @@ -1,3 +1,6 @@ +# -*- encoding: utf-8 -*- +from __future__ import unicode_literals + import json from django.http import HttpResponse, HttpResponseNotAllowed @@ -65,3 +68,35 @@ def new_training(request): corporation=Corporation.objects.get(pk=request.POST.get('corp')) ) return HttpResponse('OK') + + +def stages_export(request): + from datetime import date + from openpyxl import Workbook + from openpyxl.writer.excel import save_virtual_workbook + + export_fields = [ + ('Prénom', 'student__first_name'), ('Nom', 'student__last_name'), + ('Filière', 'period__section__name'), + ('Début', 'period__start_date'), ('Fin', 'period__end_date'), + ('Institution', 'corporation__name'), + ('Domaine', 'domain__name'), + ('Prénom référent', 'referent__first_name'), ('Nom référent', 'referent__last_name') + ] + + wb = Workbook() + ws = wb.get_active_sheet() + ws.title = 'Stages' + # Headers + for col_idx, header in enumerate([f[0] for f in export_fields]): + ws.cell(row=0, column=col_idx).value = header + ws.cell(row=0, column=col_idx).style.font.bold = True + # Data + for row_idx, tr in enumerate(Training.objects.all().values_list(*[f[1] for f in export_fields]), start=1): + for col_idx, field in enumerate(tr): + ws.cell(row=row_idx, column=col_idx).value = field + + response = HttpResponse(save_virtual_workbook(wb), mimetype='application/ms-excel') + response['Content-Disposition'] = 'attachment; filename=%s%s.xlsx' % ( + 'stages_export_', date.strftime(date.today(), '%Y-%m-%d')) + return response diff --git a/templates/admin/index.html b/templates/admin/index.html new file mode 100644 index 0000000..79a4eef --- /dev/null +++ b/templates/admin/index.html @@ -0,0 +1,86 @@ +{% extends "admin/base_site.html" %} +{% load i18n admin_static %} +{% load url from future %} + +{% block extrastyle %}{{ block.super }}{% endblock %} + +{% block coltype %}colMS{% endblock %} + +{% block bodyclass %}dashboard{% endblock %} + +{% block breadcrumbs %}{% endblock %} + +{% block content %} +
+ +{% if app_list %} + {% for app in app_list %} +
+ + + {% for model in app.models %} + + {% if model.admin_url %} + + {% else %} + + {% endif %} + + {% if model.add_url %} + + {% else %} + + {% endif %} + + {% if model.admin_url %} + + {% else %} + + {% endif %} + + {% endfor %} +
+ + {% blocktrans with name=app.name %}{{ name }}{% endblocktrans %} + +
{{ model.name }}{{ model.name }}{% trans 'Add' %} {% trans 'Change' %} 
+
+ {% endfor %} +

Exporter les données de stages

+{% else %} +

{% trans "You don't have permission to edit anything." %}

+{% endif %} +
+{% endblock %} + +{% block sidebar %} + +{% endblock %}