|
13 | 13 | from django.core import serializers |
14 | 14 | from django.core.paginator import Paginator |
15 | 15 | from django.db import connection, transaction |
16 | | -from django.db.models import Q, Count, Case, When, IntegerField, Value |
| 16 | +from django.db.models import Q, Count, Case, When, IntegerField, Value, F, Aggregate, CharField |
| 17 | +from django.db.models.functions import Concat |
17 | 18 | from django.forms import modelformset_factory |
18 | 19 | from django.http import JsonResponse, Http404, HttpResponse |
19 | 20 | from django.shortcuts import get_object_or_404, redirect, render |
@@ -1684,6 +1685,63 @@ def get(self, request, *args, **kwargs): |
1684 | 1685 | return response |
1685 | 1686 |
|
1686 | 1687 |
|
| 1688 | +class GroupConcat(Aggregate): |
| 1689 | + function = 'GROUP_CONCAT' |
| 1690 | + template = "%(function)s(%(expressions)s, '%(separator)s')" |
| 1691 | + |
| 1692 | + def __init__(self, expression, separator=', ', **extra): |
| 1693 | + super().__init__( |
| 1694 | + expression, |
| 1695 | + separator=separator, |
| 1696 | + output_field=CharField(), |
| 1697 | + **extra |
| 1698 | + ) |
| 1699 | + |
| 1700 | +class EvenementExport(FabView): |
| 1701 | + permission_required = 'orgues.view_user' |
| 1702 | + |
| 1703 | + def get(self, request, *args, **kwargs): |
| 1704 | + response = HttpResponse(content_type='text/csv') |
| 1705 | + response.write(u'\ufeff'.encode('utf8')) |
| 1706 | + response['Content-Disposition'] = 'attachment;filename=evenements_orgue_{}.csv'.format( |
| 1707 | + datetime.today().strftime("%Y-%m-%d")) |
| 1708 | + columns = [ |
| 1709 | + "annee", |
| 1710 | + "annee_fin", |
| 1711 | + "nom_orgue", |
| 1712 | + "circa", |
| 1713 | + "type", |
| 1714 | + "nom_facteurs", |
| 1715 | + "nom_manufactures", |
| 1716 | + ] |
| 1717 | + writer = csv.DictWriter(response, delimiter=';', fieldnames=columns) |
| 1718 | + |
| 1719 | + # header from verbose_names |
| 1720 | + row = {} |
| 1721 | + for column in columns: |
| 1722 | + try: |
| 1723 | + field = Evenement._meta.get_field(column) |
| 1724 | + row[column] = field.verbose_name |
| 1725 | + except: |
| 1726 | + # Pour les colonnes "virtuelles", on met un nom lisible par défaut |
| 1727 | + row[column] = column.replace('_', ' ').capitalize() |
| 1728 | + writer.writerow(row) |
| 1729 | + writer.writerows( |
| 1730 | + Evenement.objects.annotate( |
| 1731 | + nom_orgue = Concat( |
| 1732 | + F('orgue__designation'), |
| 1733 | + Value(' '), |
| 1734 | + F('orgue__edifice'), |
| 1735 | + Value(' '), |
| 1736 | + F('orgue__commune') |
| 1737 | + ), |
| 1738 | + nom_facteurs = GroupConcat('facteurs__nom', separator=', '), |
| 1739 | + nom_manufactures = GroupConcat('manufactures__nom', separator=', '), |
| 1740 | + ).values(*columns).order_by("annee") |
| 1741 | + ) |
| 1742 | + return response |
| 1743 | + |
| 1744 | + |
1687 | 1745 | class Dashboard(FabView): |
1688 | 1746 | """ |
1689 | 1747 | Dashboard d'avancement réservé aux admins |
|
0 commit comments