From 50d0c2b464710eb8eedf5f925818bd2cfe8f5ef5 Mon Sep 17 00:00:00 2001 From: Dimas Ciputra Date: Wed, 4 Sep 2024 15:37:25 +0100 Subject: [PATCH] SubSpecies should not be full taxon name in csv download (#4222) --- bims/serializers/bio_collection_serializer.py | 23 ++++++++++++++----- bims/views/download_csv_taxa_list.py | 14 ++++++++--- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/bims/serializers/bio_collection_serializer.py b/bims/serializers/bio_collection_serializer.py index 9a22fba28..995c99444 100644 --- a/bims/serializers/bio_collection_serializer.py +++ b/bims/serializers/bio_collection_serializer.py @@ -412,12 +412,6 @@ def get_genus(self, obj): 'genus_name' ) - def get_sub_species(self, obj: BiologicalCollectionRecord): - return self.taxon_name_by_rank( - obj, - 'sub_species_name' - ) - def get_species(self, obj): species_name = self.taxon_name_by_rank( obj, @@ -433,6 +427,23 @@ def get_species(self, obj): return species_name.strip() return '-' + def get_sub_species(self, obj: BiologicalCollectionRecord): + sub_species_name = self.taxon_name_by_rank( + obj, + 'sub_species_name' + ) + if sub_species_name: + genus = self.get_genus(obj) + if genus: + sub_species_name = sub_species_name.replace(genus, '', 1) + + species = self.get_species(obj) + if species: + sub_species_name = sub_species_name.replace(species, '', 1) + + return sub_species_name.strip() + return '-' + def get_kingdom(self, obj): return self.taxon_name_by_rank( obj, diff --git a/bims/views/download_csv_taxa_list.py b/bims/views/download_csv_taxa_list.py index d7fdb4293..176d5d912 100644 --- a/bims/views/download_csv_taxa_list.py +++ b/bims/views/download_csv_taxa_list.py @@ -86,6 +86,17 @@ def get_species(self, obj: Taxonomy): return species_name.strip() return species_name + def get_sub_species(self, obj: Taxonomy): + sub_species_name = obj.sub_species_name + if sub_species_name: + genus_name = obj.genus_name + if genus_name: + sub_species_name = sub_species_name.replace(genus_name, '', 1) + species_name = obj.species_name + if species_name: + sub_species_name = sub_species_name.replace(species_name, '', 1) + return sub_species_name + def get_taxon(self, obj): return obj.canonical_name @@ -140,9 +151,6 @@ def get_gbif_link(self, obj): ) return '-' - def get_sub_species(self, obj: Taxonomy): - return obj.sub_species_name - class Meta: model = Taxonomy fields = (