From 768a3522ada5cd90f0d1fdd1779e1a74184bbb55 Mon Sep 17 00:00:00 2001 From: amandine-sahl Date: Mon, 31 Jul 2023 18:42:04 +0200 Subject: [PATCH 1/2] Add test on populate bib_list --- apptax/admin/admin_view.py | 27 ++++------- .../templates/admin/populate_biblist.html | 29 +++++++++++- apptax/admin/utils.py | 47 +++++++++++++++++++ 3 files changed, 83 insertions(+), 20 deletions(-) diff --git a/apptax/admin/admin_view.py b/apptax/admin/admin_view.py index 6b8fc138..b909438e 100644 --- a/apptax/admin/admin_view.py +++ b/apptax/admin/admin_view.py @@ -136,7 +136,7 @@ def render(self, template, **kwargs): class PopulateBibListesForm(Form): delimiter = SelectField(label="Delimiter", choices=[(",", ","), (";", ";")]) with_header = BooleanField(label="With header") - upload = FileUploadField("File") + upload = FileUploadField(label="File", allowed_extensions=("csv",)) class BibListesView(FlaskAdminProtectedMixin, ModelView): @@ -171,25 +171,14 @@ def import_cd_nom_view(self, *args, **kwargs): delimiter = request.form.get("delimiter", default=",") with_header = request.form.get("with_header", default=False) file = request.files["upload"] + from .utils import PopulateBibListeException, populate_bib_liste + + try: + populate_bib_liste(id_list, delimiter, with_header, file) + except PopulateBibListeException as e: + flash(e.message, "error") + return self.render("admin/populate_biblist.html", form=form) - fstring = file.read().decode() - inputcsv = csv.reader(fstring.splitlines(), delimiter=delimiter) - - bibliste = BibListes.query.get(id_list) - # if header skip first line - if with_header: - next(inputcsv, None) - for row in inputcsv: - try: - cd_nom = int(row[0]) - except (TypeError, ValueError): - flash(f"Invalid cd_nom value: {row[0]}") - return self.render("admin/populate_biblist.html", form=form) - tax = Taxref.query.get(cd_nom) - if tax: - tax.liste.append(bibliste) - - db.session.commit() return redirect(self.get_url(".index_view")) return self.render("admin/populate_biblist.html", form=form) diff --git a/apptax/admin/templates/admin/populate_biblist.html b/apptax/admin/templates/admin/populate_biblist.html index 8a1868d7..f4308ec5 100644 --- a/apptax/admin/templates/admin/populate_biblist.html +++ b/apptax/admin/templates/admin/populate_biblist.html @@ -6,12 +6,39 @@ {{ super() }} {% endblock %} -

Populate bib list

+

Peupler la liste

+ +
+ Instructions +

+ Pour ajouter des cd_nom à une liste, il faut avoir un fichier csv contenant une colonne. + Cette colonne doit correspondre à une liste de cd_nom +

+ +
+ + + + + + + + + + + + + +
cd_nomautres colonnes facultatives et non utilisées
212...
+
+
+
{% block edit_form %} {% call lib.form_tag(action=action) %} {{ lib.render_form_fields(form, form_opts=form_opts) }} {{ lib.render_form_buttons(cancel_url, extra, is_modal) }} {% endcall %} {% endblock %} +
{% endblock %} diff --git a/apptax/admin/utils.py b/apptax/admin/utils.py index d4b00ae7..f69c2a6f 100644 --- a/apptax/admin/utils.py +++ b/apptax/admin/utils.py @@ -1,8 +1,12 @@ +import csv from werkzeug.utils import secure_filename from pypnusershub.db.models import AppUser, Application from pypnusershub.utils import get_current_app_id +from apptax.taxonomie.models import BibListes, Taxref +from apptax.database import db + def taxref_media_file_name(obj, file_data): """ @@ -24,3 +28,46 @@ def get_user_permission(id_role): AppUser.id_role == id_role ) return query.scalar() + + +class PopulateBibListeException(Exception): + def __init__(self, message): + self.message = message + + def __str__(self): + return self.message + + +def populate_bib_liste(id_list, delimiter, with_header, file): + if not ("." in file.filename and file.filename.rsplit(".", 1)[1].lower() == "csv"): + raise PopulateBibListeException("Format de fichier requis : CSV") + + try: + fstring = file.read().decode() + inputcsv = csv.reader(fstring.splitlines(), delimiter=delimiter) + except Exception: + raise PopulateBibListeException("Lecture du fichier impossible") + + bibliste = BibListes.query.get(id_list) + + # if header skip first line + if with_header: + next(inputcsv, None) + + for row in inputcsv: + try: + cd_nom = int(row[0]) + except (TypeError, ValueError): + msg = "Invalid cd_nom value: {row[0]}" + if not row[0].isnumeric(): + msg = """ + Il semble que votre fichier contienent le nom des colonnes, + sélectionner l'option 'with header' + ou que la première colonne ne corresponde pas à une liste de cd_nom""" + raise PopulateBibListeException(msg) + + tax = Taxref.query.get(cd_nom) + if tax: + tax.liste.append(bibliste) + + db.session.commit() From 2697252ed1e6b7e9ddceffe970f3bf0c73793c7b Mon Sep 17 00:00:00 2001 From: TheoLechemia Date: Tue, 1 Aug 2023 10:48:02 +0200 Subject: [PATCH 2/2] move import --- apptax/admin/admin_view.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apptax/admin/admin_view.py b/apptax/admin/admin_view.py index b909438e..1c954cba 100644 --- a/apptax/admin/admin_view.py +++ b/apptax/admin/admin_view.py @@ -50,6 +50,7 @@ from apptax.admin.utils import taxref_media_file_name, get_user_permission from pypnusershub.utils import get_current_app_id from apptax.admin.admin import adresses +from apptax.admin.utils import PopulateBibListeException, populate_bib_liste class FlaskAdminProtectedMixin: @@ -171,7 +172,6 @@ def import_cd_nom_view(self, *args, **kwargs): delimiter = request.form.get("delimiter", default=",") with_header = request.form.get("with_header", default=False) file = request.files["upload"] - from .utils import PopulateBibListeException, populate_bib_liste try: populate_bib_liste(id_list, delimiter, with_header, file)