From 722baacc622ee2de801b7333de0e90abbfba7a1c Mon Sep 17 00:00:00 2001 From: Dimas Ciputra Date: Thu, 25 Jul 2024 07:26:01 +0100 Subject: [PATCH] Add ability to add new taxon with higher taxonomic ranks (#4103) --- bims/api_views/taxon.py | 26 +++++-- bims/enums/taxonomic_rank.py | 26 +++---- .../js/taxa_management/add_new_taxon.js | 36 ++++++--- bims/static/js/taxa_management/taxa_table.js | 3 +- bims/templates/taxa_management.html | 14 ++-- bims/tests/test_add_new_taxon.py | 73 +++++++++++++++++++ 6 files changed, 140 insertions(+), 38 deletions(-) create mode 100644 bims/tests/test_add_new_taxon.py diff --git a/bims/api_views/taxon.py b/bims/api_views/taxon.py index 3b42a72fe..a68c446b7 100644 --- a/bims/api_views/taxon.py +++ b/bims/api_views/taxon.py @@ -260,9 +260,12 @@ def post(self, request, *args): author_name = self.request.POST.get('authorName', '') rank = self.request.POST.get('rank', None) family_id = self.request.POST.get('familyId', None) - family = None + parent = None if family_id: - family = Taxonomy.objects.get(id=int(family_id)) + parent = Taxonomy.objects.get(id=int(family_id)) + parent_id = self.request.POST.get('parentId', None) + if parent_id: + parent = Taxonomy.objects.get(id=int(parent_id)) if gbif_key: taxonomy = update_taxonomy_from_gbif( key=gbif_key, @@ -277,12 +280,23 @@ def post(self, request, *args): ) if taxon_group_id: taxon_group = TaxonGroup.objects.get(id=taxon_group_id) - taxon_group.taxonomies.add(taxonomy) + taxon_group.taxonomies.add( + taxonomy, + through_defaults={ + 'is_validated': False + } + ) else: if taxon_group: try: taxon_group = TaxonGroup.objects.get(name=taxon_group) - taxon_group.taxonomies.add(taxonomy) + taxon_group.taxonomies.add( + taxonomy, + through_defaults={ + 'is_validated': False + } + ) + taxon_group_id = taxon_group.id except TaxonGroup.DoesNotExist: pass if taxonomy: @@ -302,8 +316,8 @@ def post(self, request, *args): taxonomy.ready_to_be_validate() taxonomy.send_new_taxon_email(taxon_group_id) - if family: - taxonomy.parent = family + if parent: + taxonomy.parent = parent taxonomy.save() with transaction.atomic(): diff --git a/bims/enums/taxonomic_rank.py b/bims/enums/taxonomic_rank.py index e5aa2f561..fdfbc2f13 100644 --- a/bims/enums/taxonomic_rank.py +++ b/bims/enums/taxonomic_rank.py @@ -3,26 +3,26 @@ class TaxonomicRank(Enum): - SUBSPECIES = 'Sub Species' - SPECIES = 'Species' - GENUS = 'Genus' - SUBGENUS = 'Sub Genus' - FAMILY = 'Family' - SUPERFAMILY = 'Super Family' - ORDER = 'Order' - CLASS = 'Class' - SUBCLASS = 'Sub Class' + DOMAIN = 'Domain' + KINGDOM = 'Kingdom' PHYLUM = 'Phylum' SUBPHYLUM = 'SubPhylum' - KINGDOM = 'Kingdom' - DOMAIN = 'Domain' + CLASS = 'Class' + SUBCLASS = 'Sub Class' + ORDER = 'Order' SUBORDER = 'Sub Order' INFRAORDER = 'Infra Order' + SUPERFAMILY = 'Super Family' + FAMILY = 'Family' SUBFAMILY = 'Sub Family' - VARIETY = 'Variety' - FORMA = 'Forma' TRIBE = 'Tribe' SUBTRIBE = 'Sub Tribe' + GENUS = 'Genus' + SUBGENUS = 'Sub Genus' + SPECIES = 'Species' + SUBSPECIES = 'Sub Species' + VARIETY = 'Variety' + FORMA = 'Forma' @staticmethod def hierarchy(): diff --git a/bims/static/js/taxa_management/add_new_taxon.js b/bims/static/js/taxa_management/add_new_taxon.js index c06f9585a..087b6d4b7 100644 --- a/bims/static/js/taxa_management/add_new_taxon.js +++ b/bims/static/js/taxa_management/add_new_taxon.js @@ -1,25 +1,28 @@ export const addNewTaxon = (() => { - const $newTaxonFamilyIdInput = $('.new-taxon-family-id'); + const $newTaxonParentIdInput = $('.new-taxon-parent-id'); const $newTaxonFamilyInput = $('.new-taxon-family-name'); + const $newTaxonParent = $('#new-taxon-parent'); const $taxonForm = $('.new-taxon-form'); const $addNewTaxonBtn = $taxonForm.find('.add-new-taxon-btn'); const $newTaxonNameInput = $('#new-taxon-name'); let selectedTaxonGroup = ''; + function showNewTaxonForm(taxonName) { let capitalizedTaxonName = taxonName.substr(0, 1).toUpperCase() + taxonName.substr(1).toLowerCase(); - speciesAutoComplete($newTaxonFamilyInput, '&rank=family&taxonGroupId=' + currentSelectedTaxonGroup).then(value => { - $newTaxonFamilyIdInput.val(value); - }) $newTaxonNameInput.val(capitalizedTaxonName); + $newTaxonParentIdInput.val($newTaxonParent.val()) $taxonForm.show(); const authorAutoComplete = $('#author-auto-complete'); authorAutoComplete.empty(); authorAutoComplete.val(null).trigger('change'); + + $newTaxonParent.empty(); + $newTaxonParent.val(null).trigger('change'); } - function addNewTaxonToObservedList(name, gbifKey, rank, taxaId = null, familyId = "", authorName = "") { + function addNewTaxonToObservedList(name, gbifKey, rank, taxaId = null, parentId = "", authorName = "") { let postData = { 'gbifKey': gbifKey, 'taxonName': name, @@ -27,8 +30,8 @@ export const addNewTaxon = (() => { 'taxonGroupId': currentSelectedTaxonGroup, 'authorName': authorName }; - if (familyId) { - postData['familyId'] = familyId + if (parentId) { + postData['parentId'] = parentId } let table = $('.find-taxon-table'); table.hide(); @@ -149,18 +152,27 @@ export const addNewTaxon = (() => { function handleAddNewTaxon(event) { let $rank = $taxonForm.find('.new-taxon-rank'); let $author = $taxonForm.find('#author-auto-complete'); - const familyId = $newTaxonFamilyIdInput.val(); - if (!familyId) { - alert("Missing family"); + const parentId = $newTaxonParent.val(); + if (!parentId) { + alert("Missing parent"); return } - addNewTaxonToObservedList($newTaxonNameInput.val(), '', $rank.val(), null, familyId, $author.val()); + addNewTaxonToObservedList($newTaxonNameInput.val(), '', $rank.val(), null, parentId, $author.val()); $taxonForm.hide(); $newTaxonFamilyInput.val("") - $newTaxonFamilyIdInput.val("") + $newTaxonParentIdInput.val("") } function init(_selectedTaxonGroup) { + + $('#addNewTaxonModal').on('shown.bs.modal', function (e) { + const authorAutoComplete = $('#author-auto-complete'); + authorAutoComplete.empty(); + authorAutoComplete.val(null).trigger('change'); + + $newTaxonParent.empty(); + $newTaxonParent.val(null).trigger('change'); + }); selectedTaxonGroup = _selectedTaxonGroup $('#find-taxon-button').on('click', handleFindTaxonButton) diff --git a/bims/static/js/taxa_management/taxa_table.js b/bims/static/js/taxa_management/taxa_table.js index d4d119347..69d5d8d42 100644 --- a/bims/static/js/taxa_management/taxa_table.js +++ b/bims/static/js/taxa_management/taxa_table.js @@ -107,7 +107,8 @@ export const taxaTable = (() => { data: function (params) { return { term: params.term, - rank: $(this).data('rank') + rank: $(this).data('rank'), + taxonGroupId: currentSelectedTaxonGroup } }, processResults: function (data) { diff --git a/bims/templates/taxa_management.html b/bims/templates/taxa_management.html index 7c65720d5..e1104f293 100644 --- a/bims/templates/taxa_management.html +++ b/bims/templates/taxa_management.html @@ -451,7 +451,7 @@ -