From 2a6d63cf03db2002d849e1527607f4940feffe99 Mon Sep 17 00:00:00 2001 From: Arthur ZHU Date: Fri, 24 Dec 2021 18:10:19 +0100 Subject: [PATCH] export liste entreprises --- app/entreprises/models.py | 9 +++++++ app/entreprises/routes.py | 28 ++++++++++++++++++---- app/templates/entreprises/entreprises.html | 7 +++++- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/app/entreprises/models.py b/app/entreprises/models.py index ec963be20..d88a88127 100644 --- a/app/entreprises/models.py +++ b/app/entreprises/models.py @@ -12,6 +12,15 @@ class Entreprise(db.Model): contacts = db.relationship('EntrepriseContact', backref='entreprise', lazy='dynamic', cascade="all, delete-orphan") offres = db.relationship('EntrepriseOffre', backref='entreprise', lazy='dynamic', cascade="all, delete-orphan") + def to_dict(self): + return { + "siret": self.siret, + "nom": self.nom, + "adresse": self.adresse, + "codepostal": self.codepostal, + "pays": self.pays + } + class EntrepriseContact(db.Model): __tablename__ = "entreprise_contact" id = db.Column(db.Integer, primary_key=True) diff --git a/app/entreprises/routes.py b/app/entreprises/routes.py index 7570c806d..118e7e032 100644 --- a/app/entreprises/routes.py +++ b/app/entreprises/routes.py @@ -1,9 +1,10 @@ -from flask import render_template, redirect, url_for, request, flash +from flask import render_template, redirect, url_for, request, flash, send_file from flask.json import jsonify from flask_login import current_user from app.decorators import permission_required -from app.entreprises import LOGS_LEN + from app.scodoc.sco_permissions import Permission +from app.entreprises import LOGS_LEN from app.entreprises.forms import ( EntrepriseCreationForm, EntrepriseModificationForm, @@ -30,7 +31,8 @@ from app.models import ( from app.auth.models import User from app.scodoc.sco_find_etud import search_etud_by_name from app import db -from app.scodoc import sco_etud +from app.scodoc import sco_etud, sco_excel +import app.scodoc.sco_utils as scu from sqlalchemy import text @bp.route("/", methods=["GET"]) @@ -402,4 +404,22 @@ def json_responsables(): } list.append(content) content = {} - return jsonify(results=list) \ No newline at end of file + return jsonify(results=list) + +@bp.route("/export_entreprises") +def export_entreprises(): + entreprises = Entreprise.query.all() + keys=[ + "siret", + "nom", + "adresse", + "ville", + "codepostal", + "pays" + ] + titles = keys[:] + L = [[entreprise.to_dict().get(k, "") for k in keys] for entreprise in entreprises] + title = "entreprises" + xlsx = sco_excel.excel_simple_table(titles=titles, lines=L, sheet_name=title) + filename = title + return scu.send_file(xlsx, filename, scu.XLSX_SUFFIX, scu.XLSX_MIMETYPE) \ No newline at end of file diff --git a/app/templates/entreprises/entreprises.html b/app/templates/entreprises/entreprises.html index a33dd7e77..62d1fc11a 100644 --- a/app/templates/entreprises/entreprises.html +++ b/app/templates/entreprises/entreprises.html @@ -53,6 +53,11 @@
{% endif %} - Ajouter une entreprise +
+ Ajouter une entreprise + {% if entreprises %} + Exporter la liste des entreprises + {% endif %} +
{% endblock %} \ No newline at end of file