def commit_row(self): is_valid = self.validate_row() if not is_valid or not self.merged: return # not ready to commit if self.status == SpeciesImportRow.SUCCESS: return # nothing changed so no need to commit # Get our data data = self.cleaned species_edited = False # Initially grab species from row if it exists and edit it species = self.species # If not specified create a new one if species is None: species = Species(instance=self.import_event.instance) # Convert units self.convert_units(data, { fields.species.MAX_DIAMETER: self.import_event.max_diameter_conversion_factor, fields.species.MAX_HEIGHT: self.import_event.max_tree_height_conversion_factor }) for modelkey, datakey in SpeciesImportRow.SPECIES_MAP.iteritems(): importdata = data.get(datakey, None) if importdata is not None: species_edited = True setattr(species, modelkey, importdata) # Set OTM code if missing and available if not species.otm_code: species_dict = species_for_scientific_name( species.genus, species.species, species.cultivar, species.other_part_of_name) if species_dict: species_edited = True species.otm_code = species_dict['otm_code'] if species_edited: species.save_with_system_user_bypass_auth() # Make i-Tree code override(s) if necessary if fields.species.ITREE_PAIRS in data: for region_code, itree_code in data[fields.species.ITREE_PAIRS]: if itree_code != species.get_itree_code(region_code): override = ITreeCodeOverride.objects.get_or_create( instance_species=species, region=ITreeRegion.objects.get(code=region_code), )[0] override.itree_code = itree_code override.save_with_user(User.system_user()) self.species = species self.status = SpeciesImportRow.SUCCESS self.save()
def commit_row(self): is_valid = self.validate_row() if not is_valid or not self.merged: return # not ready to commit if self.status == SpeciesImportRow.SUCCESS: return # nothing changed so no need to commit # Get our data data = self.cleaned species_edited = False # Initially grab species from row if it exists and edit it species = self.species # If not specified create a new one if species is None: species = Species(instance=self.import_event.instance) # Convert units self.convert_units( data, { fields.species.MAX_DIAMETER: self.import_event.max_diameter_conversion_factor, fields.species.MAX_HEIGHT: self.import_event.max_tree_height_conversion_factor }) for modelkey, datakey in SpeciesImportRow.SPECIES_MAP.iteritems(): importdata = data.get(datakey, None) if importdata is not None: species_edited = True setattr(species, modelkey, importdata) # Set OTM code if missing and available if not species.otm_code: species_dict = species_for_scientific_name( species.genus, species.species, species.cultivar, species.other_part_of_name) if species_dict: species_edited = True species.otm_code = species_dict['otm_code'] if species_edited: species.save_with_system_user_bypass_auth() # Make i-Tree code override(s) if necessary if fields.species.ITREE_PAIRS in data: for region_code, itree_code in data[fields.species.ITREE_PAIRS]: if itree_code != species.get_itree_code(region_code): override = ITreeCodeOverride.objects.get_or_create( instance_species=species, region=ITreeRegion.objects.get(code=region_code), )[0] override.itree_code = itree_code override.save_with_user(User.system_user()) self.species = species self.status = SpeciesImportRow.SUCCESS self.save()
def test_species_for_scientific_name(self): species_dict = species_for_scientific_name('Abies', 'alba', '', '') self.assertEqual(species_dict['otm_code'], 'ABAL')