diff options
| -rw-r--r-- | archaeological_context_records/tests.py | 9 | ||||
| -rw-r--r-- | archaeological_finds/models.py | 8 | ||||
| -rw-r--r-- | archaeological_finds/tests.py | 67 | ||||
| -rw-r--r-- | archaeological_finds/tests/MCC-finds-example.csv | 5 | ||||
| -rw-r--r-- | ishtar_common/data_importer.py | 5 | ||||
| -rw-r--r-- | ishtar_common/models.py | 8 | 
6 files changed, 98 insertions, 4 deletions
| diff --git a/archaeological_context_records/tests.py b/archaeological_context_records/tests.py index 2900b8786..00854ad7c 100644 --- a/archaeological_context_records/tests.py +++ b/archaeological_context_records/tests.py @@ -27,14 +27,16 @@ from django.core.exceptions import ValidationError  from django.core.files.uploadedfile import SimpleUploadedFile  from django.test import TestCase -from ishtar_common.models import ImporterType, TargetKey -from archaeological_operations.tests import OperationInitTest, ImportOperationTest +from ishtar_common.models import ImporterType +from archaeological_operations.tests import OperationInitTest, \ +                                            ImportOperationTest  from archaeological_operations.models import Parcel  from archaeological_context_records import models  from ishtar_common import forms_common  class ImportContextRecordTest(ImportOperationTest):      test_operations = False +    test_context_records = True      fixtures = ImportOperationTest.fixtures + [          settings.ROOT_PATH + @@ -42,7 +44,8 @@ class ImportContextRecordTest(ImportOperationTest):      ]      def testMCCImportContextRecords(self, test=True): -        self.testMCCImportOperation(test=False) +        if test and not self.test_context_records: +            return          self.testMCCImportParcels(test=False)          old_nb = models.ContextRecord.objects.count() diff --git a/archaeological_finds/models.py b/archaeological_finds/models.py index 8aae745e6..dd4b20b39 100644 --- a/archaeological_finds/models.py +++ b/archaeological_finds/models.py @@ -166,6 +166,14 @@ class BaseFind(BaseHistorizedItem, OwnPerms):      def name(self):          return self.label +    @classmethod +    def get_extra_fields(cls): +        fields = {} +        for field in Find._meta.many_to_many: +            if field.name == 'base_finds': +                fields['find'] = field.related.model +        return fields +  WEIGHT_UNIT = (('g', _(u"g")),                 ('kg', _(u"kg")),) diff --git a/archaeological_finds/tests.py b/archaeological_finds/tests.py new file mode 100644 index 000000000..f61807637 --- /dev/null +++ b/archaeological_finds/tests.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Copyright (C) 2015 Étienne Loks  <etienne.loks_AT_peacefrogsDOTnet> + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the +# GNU Affero General Public License for more details. + +# You should have received a copy of the GNU Affero General Public License +# along with this program.  If not, see <http://www.gnu.org/licenses/>. + +# See the file COPYING for details. + +from django.conf import settings +from django.core.files.uploadedfile import SimpleUploadedFile +from ishtar_common.models import ImporterType + +from archaeological_finds import models + +from archaeological_context_records.tests import ImportContextRecordTest + +from ishtar_common import forms_common + +class ImportFindTest(ImportContextRecordTest): +    test_context_records = False + +    fixtures = ImportContextRecordTest.fixtures + [ +        settings.ROOT_PATH + +        '../archaeological_finds/fixtures/initial_data-fr.json', +    ] + +    def testMCCImportFinds(self, test=True): +        self.testMCCImportContextRecords(test=False) + +        old_nb = models.BaseFind.objects.count() +        MCC = ImporterType.objects.get(name=u"MCC - Mobilier") +        mcc_file = open(settings.ROOT_PATH + \ +              '../archaeological_finds/tests/MCC-finds-example.csv', 'rb') +        file_dict = {'imported_file': SimpleUploadedFile(mcc_file.name, +                                                         mcc_file.read())} +        post_dict = {'importer_type':MCC.pk, 'skip_lines':1, +                     "encoding":'utf-8'} +        form = forms_common.NewImportForm(data=post_dict, files=file_dict, +                                          instance=None) +        form.is_valid() +        if test: +            self.assertTrue(form.is_valid()) +        impt = form.save(self.ishtar_user) +        impt.initialize() + +        # doing manual connections +        ceram = models.MaterialType.objects.get(txt_idx='ceramic').pk +        self.setTargetKey('find__material_type', 'ceramique', ceram) +        impt.importation() +        if not test: +            return +        # new finds has now been imported +        current_nb = models.BaseFind.objects.count() +        self.assertTrue(current_nb == (old_nb + 4)) +        self.assertEqual( +            models.Find.objects.filter(material_type__pk=ceram).count(), 4) diff --git a/archaeological_finds/tests/MCC-finds-example.csv b/archaeological_finds/tests/MCC-finds-example.csv new file mode 100644 index 000000000..67971132f --- /dev/null +++ b/archaeological_finds/tests/MCC-finds-example.csv @@ -0,0 +1,5 @@ +code OA,identifiant materiel,objet lot,partage,composite,matiere,type,determination,numero contenant,nombre fragment,poids,unite poids,identifiant UE,date decouverte,identifiant parcelle,etat sanitaire,type preservation a envisager,commentaire,chronologie,localisation topographique,interet particulier,description,remontage
 +4200,1,lot,non,,terre cuite,céramique,sfq,1,76,4040,g,HC,,XXXXXX,sais pas,,Blah,med,,,,
 +4200,1,lot,non,,terre cuite,céramique,qsdfsqfd,1,56,2280,g,H.-C.,,XXXXXX,,,,med,,,,
 +4200,1,lot,non,,terre cuite,céramique,sqfdsq,2,44,5210,g,HC,,YY55,stable,,,GR,,,,
 +4200,2,lot,non,,terre cuite,céramique,sqfdsq,45,43,1500,g,US17,,YY55,,,,,,,,
 diff --git a/ishtar_common/data_importer.py b/ishtar_common/data_importer.py index b5fc9b0c6..27714458b 100644 --- a/ishtar_common/data_importer.py +++ b/ishtar_common/data_importer.py @@ -437,7 +437,7 @@ class Importer(object):                    u"and encoding are similar to the ones used by the reference "                    u"file."),          'too_many_cols':_(u"Too many cols (%(user_col)d) when " -                         u"maximum is %(ref_col)d"), +                          u"maximum is %(ref_col)d"),          'no_data':_(u"No data provided"),          'value_required':_(u"Value is required"),          'not_enough_cols':_(u"At least %d columns must be filled"), @@ -794,6 +794,8 @@ class Importer(object):                          model = field_object.rel.to                      elif hasattr(field_object, 'to'):                          model = field_object.to +                    elif hasattr(field_object, 'model'): +                        model = field_object.model                      if type(many_values) not in (list, tuple):                          many_values = [many_values]                      for val in many_values: @@ -808,6 +810,7 @@ class Importer(object):                              for key in val.keys():                                  if type(val[key]) not in (list, tuple):                                      default_dict[key] = val[key] +                            vals.append(default_dict.copy())                              ## manage multiple values                              for key in val.keys():                                  if type(val[key]) in (list, tuple): diff --git a/ishtar_common/models.py b/ishtar_common/models.py index a57e814f0..9f7261010 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -40,6 +40,7 @@ from django.core.validators import validate_slug  from django.core.urlresolvers import reverse, NoReverseMatch  from django.db.utils import DatabaseError  from django.db.models import Q, Max, Count +from django.db.models.base import ModelBase  from django.db.models.signals import post_save, pre_delete  from django.utils.translation import ugettext_lazy as _, ugettext  from django.utils.safestring import SafeUnicode, mark_safe @@ -993,6 +994,8 @@ def get_model_fields(model):      options = model._meta      for field in sorted(options.fields + options.many_to_many):          fields[field.name] = field +    if hasattr(model, 'get_extra_fields'): +        fields.update(model.get_extra_fields())      return fields  def import_class(full_path_classname): @@ -1075,6 +1078,8 @@ def get_associated_model(parent_model, keys):              field = get_model_fields(OBJECT_CLS)[item]              if hasattr(field, 'rel') and hasattr(field.rel, 'to'):                  model = field.rel.to +            if type(field) == ModelBase: +                model = field          else:              return get_associated_model(model, keys[1:])      return model @@ -1259,6 +1264,9 @@ TARGET_MODELS = [      ('archaeological_operations.models.OperationType', _(u"Operation type")),      ('archaeological_operations.models.Period', _(u"Period")),      ('archaeological_context_records.models.Unit', _(u"Unit")), +    ('archaeological_finds.models.MaterialType', _(u"Material")), +    ('archaeological_finds.models.ConservatoryState', _(u"Conservatory state")), +    ('archaeological_finds.models.PreservationType', _(u"Preservation type")),      ]  TARGET_MODELS_KEYS = [tm[0] for tm in TARGET_MODELS] | 
