Skip to content

Commit

Permalink
Update IUCN status data (#4013)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimasciput committed Jun 25, 2024
1 parent e416f18 commit 815ddba
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 19 deletions.
6 changes: 4 additions & 2 deletions bims/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,11 @@ def save_model(self, request, obj, form, change):
obj.save()


class IUCNStatusAdmin(admin.ModelAdmin):
list_display = ('get_category_display', 'sensitive',
class IUCNStatusAdmin(OrderedModelAdmin):
list_display = ('id', 'get_category_display', 'move_up_down_links',
'order', 'sensitive',
'iucn_colour', 'national', 'total_species')
ordering = ('order',)

list_filter = (
'national',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 4.2.8 on 2024-06-24 15:25

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('bims', '0411_importtask_cancel'),
]

operations = [
migrations.AlterModelOptions(
name='iucnstatus',
options={'ordering': ['order'], 'verbose_name': 'IUCN Status', 'verbose_name_plural': 'IUCN Status'},
),
migrations.AddField(
model_name='iucnstatus',
name='order',
field=models.PositiveIntegerField(db_index=True, default=0, editable=False, verbose_name='order'),
preserve_default=False,
),
migrations.AlterField(
model_name='iucnstatus',
name='category',
field=models.CharField(blank=True, choices=[('LC', 'Least Concern'), ('NT', 'Near Threatened'), ('VU', 'Vulnerable'), ('EN', 'Endangered'), ('CR', 'Critically Endangered'), ('CR PE', 'Critically Endangered, Possibly Extinct'), ('EW', 'Extinct in the Wild'), ('EX', 'Extinct'), ('RE', 'Regionally Extinct'), ('CA', 'Critically Rare'), ('RA', 'Rare'), ('D', 'Declining'), ('DD', 'Data Deficient'), ('DDD', 'Data Deficient - Insufficient Information'), ('DDT', 'Data Deficient - Taxonomically Problematic'), ('NE', 'Not Evaluated')], default='', max_length=50),
),
]
36 changes: 23 additions & 13 deletions bims/models/iucn_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.db import models
from django.dispatch import receiver
from colorfield.fields import ColorField
from ordered_model.models import OrderedModel

SENSITIVE_STATUS = ['CR', 'EN', 'VU']
IUCN_CATEGORIES = {
Expand All @@ -14,40 +15,48 @@
'vulnerable': 'VU',
'endangered': 'EN',
'critically endangered': 'CR',
'critically endangered, possibly extinct': 'CR PE',
'extinct in the wild': 'EW',
'extinct': 'EX',
'regionally extinct': 'RE',
'critically endangered, possibly extinct': 'CE',
'critically rare': 'CA',
'rare': 'RA',
'declining': 'D',
'data deficient': 'DD',
'data deficient - insufficient information': 'DI',
'data deficient - taxonomically problematic': 'DT',
'not evaluated': 'NE'
'data deficient - insufficient information': 'DDD',
'data deficient - taxonomically problematic': 'DDT',
'not evaluated': 'NE',
'conservation dependent': 'LR/cd',
'near threatened - legacy': 'LR/nt',
'least concern - legacy': 'LR/lc',
}


class IUCNStatus(models.Model):
class IUCNStatus(OrderedModel):
"""IUCN status model."""
CATEGORY_CHOICES = (
(IUCN_CATEGORIES['least concern'], 'Least concern'),
(IUCN_CATEGORIES['near threatened'], 'Near threatened'),
(IUCN_CATEGORIES['least concern'], 'Least Concern'),
(IUCN_CATEGORIES['near threatened'], 'Near Threatened'),
(IUCN_CATEGORIES['vulnerable'], 'Vulnerable'),
(IUCN_CATEGORIES['endangered'], 'Endangered'),
(IUCN_CATEGORIES['critically endangered'], 'Critically endangered'),
(IUCN_CATEGORIES['extinct in the wild'], 'Extinct in the wild'),
(IUCN_CATEGORIES['extinct'], 'Extinct'),
(IUCN_CATEGORIES['data deficient'], 'Data deficient'),
(IUCN_CATEGORIES['not evaluated'], 'Not evaluated'),
(IUCN_CATEGORIES['regionally extinct'], 'Regionally Extinct'),
(IUCN_CATEGORIES['critically endangered'], 'Critically Endangered'),
(IUCN_CATEGORIES['critically endangered, possibly extinct'],
'Critically Endangered, Possibly Extinct'),
(IUCN_CATEGORIES['extinct in the wild'], 'Extinct in the Wild'),
(IUCN_CATEGORIES['extinct'], 'Extinct'),
(IUCN_CATEGORIES['regionally extinct'], 'Regionally Extinct'),
(IUCN_CATEGORIES['critically rare'], 'Critically Rare'),
(IUCN_CATEGORIES['rare'], 'Rare'),
(IUCN_CATEGORIES['declining'], 'Declining'),
(IUCN_CATEGORIES['data deficient'], 'Data Deficient'),
(IUCN_CATEGORIES['data deficient - insufficient information'],
'Data Deficient - Insufficient Information'),
(IUCN_CATEGORIES['data deficient - taxonomically problematic'],
'Data Deficient - Taxonomically Problematic'),
(IUCN_CATEGORIES['not evaluated'], 'Not Evaluated'),
(IUCN_CATEGORIES['conservation dependent'], 'Conservation Dependent'),
(IUCN_CATEGORIES['near threatened - legacy'], 'Near Threatened - Legacy'),
(IUCN_CATEGORIES['least concern - legacy'], 'Least Concern - Legacy'),
)

category = models.CharField(
Expand Down Expand Up @@ -83,6 +92,7 @@ class Meta:
app_label = 'bims'
verbose_name_plural = 'IUCN Status'
verbose_name = 'IUCN Status'
ordering = ['order']


@receiver(models.signals.pre_save, sender=IUCNStatus)
Expand Down
2 changes: 1 addition & 1 deletion bims/views/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def get_context_data(self, **kwargs):

# Get all the iucn conservation status
iucn_statuses = IUCNStatus.objects.all().distinct(
'category', 'national')
'category', 'national', 'order').order_by('order')
conservation_status_data = []
conservation_status_desc = {
'CR': 'A taxon is Critically Endangered when the best '
Expand Down
4 changes: 1 addition & 3 deletions deployment/docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ python manage.py migrate sites --noinput
python manage.py migrate --noinput
python manage.py gruntserver
python manage.py collectstatic --noinput
python manage.py update_site_domain
python manage.py add_default_location_site_view
python manage.py add_location_site_view_geoserver
python manage.py create_or_update_iucn_status
popd

exec "$@"
61 changes: 61 additions & 0 deletions scripts/management/commands/create_or_update_iucn_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# -*- coding: utf-8 -*-
from django.core.management.base import BaseCommand
from django.db.models import signals
from django_tenants.utils import get_tenant_model, schema_context

from bims.models import (
BiologicalCollectionRecord, collection_post_save_handler, IUCNStatus
)
from bims.utils.logger import log


class Command(BaseCommand):

def create_or_update_iucn_status(self):
categories = [
{'category': 'EX', 'national': False, 'sensitive': False, 'colour': '#606060', 'order': 0},
{'category': 'EW', 'national': False, 'sensitive': False, 'colour': '#808080', 'order': 1},
{'category': 'CR', 'national': False, 'sensitive': True, 'colour': '#B00000', 'order': 2},
{'category': 'EN', 'national': False, 'sensitive': True, 'colour': '#D00000', 'order': 3},
{'category': 'VU', 'national': False, 'sensitive': True, 'colour': '#FF0000', 'order': 4},
{'category': 'NT', 'national': False, 'sensitive': False, 'colour': '#FFC000', 'order': 5},
{'category': 'LC', 'national': False, 'sensitive': False, 'colour': '#009106', 'order': 6},
{'category': 'DD', 'national': False, 'sensitive': False, 'colour': '#808000', 'order': 7},
{'category': 'NE', 'national': False, 'sensitive': False, 'colour': '#808000', 'order': 8},

{'category': 'EX', 'national': True, 'sensitive': False, 'colour': '#606060', 'order': 9},
{'category': 'EW', 'national': True, 'sensitive': False, 'colour': '#606060', 'order': 10},
{'category': 'RE', 'national': True, 'sensitive': False, 'colour': '#404040', 'order': 11},
{'category': 'CR PE', 'national': True, 'sensitive': True, 'colour': '#A00000', 'order': 12},
{'category': 'EN', 'national': True, 'sensitive': True, 'colour': '#D00000', 'order': 13},
{'category': 'VU', 'national': True, 'sensitive': True, 'colour': '#FF0000', 'order': 14},
{'category': 'NT', 'national': True, 'sensitive': False, 'colour': '#FFC000', 'order': 15},
{'category': 'CA', 'national': True, 'sensitive': False, 'colour': '#FFC000', 'order': 16},
{'category': 'RA', 'national': True, 'sensitive': False, 'colour': '#FF8000', 'order': 17},
{'category': 'D', 'national': True, 'sensitive': False, 'colour': '#FF4000', 'order': 18},
{'category': 'DDD', 'national': True, 'sensitive': False, 'colour': '#808000', 'order': 19},
{'category': 'DDT', 'national': True, 'sensitive': False, 'colour': '#808000', 'order': 20},
{'category': 'LC', 'national': True, 'sensitive': False, 'colour': '#009106', 'order': 21},
]
for cat in categories:
iucn_status, created = IUCNStatus.objects.update_or_create(
category=cat['category'], national=cat['national'],
defaults={
'sensitive': cat['sensitive'],
'colour': cat['colour'],
'order': cat['order']
}
)
if created:
print(f'Created {cat["category"]} (national={cat["national"]})')
else:
print(f'Updated {cat["category"]} (national={cat["national"]})')

def handle(self, *args, **options):

TenantModel = get_tenant_model()
tenants = TenantModel.objects.all().exclude(schema_name='public')

for tenant in tenants:
with schema_context(tenant.schema_name):
self.create_or_update_iucn_status()

0 comments on commit 815ddba

Please sign in to comment.