Skip to content

Commit 2d97bdf

Browse files
authored
Set all the species as unvalidated when uploaded (#4411)
1 parent c7e548c commit 2d97bdf

File tree

6 files changed

+89
-11
lines changed

6 files changed

+89
-11
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 4.2.16 on 2025-01-20 12:54
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('bims', '0449_alter_biologicalcollectionrecord_uuid'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='sitesetting',
15+
name='auto_validate_taxa_on_upload',
16+
field=models.BooleanField(default=True, help_text='If True, taxa from CSV uploads are automatically validated.'),
17+
),
18+
]

bims/models/site_setting.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class SiteSetting(Preferences):
3131
'"filter_values": ["bims"]}]'
3232
)
3333

34+
auto_validate_taxa_on_upload = models.BooleanField(
35+
default=True,
36+
help_text='If True, taxa from CSV uploads are automatically validated.'
37+
)
38+
3439
default_location_site_cluster = models.CharField(
3540
max_length=100,
3641
help_text='SQL view name of the location site cluster which '

bims/scripts/taxa_upload.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,48 @@
1717
IUCNStatus,
1818
IUCN_CATEGORIES,
1919
VernacularName,
20-
ORIGIN_CATEGORIES, TaxonTag, SourceReference, SourceReferenceBibliography,
20+
ORIGIN_CATEGORIES, TaxonTag, SourceReference,
21+
SourceReferenceBibliography,
2122
Invasion
2223
)
2324
from bims.templatetags import is_fada_site
2425
from bims.utils.fetch_gbif import (
2526
fetch_all_species_from_gbif, fetch_gbif_vernacular_names
2627
)
2728
from bims.scripts.data_upload import DataCSVUpload
28-
from bims.utils.gbif import get_vernacular_names
2929
from td_biblio.exceptions import DOILoaderError
3030
from td_biblio.models import Entry
3131
from td_biblio.utils.loaders import DOILoader
3232

33+
from preferences import preferences
34+
3335
logger = logging.getLogger('bims')
3436

3537

3638
class TaxaProcessor(object):
3739

3840
all_keys = {}
3941

42+
def add_taxon_to_taxon_group(self, taxonomy: Taxonomy, taxon_group: TaxonGroup, validated = True):
43+
"""
44+
Add or update the relationship between a taxonomy and a taxon group,
45+
ensuring the 'is_validated' field is properly set in both the
46+
intermediate table (TaxonGroupTaxonomy) and on the Taxonomy object.
47+
"""
48+
taxon_group.taxonomies.add(
49+
taxonomy,
50+
through_defaults={
51+
'is_validated': validated
52+
}
53+
)
54+
55+
def add_taxon_to_taxon_group_unvalidated(self, taxonomy, taxon_group):
56+
"""
57+
A helper function that calls `add_taxon_to_taxon_group` with
58+
validated=True
59+
"""
60+
self.add_taxon_to_taxon_group(taxonomy, taxon_group, validated=True)
61+
4062
def handle_error(self, row, message):
4163
pass
4264

@@ -647,12 +669,7 @@ def process_data(self, row, taxon_group: TaxonGroup):
647669
)
648670

649671
# -- Add to taxon group
650-
taxon_group.taxonomies.add(
651-
taxonomy,
652-
through_defaults={
653-
'is_validated': True
654-
}
655-
)
672+
self.add_taxon_to_taxon_group_unvalidated(taxonomy, taxon_group)
656673

657674
# -- Endemism
658675
endemism = self.endemism(row)
@@ -746,8 +763,6 @@ def process_data(self, row, taxon_group: TaxonGroup):
746763
if accepted_taxon:
747764
taxonomy.accepted_taxonomy = accepted_taxon
748765

749-
taxonomy.validated = True
750-
751766
taxonomy.save()
752767
self.finish_processing_row(row, taxonomy)
753768
except Exception as e: # noqa
@@ -757,6 +772,14 @@ def process_data(self, row, taxon_group: TaxonGroup):
757772
class TaxaCSVUpload(DataCSVUpload, TaxaProcessor):
758773
model_name = 'taxonomy'
759774

