From e72b9341adbe21a6bb1f7951375d7cd3edf1be17 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Tue, 25 Sep 2018 19:33:45 +0200 Subject: [PATCH] new command to change csv_file (fixes #26582) --- .../apps/csvdatasource/management/__init__.py | 0 .../management/commands/__init__.py | 0 .../management/commands/change-csv.py | 42 +++++++++++++++++++ tests/test_csv_datasource.py | 19 +++++++++ 4 files changed, 61 insertions(+) create mode 100644 passerelle/apps/csvdatasource/management/__init__.py create mode 100644 passerelle/apps/csvdatasource/management/commands/__init__.py create mode 100644 passerelle/apps/csvdatasource/management/commands/change-csv.py diff --git a/passerelle/apps/csvdatasource/management/__init__.py b/passerelle/apps/csvdatasource/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/passerelle/apps/csvdatasource/management/commands/__init__.py b/passerelle/apps/csvdatasource/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/passerelle/apps/csvdatasource/management/commands/change-csv.py b/passerelle/apps/csvdatasource/management/commands/change-csv.py new file mode 100644 index 0000000..72e7d78 --- /dev/null +++ b/passerelle/apps/csvdatasource/management/commands/change-csv.py @@ -0,0 +1,42 @@ +# passerelle - uniform access to multiple data sources and services +# Copyright (C) 2017 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import os +import argparse + +from django.core.files import File +from django.core.management.base import BaseCommand, CommandError + +from passerelle.apps.csvdatasource.models import CsvDataSource + + +class Command(BaseCommand): + help = 'change CSV file' + + def add_arguments(self, parser): + parser.add_argument('slug') + parser.add_argument('csv_file', type=argparse.FileType('r')) + parser.add_argument('--sheet-name', default=None) + + def handle(self, slug, csv_file, **options): + try: + instance = CsvDataSource.objects.get(slug=slug) + except CsvDataSource.DoesNotExist: + raise CommandError('csvdatasource %r does not exist' % slug) + instance.csv_file = File(csv_file, name=os.path.basename(csv_file.name)) + if options['sheet_name']: + instance.sheet_name = options['sheet_name'] + instance.save() diff --git a/tests/test_csv_datasource.py b/tests/test_csv_datasource.py index 6ecec29..3581772 100644 --- a/tests/test_csv_datasource.py +++ b/tests/test_csv_datasource.py @@ -9,6 +9,7 @@ from django.core.files import File from django.core.urlresolvers import reverse from django.contrib.contenttypes.models import ContentType from django.test import Client +from django.core.management import call_command from passerelle.base.models import ApiUser, AccessRight from test_manager import login, admin_user @@ -672,3 +673,21 @@ def test_csv_sniffer(admin_user, app): form.set('csv_file', webtest.Upload('test.csv', '\n', 'application/octet-stream')) resp = form.submit() assert 'Could not detect CSV dialect' in resp + + +def test_change_csv_command(setup): + csv, url = setup(data=data) + call_command('change-csv', 'test', os.path.join(TEST_BASE_DIR, 'data-empty.ods')) + csv.refresh_from_db() + assert list(csv.get_rows()) == [] + call_command('change-csv', 'test', os.path.join(TEST_BASE_DIR, 'data.csv')) + csv.refresh_from_db() + assert list(csv.get_rows()) != [] + + call_command('change-csv', 'test', os.path.join(TEST_BASE_DIR, 'data-empty.ods')) + csv.refresh_from_db() + assert list(csv.get_rows()) == [] + + call_command('change-csv', 'test', os.path.join(TEST_BASE_DIR, 'data.ods'), sheet_name='Feuille2') + csv.refresh_from_db() + assert list(csv.get_rows()) != [] -- 2.18.0