Skip to content

Commit

Permalink
Feat - search for taxa if not found in the current group (#4318)
Browse files Browse the repository at this point in the history
* Feat - search for taxa if not found in the current group

* Update bims/static/js/taxa_management/taxa_management.js
  • Loading branch information
dimasciput authored Oct 18, 2024
1 parent bc08a96 commit 50373a6
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 25 deletions.
64 changes: 43 additions & 21 deletions bims/api_views/taxon.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,8 @@ def get_taxa_by_parameters(request):
family_name = request.GET.get('family', '')
genus_name = request.GET.get('genus', '')
species_name = request.GET.get('species', '')
summary_only = request.GET.get('summary', 'False') == 'True'
taxon_group_ids = None

authors = []
if author_names:
Expand Down Expand Up @@ -474,19 +476,26 @@ def get_taxa_by_parameters(request):
id = request.GET.get('id', '')
if id:
return Taxonomy.objects.filter(id=id)
if not taxon_group_id:
raise Http404('Missing taxon group id')
try:
taxon_group = TaxonGroup.objects.get(id=taxon_group_id)
except TaxonGroup.DoesNotExist:
raise Http404('Taxon group does not exist')

taxon_group_ids = TaxaList.get_descendant_group_ids(
taxon_group)
taxon_list = Taxonomy.objects.filter(
taxongroup__id__in=taxon_group_ids,
taxongrouptaxonomy__is_rejected=False,
).distinct().order_by('canonical_name')
if taxon_group_id:
try:
taxon_group = TaxonGroup.objects.get(id=taxon_group_id)
except TaxonGroup.DoesNotExist:
raise Http404('Taxon group does not exist')
else:
taxon_group = None

if taxon_group:
taxon_group_ids = TaxaList.get_descendant_group_ids(
taxon_group)
taxon_list = Taxonomy.objects.filter(
taxongroup__id__in=taxon_group_ids,
taxongrouptaxonomy__is_rejected=False,
).distinct().order_by('canonical_name')
else:
taxon_list = Taxonomy.objects.filter(
taxongrouptaxonomy__is_rejected=False,
).distinct().order_by('canonical_name')

if len(authors) > 0:
taxon_list = taxon_list.filter(
Expand Down Expand Up @@ -568,15 +577,20 @@ def get_taxa_by_parameters(request):
try:
validated = ast.literal_eval(validated.replace('/', ''))
if not validated:
taxon_list = taxon_list.filter(
taxongrouptaxonomy__is_validated=False,
taxongrouptaxonomy__taxongroup__in=taxon_group_ids
)
validated_filters = {
'taxongrouptaxonomy__is_validated': False,
}
else:
taxon_list = taxon_list.filter(
taxongrouptaxonomy__is_validated=True,
taxongrouptaxonomy__taxongroup__in=taxon_group_ids
)
validated_filters = {
'taxongrouptaxonomy__is_validated': True,
}
if taxon_group_ids:
validated_filters[
'taxongrouptaxonomy__taxongroup__in'
] = taxon_group_ids
taxon_list = taxon_list.filter(
**validated_filters
)
except ValueError:
pass
if is_gbif:
Expand Down Expand Up @@ -605,7 +619,7 @@ def get_taxa_by_parameters(request):
)
except ValueError:
pass
if order:
if order and not summary_only:
if 'total_records' in order:
taxon_list = taxon_list.annotate(
total_records=Count('biologicalcollectionrecord')
Expand Down Expand Up @@ -667,6 +681,14 @@ def get_paginated_response(self, data):

def get(self, request, *args):
taxon_list = self.get_taxa_by_parameters(request)
summary_only = request.GET.get('summary', 'False') == 'True'

if summary_only:
return Response(list(TaxonGroupTaxonomy.objects.filter(
taxonomy__in=taxon_list
).values('taxongroup', 'taxongroup__name').annotate(
total=Count('taxongroup'))))

self.pagination_class.page_size = request.GET.get('page_size', 20)
page = self.paginate_queryset(taxon_list)
validated = ast.literal_eval(request.GET.get('validated', 'True'))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 4.2.16 on 2024-10-18 16:27

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('bims', '0446_sitesetting_park_layer'),
]

operations = [
migrations.AlterField(
model_name='sitesetting',
name='park_wfs_attribute_key',
field=models.CharField(blank=True, default='', help_text='Park attribute key', max_length=200),
),
migrations.AlterField(
model_name='taxongrouptaxonomy',
name='is_rejected',
field=models.BooleanField(db_index=True, default=False),
),
migrations.AlterField(
model_name='taxongrouptaxonomy',
name='is_validated',
field=models.BooleanField(db_index=True, default=False),
),
migrations.AlterField(
model_name='taxonomy',
name='canonical_name',
field=models.CharField(blank=True, db_index=True, max_length=200, null=True, verbose_name='Canonical Name'),
),
migrations.AlterField(
model_name='taxonomyupdateproposal',
name='canonical_name',
field=models.CharField(blank=True, db_index=True, max_length=200, null=True, verbose_name='Canonical Name'),
),
]
6 changes: 4 additions & 2 deletions bims/models/taxon_group_taxonomy.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ class TaxonGroupTaxonomy(models.Model):
)

