Skip to content

Commit

Permalink
Merge pull request #4779 from kobotoolbox/fix-xlsx-bulk-library-import
Browse files Browse the repository at this point in the history
Fix XLSX bulk library import, broken by #3765
  • Loading branch information
noliveleger committed Dec 21, 2023
2 parents 07b087d + e68f0c1 commit 7b5c3e6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
21 changes: 16 additions & 5 deletions kpi/tests/api/v2/test_api_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ def test_import_locking_xls_as_question(self):
assert expected_content_settings == created_asset.content['settings']
assert not created_asset.content['kobo--locking-profiles']

def test_import_library_bulk_xls(self):
def _test_import_library_bulk(self, filetype='xlsx'):
library_sheet_content = [
['block', 'name', 'type', 'label', 'tag:subject:fungus', 'tag:subject:useless'],
['mushroom', 'cap', 'text', 'Describe the cap', '1', None],
Expand All @@ -785,14 +785,19 @@ def test_import_library_bulk_xls(self):
['seasons', 'fall', 'Fall'],
['seasons', 'winter', 'Winter'],
]

content = (
('library', library_sheet_content),
('choices', choices_sheet_content),
)
task_data = self._construct_xls_for_import(
content, name='Collection created from bulk library import'
)
name='Collection created from bulk library import'

if filetype == 'xls':
task_data = self._construct_xls_for_import(content, name=name)
elif filetype == 'xlsx':
task_data = self._construct_xlsx_for_import(content, name=name)
else:
raise NotImplementedError(f'{filetype} must be either xls or xlsx')

post_url = reverse('api_v2:importtask-list')
response = self.client.post(post_url, task_data)
assert response.status_code == status.HTTP_201_CREATED
Expand Down Expand Up @@ -888,6 +893,12 @@ def test_import_library_bulk_xls(self):
tagged_as_useless[1], non_block_assets[1]
)

def test_import_library_bulk_xls(self):
self._test_import_library_bulk('xls')

def test_import_library_bulk_xlsx(self):
self._test_import_library_bulk('xlsx')

def test_import_asset_xls(self):
xlsx_io = self.asset.to_xlsx_io()
task_data = {
Expand Down
17 changes: 8 additions & 9 deletions kpi/utils/rename_xls_sheet.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from io import BytesIO

import openpyxl
from xlutils.copy import copy
from xlrd import open_workbook

import xlrd
import xlutils.copy

class NoFromSheetError(Exception):
pass
Expand All @@ -22,17 +21,17 @@ def rename_xls_sheet(
sheet names in pyxform inputs;
see https://github.com/XLSForm/pyxform/issues/229.
"""
readable = open_workbook(file_contents=xls_stream.read())
writable = copy(readable)
sheet_names = readable.sheet_names()
read_only_book = xlrd.open_workbook(file_contents=xls_stream.read())
book = xlutils.copy.copy(read_only_book)
sheet_names = read_only_book.sheet_names()
if from_sheet in sheet_names and to_sheet in sheet_names:
raise ConflictSheetError()
if from_sheet not in sheet_names:
raise NoFromSheetError(from_sheet)
index = sheet_names.index(from_sheet)
writable.get_sheet(index).name = to_sheet
book.get_sheet(index).name = to_sheet
stream = BytesIO()
writable.save(stream)
book.save(stream)
stream.seek(0)
return stream

Expand All @@ -48,6 +47,6 @@ def rename_xlsx_sheet(
raise NoFromSheetError(from_sheet)
book[from_sheet].title = to_sheet
stream = BytesIO()
writable.save(stream)
book.save(stream)
stream.seek(0)
return stream

0 comments on commit 7b5c3e6

Please sign in to comment.