775+
def add_taxon_to_taxon_group_unvalidated(self, taxonomy, taxon_group):
776+
"""
777+
A helper function that calls `add_taxon_to_taxon_group` with
778+
validated=False
779+
"""
780+
auto_validate = preferences.SiteSetting.auto_validate_taxa_on_upload
781+
self.add_taxon_to_taxon_group(taxonomy, taxon_group, validated=auto_validate)
782+
760783
def finish_processing_row(self, row, taxonomy):
761784
# -- Add to taxon group
762785
taxon_group = self.upload_session.module_group
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Invasion,Taxon Rank,Vernacular name(s),Language,Kingdom,Phylum,Class,Order,Family,Genus,Species,SubSpecies,Taxon,Accepted Taxon,Taxonomic status,Scientific name and authority,Common NAME,Origin,Endemism,Conservation status global,Conservation status national,lentic,On GBIF,Lakes (Y/N),Author(s),ANT,AT
2+
Category 1a invasive,Class,,,Animalia,Annelida,Oligochaeta,,,,,,Oligochaeta2,,accepted,Oligochaeta2,"Earthworm (eng), Earthworm2, ミミズ (jpn)",Native,Unknown,Not evaluated,,,No,,,,

bims/tests/test_source_references.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from unittest.mock import patch, MagicMock
55

66
from django_tenants.test.cases import FastTenantTestCase
7+
from datetime import datetime
78

89
from bims.factories import EntryFactory
910
from bims.tasks import get_source_reference_filter, generate_source_reference_filter
@@ -67,5 +68,5 @@ def test_generate_source_reference_filter(self, mock_get_current, mock_set, mock
6768
f'source_reference_filter_{self.tenant}',
6869
[
6970
{'id': self.references[0].id, 'reference': f'- | {self.entry.publication_date.year} | Test', 'type': 'Peer-reviewed scientific article'},
70-
{'id': self.references[1].id, 'reference': f'- | 2024 | {self.document.source}', 'type': 'Published book, report or thesis'}]
71+
{'id': self.references[1].id, 'reference': f'- | {datetime.now().year} | {self.document.source}', 'type': 'Published book, report or thesis'}]
7172
, timeout=None)

bims/tests/test_taxa_upload.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,35 @@ def test_taxa_upload(self, mock_fetch_all_species_from_gbif, mock_finish):
172172
).exists()
173173
)
174174

175+
@mock.patch('bims.scripts.data_upload.DataCSVUpload.finish')
176+
@mock.patch('bims.scripts.taxa_upload.fetch_all_species_from_gbif')
177+
@mock.patch('bims.scripts.taxa_upload.preferences')
178+
def test_taxa_upload_unvalidated(self, mock_preferences, mock_fetch_all_species_from_gbif, mock_finish):
179+
mock_preferences.SiteSetting.auto_validate_taxa_on_upload = False
180+
mock_finish.return_value = None
181+
mock_fetch_all_species_from_gbif.return_value = self.taxonomy
182+
183+
with open(os.path.join(
184+
test_data_directory, 'taxa_upload_family_2.csv'
185+
), 'rb') as file:
186+
upload_session = UploadSessionF.create(
187+
uploader=self.owner,
188+
process_file=File(file),
189+
module_group=self.taxon_group
190+
)
191+
192+
taxa_csv_upload = TaxaCSVUpload()
193+
taxa_csv_upload.upload_session = upload_session
194+
taxa_csv_upload.start('utf-8-sig')
195+
196+
self.assertTrue(
197+
TaxonGroupTaxonomy.objects.filter(
198+
taxonomy__canonical_name='Oligochaeta2',
199+
taxongroup=self.taxon_group,
200+
is_validated=False
201+
).exists()
202+
)
203+
175204
@mock.patch('bims.scripts.data_upload.DataCSVUpload.finish')
176205
@mock.patch('bims.scripts.taxa_upload.fetch_all_species_from_gbif')
177206
def test_taxa_upload_variety_and_forma(self, mock_fetch_all_species_from_gbif, mock_finish):

0 commit comments

Comments
 (0)