is_validated = models.BooleanField(
default=False
default=False,
db_index=True
)

is_rejected = models.BooleanField(
default=False
default=False,
db_index=True
)

endemism = models.ForeignKey(
Expand Down
3 changes: 2 additions & 1 deletion bims/models/taxonomy.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ class AbstractTaxonomy(AbstractValidation):
verbose_name='Canonical Name',
max_length=200,
null=True,
blank=True
blank=True,
db_index=True
)

legacy_canonical_name = models.CharField(
Expand Down
44 changes: 44 additions & 0 deletions bims/static/js/taxa_management/taxa_management.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,45 @@ export const taxaManagement = (() => {
return luminance < 0.5;
}

const findTaxaWithoutTaxonGroup = (url) => {
let urlObj = new URL(url, window.location.origin);
let params = new URLSearchParams(urlObj.search);

params.delete('taxonGroup');
let taxon = params.get('taxon');

urlObj.search = params.toString();

let updatedUrl = urlObj.toString();
if (taxon) {
// Only search taxa if there is search string
$.ajax({
url: updatedUrl + '&summary=True',
dataType: 'json',
success: function (data) {
if (data.length > 0) {
let warningDivs = $('<div>')
for (let i=0; i < data.length; i++) {
let warningDiv = $('<div>');
warningDiv.append(
`<div class="alert alert-warning" role="alert" style="cursor: pointer">
${data[i].total} taxon found in ${data[i].taxongroup__name}. Click here to view the taxon group.
</div>`
)
warningDiv.on('click', function () {
$('#sortable').find(`[data-id="${data[i].taxongroup}"]`).click();
})
warningDivs.append(warningDiv);
}
$('.footer-warning').html(warningDivs)
}
}
});
}
}

const getTaxaList = (url) => {
$('.footer-warning').html('');
taxaUrlList = url;
let urlParams = new URLSearchParams(window.location.search);
let initialPage = urlParams.get('page') ? parseInt(urlParams.get('page')) : 1;
Expand All @@ -245,6 +283,7 @@ export const taxaManagement = (() => {
let initialSortBy = urlParams.get('o') ? (urlParams.get('o').includes('-') ? '-' : '') : '';
let initialStart = (initialPage - 1) * 20;


$('.download-button-container').show();
let tableInitialized = false;

Expand Down Expand Up @@ -363,6 +402,11 @@ export const taxaManagement = (() => {
$('#add-taxon-btn').show();
}

if (response.count === 0) {
// Find without taxon group id
findTaxaWithoutTaxonGroup(url)
}

$.each(response.results, function (index, data) {
let name = data.canonical_name || data.scientific_name;
let taxonomicStatusHTML = (data.taxonomic_status && data.taxonomic_status.toLowerCase() === 'synonym') ?
Expand Down
1 change: 0 additions & 1 deletion bims/static/js/taxa_management/taxa_sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ export const taxaSidebar = (() => {
},
error: function (data) {
console.log("error");
console.log(data);
$removeModuleBtn.html('Remove Module');
$removeModuleBtn.attr('disabled', false);
// Hide the processing modal
Expand Down
3 changes: 3 additions & 0 deletions bims/templates/taxa_management.html
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,9 @@ <h2>Taxon Management</h2>
</thead>
<tbody id="taxa-list"></tbody>
</table>
</div>
<div class="footer-warning">

</div>
<div class="pagination-centered" style="display:none">
<ul class="pagination">
Expand Down

0 comments on commit 50373a6

Please sign in to comment.