diff --git a/apptax/admin/admin_view.py b/apptax/admin/admin_view.py index 6b8fc138..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: @@ -136,7 +137,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): @@ -172,24 +173,12 @@ def import_cd_nom_view(self, *args, **kwargs): with_header = request.form.get("with_header", default=False) file = request.files["upload"] - 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() + 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) + 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()