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 %} -
+ 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_nom | +autres colonnes facultatives et non utilisées | +
---|---|
212 | +... | +