diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-03-11 20:05:04 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-04-24 19:41:37 +0200 |
commit | f9620956381f843fc5d341fd19cdf0fc0f68d1a7 (patch) | |
tree | d4dd0e61a429a660c7ec8776bf9287dab8d5610f | |
parent | e4454fdbf47a44de9de725ed733d12b604855da6 (diff) | |
download | Ishtar-f9620956381f843fc5d341fd19cdf0fc0f68d1a7.tar.bz2 Ishtar-f9620956381f843fc5d341fd19cdf0fc0f68d1a7.zip |
Import: manage pre-import function - test document import
-rw-r--r-- | archaeological_operations/tests.py | 57 | ||||
-rw-r--r-- | archaeological_operations/tests/document-example.csv | 3 | ||||
-rw-r--r-- | ishtar_common/data_importer.py | 2 | ||||
-rw-r--r-- | ishtar_common/models.py | 24 | ||||
-rw-r--r-- | ishtar_common/tests.py | 2 |
5 files changed, 79 insertions, 9 deletions
diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index 14577e119..06898014c 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -498,6 +498,63 @@ class ImportOperationTest(ImportTest, TestCase): u"autre": 333}) +class ImportDocumentTest(ImportTest, TestCase): + fixtures = OPERATION_TOWNS_FIXTURES + + def init_doc_import(self, filename='document-example.csv'): + model, __ = ImporterModel.objects.get_or_create( + klass="ishtar_common.models.Document", + defaults={ + "name": "Documentation" + } + ) + doc_import, __ = ImporterType.objects.get_or_create( + name="Doc import", slug="doc-import", associated_models=model) + + col = ImporterColumn.objects.create(col_number=1, + importer_type_id=doc_import.pk) + formater = FormaterType.objects.filter( + formater_type='IntegerFormater').all()[0] + ImportTarget.objects.create(target='import_get_next_index', + formater_type_id=formater.pk, + column_id=col.pk) + col = ImporterColumn.objects.create(col_number=2, + importer_type_id=doc_import.pk) + formater, __ = FormaterType.objects.get_or_create( + formater_type='UnicodeFormater', options=None) + ImportTarget.objects.create(target='title', + formater_type_id=formater.pk, + column_id=col.pk) + + doc_import_file = open( + settings.ROOT_PATH + + '../archaeological_operations/tests/' + filename, + 'rb') + + file_dict = {'imported_file': SimpleUploadedFile( + doc_import_file.name, doc_import_file.read())} + + group, c = TargetKeyGroup.objects.get_or_create(name="My group") + post_dict = {'importer_type': doc_import.pk, 'skip_lines': 1, + "encoding": 'utf-8', "name": 'init_ope_import', + "associated_group": group.pk} + form = forms_common.NewImportForm(data=post_dict, files=file_dict, + user=self.user) + form.is_valid() + return doc_import, form + + def test_import_document(self): + doc_nb = Document.objects.count() + current_index = Document.get_next_index() - 1 + + importer, form = self.init_doc_import() + self.assertTrue(form.is_valid()) + impt = form.save(self.ishtar_user) + impt.importation() + self.assertEqual(doc_nb + 2, Document.objects.count()) + self.assertEqual(current_index + 2, Document.get_next_index() - 1) + + class ImportStepByStepTest(ImportTest, TestCase): fixtures = OPERATION_TOWNS_FIXTURES diff --git a/archaeological_operations/tests/document-example.csv b/archaeological_operations/tests/document-example.csv new file mode 100644 index 000000000..3dc13987f --- /dev/null +++ b/archaeological_operations/tests/document-example.csv @@ -0,0 +1,3 @@ +Index,title +1,Plouf 1 +1,Plouf 2 diff --git a/ishtar_common/data_importer.py b/ishtar_common/data_importer.py index d20096a91..d4c4ba786 100644 --- a/ishtar_common/data_importer.py +++ b/ishtar_common/data_importer.py @@ -1542,7 +1542,7 @@ class Importer(object): """ func = getattr(cls, attribute) if func.importer_trigger == 'pre': - pass # TODO + func(data, data[attribute]) elif func.importer_trigger == 'post': self._item_post_processing.append([attribute, data, data[attribute]]) diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 641a9b0a4..57283e8f4 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -70,6 +70,8 @@ from unidecode import unidecode from ishtar_common.alternative_configs import ALTERNATE_CONFIGS, \ ALTERNATE_CONFIGS_CHOICES +from ishtar_common.data_importer import pre_importer_action + from ishtar_common.model_merging import merge_model_objects from ishtar_common.models_imports import ImporterModel, ImporterType, \ ImporterDefault, ImporterDefaultValues, ImporterColumn, \ @@ -4690,18 +4692,26 @@ class Document(BaseHistorizedItem, OwnPerms, ImageModel): def _generate_cache_related_label(self): return self.related_label() - def set_index(self): - if self.index: - return - q = Document.objects.values('index').filter( + @classmethod + def get_next_index(cls): + q = cls.objects.values('index').filter( index__isnull=False).order_by("-index") if not q.count(): - self.index = 1 - return + return 1 cid = q.all()[0]['index'] if not cid: cid = 0 - self.index = cid + 1 + return cid + 1 + + def set_index(self): + if self.index: + return + self.index = self.get_next_index() + + @classmethod + @pre_importer_action + def import_get_next_index(cls, context, value): + context["index"] = cls.get_next_index() def save(self, *args, **kwargs): no_path_change = 'no_path_change' in kwargs \ diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py index 3e9181f30..7166f9cb7 100644 --- a/ishtar_common/tests.py +++ b/ishtar_common/tests.py @@ -1360,7 +1360,7 @@ class ShortMenuTest(TestCase): class ImportTest(TestCase): - def testDeleteRelated(self): + def test_delete_related(self): town = models.Town.objects.create(name='my-test') self.assertEqual(models.Town.objects.filter(name='my-test').count(), 1) |