diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2017-10-19 16:29:55 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2017-10-19 16:29:55 +0200 |
commit | 0fb4dd2f90c550128f1336daa934df8a9c8e502e (patch) | |
tree | 3688ff4d93476aca0d1568a2670f564b5ddaa5f0 | |
parent | f70817381f91063845ff9988393110d454f2c411 (diff) | |
download | Ishtar-0fb4dd2f90c550128f1336daa934df8a9c8e502e.tar.bz2 Ishtar-0fb4dd2f90c550128f1336daa934df8a9c8e502e.zip |
Json fields: manage imports (refs #3077)
-rw-r--r-- | archaeological_operations/tests.py | 21 | ||||
-rw-r--r-- | archaeological_operations/tests/operations-with-json-fields.csv | 3 | ||||
-rw-r--r-- | ishtar_common/data_importer.py | 7 |
3 files changed, 28 insertions, 3 deletions
diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index ab0876aef..67b89ce11 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -38,7 +38,8 @@ from archaeological_operations import views from ishtar_common.models import OrganizationType, Organization, ItemKey, \ ImporterType, IshtarUser, TargetKey, ImporterModel, IshtarSiteProfile, \ Town, ImporterColumn, Person, Author, SourceType, AuthorType, \ - DocumentTemplate, PersonType, TargetKeyGroup, JsonDataField, JsonDataSection + DocumentTemplate, PersonType, TargetKeyGroup, JsonDataField, \ + JsonDataSection, ImportTarget, FormaterType from archaeological_files.models import File, FileType from archaeological_context_records.models import Unit @@ -454,6 +455,24 @@ class ImportOperationTest(ImportTest, TestCase): impt.delete() self.assertEqual(parcel_count - 3, models.Parcel.objects.count()) + def test_json_fields(self): + importer, form = self.init_ope_import("operations-with-json-fields.csv") + col = ImporterColumn.objects.create(importer_type=importer, + col_number=11) + formater_type = FormaterType.objects.get( + formater_type='IntegerFormater') + ImportTarget.objects.create( + column=col, target='data__autre_refs__arbitraire', + formater_type=formater_type) + impt = form.save(self.ishtar_user) + impt.initialize() + self.init_ope_targetkey(imp=impt) + impt.importation() + ope1 = models.Operation.objects.get(code_patriarche='4200') + self.assertEqual(ope1.data, {u'autre_refs': {u'arbitraire': 789}}) + ope2 = models.Operation.objects.get(code_patriarche='4201') + self.assertEqual(ope2.data, {u'autre_refs': {u'arbitraire': 456}}) + class ParcelTest(ImportTest, TestCase): fixtures = OPERATION_TOWNS_FIXTURES diff --git a/archaeological_operations/tests/operations-with-json-fields.csv b/archaeological_operations/tests/operations-with-json-fields.csv new file mode 100644 index 000000000..015497b4c --- /dev/null +++ b/archaeological_operations/tests/operations-with-json-fields.csv @@ -0,0 +1,3 @@ +code OA,region,type operation,intitule operation,operateur,responsable operation,date debut terrain,date fin terrain,chronologie generale,identifiant document georeferencement,notice scientifique,numéro arbitraire +4201,Bourgogne,Fouille programmée,Oppìdum de Paris 2,L'opérateur,,2000/01/31,2002/12/31,Age du Fer,,456 +4200,Bourgogne,Fouille programmée,Oppìdum de Paris,L'opérateur,Jean Sui-Resp'on Sablé,2000/01/22,2002/12/31,Age du Fer & Gallo-Romain & Néolithik & Moderne,,789 diff --git a/ishtar_common/data_importer.py b/ishtar_common/data_importer.py index 9caebb2dd..e8ec43ab2 100644 --- a/ishtar_common/data_importer.py +++ b/ishtar_common/data_importer.py @@ -1486,6 +1486,9 @@ class Importer(object): # importer trigger self._set_importer_trigger(cls, attribute, data) return + if attribute == 'data': # json field + # no need to do anything + return try: field_object = cls._meta.get_field(attribute) except FieldDoesNotExist: @@ -1570,8 +1573,8 @@ class Importer(object): create_dict = copy.deepcopy(data) for k in create_dict.keys(): - # filter unnecessary default values - if type(create_dict[k]) == dict: + # filter unnecessary default values but not the json field + if type(create_dict[k]) == dict and k != 'data': create_dict.pop(k) # File doesn't like deepcopy elif type(create_dict[k]) == File: |