0001-new-command-to-change-csv_file-fixes-26582.patch
passerelle/apps/csvdatasource/management/commands/change-csv.py | ||
---|---|---|
1 |
# passerelle - uniform access to multiple data sources and services |
|
2 |
# Copyright (C) 2017 Entr'ouvert |
|
3 |
# |
|
4 |
# This program is free software: you can redistribute it and/or modify it |
|
5 |
# under the terms of the GNU Affero General Public License as published |
|
6 |
# by the Free Software Foundation, either version 3 of the License, or |
|
7 |
# (at your option) any later version. |
|
8 |
# |
|
9 |
# This program is distributed in the hope that it will be useful, |
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
# GNU Affero General Public License for more details. |
|
13 |
# |
|
14 |
# You should have received a copy of the GNU Affero General Public License |
|
15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | ||
17 |
import os |
|
18 |
import argparse |
|
19 | ||
20 |
from django.core.files import File |
|
21 |
from django.core.management.base import BaseCommand, CommandError |
|
22 | ||
23 |
from passerelle.apps.csvdatasource.models import CsvDataSource |
|
24 | ||
25 | ||
26 |
class Command(BaseCommand): |
|
27 |
help = 'change CSV file' |
|
28 | ||
29 |
def add_arguments(self, parser): |
|
30 |
parser.add_argument('slug') |
|
31 |
parser.add_argument('csv_file', type=argparse.FileType('r')) |
|
32 |
parser.add_argument('--sheet-name', default=None) |
|
33 | ||
34 |
def handle(self, slug, csv_file, **options): |
|
35 |
try: |
|
36 |
instance = CsvDataSource.objects.get(slug=slug) |
|
37 |
except CsvDataSource.DoesNotExist: |
|
38 |
raise CommandError('csvdatasource %r does not exist' % slug) |
|
39 |
instance.csv_file = File(csv_file, name=os.path.basename(csv_file.name)) |
|
40 |
if options['sheet_name']: |
|
41 |
instance.sheet_name = options['sheet_name'] |
|
42 |
instance.save() |
tests/test_csv_datasource.py | ||
---|---|---|
9 | 9 |
from django.core.urlresolvers import reverse |
10 | 10 |
from django.contrib.contenttypes.models import ContentType |
11 | 11 |
from django.test import Client |
12 |
from django.core.management import call_command |
|
12 | 13 | |
13 | 14 |
from passerelle.base.models import ApiUser, AccessRight |
14 | 15 |
from test_manager import login, admin_user |
... | ... | |
672 | 673 |
form.set('csv_file', webtest.Upload('test.csv', '\n', 'application/octet-stream')) |
673 | 674 |
resp = form.submit() |
674 | 675 |
assert 'Could not detect CSV dialect' in resp |
676 | ||
677 | ||
678 |
def test_change_csv_command(setup): |
|
679 |
csv, url = setup(data=data) |
|
680 |
call_command('change-csv', 'test', os.path.join(TEST_BASE_DIR, 'data-empty.ods')) |
|
681 |
csv.refresh_from_db() |
|
682 |
assert list(csv.get_rows()) == [] |
|
683 |
call_command('change-csv', 'test', os.path.join(TEST_BASE_DIR, 'data.csv')) |
|
684 |
csv.refresh_from_db() |
|
685 |
assert list(csv.get_rows()) != [] |
|
686 | ||
687 |
call_command('change-csv', 'test', os.path.join(TEST_BASE_DIR, 'data-empty.ods')) |
|
688 |
csv.refresh_from_db() |
|
689 |
assert list(csv.get_rows()) == [] |
|
690 | ||
691 |
call_command('change-csv', 'test', os.path.join(TEST_BASE_DIR, 'data.ods'), sheet_name='Feuille2') |
|
692 |
csv.refresh_from_db() |
|
693 |
assert list(csv.get_rows()) != [] |
|
675 |
- |