Skip to content

Commit

Permalink
Allow separate import of English concepts and translations
Browse files Browse the repository at this point in the history
  • Loading branch information
arielpontes committed Jan 7, 2021
1 parent 5c84eb7 commit 2058e66
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 24 deletions.
51 changes: 29 additions & 22 deletions gemet/thesaurus/import_spreadsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def __init__(self, import_obj):

@transaction.atomic
def import_file(self):
""" Imports data from file and returns string with results """
self.concept_ns = Namespace.objects.get(heading='Concepts')
self.group_ns = Namespace.objects.get(heading='Groups')
# Number of regular concepts before
Expand All @@ -85,44 +86,50 @@ def import_file(self):
self.version = Version.under_work()
# Keep a cache with a reference to all created concepts
self.concepts = {}
# The first sheet must have the original English concepts
concepts_sheetname = wb.sheetnames[0]
# All other sheets must have translations
translation_sheetnames = wb.sheetnames[1:]

print('Creating concepts...')
self._create_concepts(wb[concepts_sheetname])
results = ""

print('Creating relations...')
self._create_relations(wb[concepts_sheetname])
# The 'EN' sheet must have the original English concepts
if 'EN' in wb.sheetnames:
print('Creating concepts...')
self._create_concepts(wb['EN'])

num_reg_concepts_after = Concept.objects.filter(
namespace=self.concept_ns
).count()
num_groups_after = Concept.objects.filter(
namespace=self.group_ns
).count()
print('Creating relations...')
self._create_relations(wb['EN'])

report = "Created {} regular concepts and {} group concepts.".format(
num_reg_concepts_after - num_reg_concepts_bef,
num_groups_after - num_groups_bef,
)
num_reg_concepts_after = Concept.objects.filter(
namespace=self.concept_ns
).count()
num_groups_after = Concept.objects.filter(
namespace=self.group_ns
).count()

results = (
"Created {} regular concepts and {} group concepts."
).format(
num_reg_concepts_after - num_reg_concepts_bef,
num_groups_after - num_groups_bef,
)

# All other sheets must have translations
translation_sheetnames = [sn for sn in wb.sheetnames if sn != 'EN']

if translation_sheetnames:
print('Creating translations...')
for sheetname in translation_sheetnames:
print(' {}...'.format(sheetname))
self._add_translations(wb[sheetname])

report += (
"\n\nTranslations for the following {} languages"
" were also created: {}."
if results:
results += '\n\n'
results += (
"Created translations for the following {} languages: {}."
).format(
len(translation_sheetnames),
', '.join(translation_sheetnames)
)

return report
return results

def _create_concepts(self, sheet):
for i, row in enumerate(row_dicts(sheet)):
Expand Down
Binary file added gemet/thesaurus/tests/files/only_en.xlsx
Binary file not shown.
Binary file not shown.
39 changes: 37 additions & 2 deletions gemet/thesaurus/tests/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.test import TestCase, Client

from .factories import VersionFactory
from gemet.thesaurus.models import Concept, Import
from gemet.thesaurus.models import Concept, Import, Property


class ConceptImportView(TestCase):
Expand All @@ -14,7 +14,7 @@ def setUp(self):
VersionFactory(identifier='')
self.client = Client()

def test_import(self):
def test_import_concepts_and_translations_together(self):
import_obj = Import.objects.create(
spreadsheet=File(open('gemet/thesaurus/tests/files/concepts.xlsx'))
)
Expand All @@ -33,3 +33,38 @@ def test_import(self):
).count(),
2
)

def test_import_concepts_and_translations_separately(self):
# Import concepts in English first
import_obj = Import.objects.create(
spreadsheet=File(open('gemet/thesaurus/tests/files/only_en.xlsx'))
)
url = '/import/{}/start/'.format(import_obj.pk)
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(
Concept.objects.filter(
status=0, namespace__heading='Concepts'
).count(),
79
)
self.assertEqual(
Concept.objects.filter(
status=0, namespace__heading='Groups'
).count(),
2
)
num_properties_before = Property.objects.count()
# Import a separate spreadsheet only with translations
import_obj = Import.objects.create(
spreadsheet=File(
open('gemet/thesaurus/tests/files/only_translations.xlsx')
)
)
url = '/import/{}/start/'.format(import_obj.pk)
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
# The number of concepts is still the same
self.assertEqual(Concept.objects.filter(status=0).count(), 81)
# New properties were created with translations
self.assertGreater(Property.objects.count(), num_properties_before)

0 comments on commit 2058e66

Please sign in to comment.