Skip to content

Commit

Permalink
Add default conservation status global
Browse files Browse the repository at this point in the history
  • Loading branch information
dimasciput committed Feb 24, 2025
1 parent c3e102a commit d8ffcbc
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions bims/migrations/0451_auto_20250224_1420.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Generated by Django 4.2.16 on 2025-02-24 14:20

from django.db import migrations
from django.db.models import Max


def add_iucn_statuses(apps, schema_editor):
IUCNStatus = apps.get_model('bims', 'IUCNStatus')

existing_max_order = IUCNStatus.objects.filter(
national=False).aggregate(Max('order'))['order__max'] or 0

# (category_value, color_code)
# The "category" field values come from IUCN_CATEGORIES in your model:
# {
# 'extinct': 'EX',
# 'extinct in the wild': 'EW',
# 'regionally extinct': 'RE',
# 'critically endangered, possibly extinct': 'CR PE',
# 'critically endangered': 'CR',
# 'endangered': 'EN',
# 'vulnerable': 'VU',
# 'near threatened': 'NT',
# 'near threatened - legacy': 'LR/nt',
# 'critically rare': 'CA',
# 'rare': 'RA',
# 'declining': 'D',
# 'data deficient': 'DD',
# 'data deficient - insufficient information': 'DDD',
# 'data deficient - taxonomically problematic': 'DDT',
# 'least concern': 'LC',
# 'least concern - legacy': 'LR/lc',
# 'conservation dependent': 'LR/cd',
# 'not evaluated': 'NE',
# }
status_data = [
('EX', '#C9CAC7'), # Extinct
('EW', '#A2A3A0'), # Extinct in the Wild
('RE', '#797A78'), # Regionally Extinct
('CR PE', '#DD456A'), # Critically Endangered – Possibly Extinct
('CR', '#641F30'), # Critically Endangered
('EN', '#8D2641'), # Endangered
('VU', '#525351'), # Vulnerable
('NT', '#3D647D'), # Near Threatened
('LR/nt', '#4D7E9D'), # Near Threatened – Legacy
('CA', '#005904'), # Critically Rare
('RA', '#009106'), # Rare
('D', '#91900D'), # Declining
('DD', '#D7CD47'), # Data Deficient
('DDD', '#D7CD47'), # Data Deficient – Insufficient Information
('DDT', '#D7CD47'), # Data Deficient – Taxonomically Problematic
('LC', '#17766B'), # Least Concern
('LR/lc', '#14655C'), # Least Concern – Legacy
('LR/cd', '#0F4D46'), # Conservation Dependent
('NE', '#39B2A3'), # Not evaluated
]

for category, colour in status_data:
# Only create if it doesn't already exist
if not IUCNStatus.objects.filter(category=category, national=False).exists():
existing_max_order += 1
IUCNStatus.objects.create(
category=category,
colour=colour,
national=False,
order=existing_max_order
)


class Migration(migrations.Migration):

dependencies = [
('bims', '0450_sitesetting_auto_validate_taxa_on_upload'),
]

operations = [
migrations.RunPython(add_iucn_statuses, migrations.RunPython.noop),
]

0 comments on commit d8ffcbc

Please sign in to comment.