Added export functionality

This commit is contained in:
Claude Paroz 2012-11-07 14:06:15 +01:00
parent a349a05437
commit d4072450fa
4 changed files with 123 additions and 0 deletions

View file

@ -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<pk>\d+)/periods/', 'stages.views.section_periods'),

1
requirements.txt Normal file
View file

@ -0,0 +1 @@
openpyxl

View file

@ -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

View file

@ -0,0 +1,86 @@
{% extends "admin/base_site.html" %}
{% load i18n admin_static %}
{% load url from future %}
{% block extrastyle %}{{ block.super }}<link rel="stylesheet" type="text/css" href="{% static "admin/css/dashboard.css" %}" />{% endblock %}
{% block coltype %}colMS{% endblock %}
{% block bodyclass %}dashboard{% endblock %}
{% block breadcrumbs %}{% endblock %}
{% block content %}
<div id="content-main">
{% if app_list %}
{% for app in app_list %}
<div class="module">
<table>
<caption>
<a href="{{ app.app_url }}" class="section" title="{% blocktrans with name=app.name %}Models in the {{ name }} application{% endblocktrans %}">
{% blocktrans with name=app.name %}{{ name }}{% endblocktrans %}
</a>
</caption>
{% for model in app.models %}
<tr>
{% if model.admin_url %}
<th scope="row"><a href="{{ model.admin_url }}">{{ model.name }}</a></th>
{% else %}
<th scope="row">{{ model.name }}</th>
{% endif %}
{% if model.add_url %}
<td><a href="{{ model.add_url }}" class="addlink">{% trans 'Add' %}</a></td>
{% else %}
<td>&nbsp;</td>
{% endif %}
{% if model.admin_url %}
<td><a href="{{ model.admin_url }}" class="changelink">{% trans 'Change' %}</a></td>
{% else %}
<td>&nbsp;</td>
{% endif %}
</tr>
{% endfor %}
</table>
</div>
{% endfor %}
<p><a href="{% url 'stages_export' %}">Exporter les données de stages</a></p>
{% else %}
<p>{% trans "You don't have permission to edit anything." %}</p>
{% endif %}
</div>
{% endblock %}
{% block sidebar %}
<div id="content-related">
<div class="module" id="recent-actions-module">
<h2>{% trans 'Recent Actions' %}</h2>
<h3>{% trans 'My Actions' %}</h3>
{% load log %}
{% get_admin_log 10 as admin_log for_user user %}
{% if not admin_log %}
<p>{% trans 'None available' %}</p>
{% else %}
<ul class="actionlist">
{% for entry in admin_log %}
<li class="{% if entry.is_addition %}addlink{% endif %}{% if entry.is_change %}changelink{% endif %}{% if entry.is_deletion %}deletelink{% endif %}">
{% if entry.is_deletion or not entry.get_admin_url %}
{{ entry.object_repr }}
{% else %}
<a href="{{ entry.get_admin_url }}">{{ entry.object_repr }}</a>
{% endif %}
<br/>
{% if entry.content_type %}
<span class="mini quiet">{% filter capfirst %}{% trans entry.content_type.name %}{% endfilter %}</span>
{% else %}
<span class="mini quiet">{% trans 'Unknown content' %}</span>
{% endif %}
</li>
{% endfor %}
</ul>
{% endif %}
</div>
</div>
{% endblock %}