diff options
| -rw-r--r-- | archaeological_operations/tests.py | 21 | ||||
| -rw-r--r-- | ishtar_common/data_importer.py | 84 | ||||
| -rw-r--r-- | ishtar_common/fixtures/test_towns.json | 26 | 
3 files changed, 88 insertions, 43 deletions
| diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index 9111c9ffe..9807e1b87 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -44,7 +44,7 @@ class ImportOperationTest(TestCase):                  settings.ROOT_PATH +                  '../ishtar_common/fixtures/initial_data-fr.json',                  settings.ROOT_PATH + -                '../ishtar_common/fixtures/initial_towns-fr.json', +                '../ishtar_common/fixtures/test_towns.json',                  settings.ROOT_PATH +                  '../ishtar_common/fixtures/initial_importtypes-fr.json',                  settings.ROOT_PATH + @@ -80,14 +80,25 @@ class ImportOperationTest(TestCase):          # operation_type value          self.assertTrue(current_ope_nb == first_ope_nb) -        # doing manualy the connection assuming that the last 'operation_type' -        # target is the one we need +        # doing manualy connections          tg = TargetKey.objects.filter(target__target='operation_type'                                        ).order_by('-pk').all()[0]          tg.value = models.OperationType.objects.get(txt_idx='prog_excavation').pk          tg.is_set = True          tg.save() +        target = TargetKey.objects.get(key='gallo-romain') +        gallo = models.Period.objects.get(txt_idx='gallo-roman') +        target.value = gallo.pk +        target.is_set = True +        target.save() + +        target = TargetKey.objects.get(key='age-du-fer') +        iron = models.Period.objects.get(txt_idx='iron_age') +        target.value = iron.pk +        target.is_set = True +        target.save() +          impt.importation()          # a new operation has now been imported          current_ope_nb = models.Operation.objects.count() @@ -96,6 +107,10 @@ class ImportOperationTest(TestCase):          last_ope = models.Operation.objects.order_by('-pk').all()[0]          self.assertTrue(last_ope.code_patriarche == 4000)          self.assertTrue(last_ope.operation_type.txt_idx == 'prog_excavation') +        self.assertEqual(last_ope.periods.count(), 2) +        periods = last_ope.periods.all() +        self.assertTrue(iron in periods and gallo in periods) +          # a second importation will be not possible: no two same patriarche code          impt.importation() diff --git a/ishtar_common/data_importer.py b/ishtar_common/data_importer.py index 0ec579b4f..19382c4aa 100644 --- a/ishtar_common/data_importer.py +++ b/ishtar_common/data_importer.py @@ -837,51 +837,55 @@ class Importer(object):                  field_object, model, direct, m2m = \                                      cls._meta.get_field_by_name(attribute)                  if m2m: -                    val = data.pop(attribute) +                    many_values = data.pop(attribute)                      if hasattr(field_object, 'rel'):                          model = field_object.rel.to                      elif hasattr(field_object, 'to'):                          model = field_object.to -                    if val.__class__ == model: -                        m2ms.append((attribute, val)) -                    elif val.__class__ != model and type(val) == dict: -                        vals = [] - -                        # contruct many dict for each values -                        default_dict = {} -                        ## init with simple values that will be duplicated -                        for key in val.keys(): -                            if type(val[key]) not in (list, tuple): -                                default_dict[key] = val[key] -                        ## manage multiple values -                        for key in val.keys(): -                            if type(val[key]) in (list, tuple): -                                for idx, v in enumerate(val[key]): -                                    if len(vals) <= idx: -                                        vals.append(default_dict.copy()) -                                    vals[idx][key] = v - -                        # check that m2m are not empty -                        notempty = False -                        for dct in vals: -                            for k in dct: -                                if dct[k] not in ("", None): -                                    notempty = True -                                    break -                        if not notempty: -                            continue - -                        for v in vals: -                            v['defaults'] = v.get('defaults', {}) -                            if 'history_modifier' in \ -                                    model._meta.get_all_field_names(): -                                v['defaults']['history_modifier'] = \ +                    if type(many_values) not in (list, tuple): +                        many_values = [many_values] +                    for val in many_values: +                        if val.__class__ == model: +                            m2ms.append((attribute, val)) +                        elif val.__class__ != model and type(val) == dict: +                            vals = [] + +                            # contruct many dict for each values +                            default_dict = {} +                            ## init with simple values that will be duplicated +                            for key in val.keys(): +                                if type(val[key]) not in (list, tuple): +                                    default_dict[key] = val[key] +                            ## manage multiple values +                            for key in val.keys(): +                                if type(val[key]) in (list, tuple): +                                    for idx, v in enumerate(val[key]): +                                        if len(vals) <= idx: +                                            vals.append(default_dict.copy()) +                                        vals[idx][key] = v + +                            # check that m2m are not empty +                            notempty = False +                            for dct in vals: +                                for k in dct: +                                    if dct[k] not in ("", None): +                                        notempty = True +                                        break +                            if not notempty: +                                continue + +                            for v in vals: +                                v['defaults'] = v.get('defaults', {}) +                                if 'history_modifier' in \ +                                        model._meta.get_all_field_names(): +                                    v['defaults']['history_modifier'] = \                                                            self.history_modifier -                                v, created = model.objects.get_or_create(**v) -                                if self.import_instance \ -                                   and hasattr(v, 'imports'): -                                    v.imports.add(self.import_instance) -                                m2ms.append((attribute, v)) +                                    v, created = model.objects.get_or_create( +                                                                            **v) +                                    if self.import_instance \ +                                       and hasattr(v, 'imports'): +                                        v.imports.add(self.import_instance) +                                    m2ms.append((attribute, v))                  elif hasattr(field_object, 'rel') and field_object.rel and \                     type(data[attribute]) == dict:                      c_path.append(attribute) diff --git a/ishtar_common/fixtures/test_towns.json b/ishtar_common/fixtures/test_towns.json new file mode 100644 index 000000000..a88ca6476 --- /dev/null +++ b/ishtar_common/fixtures/test_towns.json @@ -0,0 +1,26 @@ +[ +    { +        "pk": 20816,  +        "model": "ishtar_common.town",  +        "fields": { +            "departement": null,  +            "name": "PARIS-1ER-ARRONDISSEMENT",  +            "center": "POINT (599976.9837326267734170 2429351.2226647692732513)",  +            "surface": 1810000,  +            "canton": null,  +            "numero_insee": "75101" +        } +    },  +    { +        "pk": 23920,  +        "model": "ishtar_common.town",  +        "fields": { +            "departement": null,  +            "name": "LILLE",  +            "center": "POINT (650348.5204579939600080 2626592.6267738011665642)",  +            "surface": 34990000,  +            "canton": null,  +            "numero_insee": "59350" +        } +    } +] | 
