No conservation status information is available for this
+ species.
+ {% endif %}
diff --git a/goorchids/site/tests/__init__.py b/goorchids/site/tests/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/goorchids/site/tests/test_species_conservation_status.py b/goorchids/site/tests/test_species_conservation_status.py
new file mode 100644
index 0000000..26b121f
--- /dev/null
+++ b/goorchids/site/tests/test_species_conservation_status.py
@@ -0,0 +1,126 @@
+from django.test import TestCase
+
+from gobotany.core import models as gobotany_models
+
+from goorchids.core.models import GoOrchidTaxon, RegionalConservationStatus
+
+
+class SpeciesPageTestCase(TestCase):
+
+ def setUp(self):
+ self.family = gobotany_models.Family.objects.create(
+ name='Orchidaceae',
+ common_name='orchid family')
+ self.genus = gobotany_models.Genus.objects.create(
+ name='Dendrophylax',
+ common_name='ghost orchid',
+ family=self.family)
+ self.taxon = GoOrchidTaxon.objects.create(
+ scientific_name='Dendrophylax lindenii',
+ family=self.family,
+ genus=self.genus,
+ taxonomic_authority='Tester',
+ ready_for_display=True)
+ pilegroup = gobotany_models.PileGroup.objects.create(
+ name='Monocots',
+ slug='monocots')
+ pile = gobotany_models.Pile.objects.create(
+ name='Orchid monocots',
+ slug='orchid-monocots',
+ pilegroup=pilegroup)
+ self.taxon.piles.add(pile)
+
+ def get_species_page(self):
+ response = self.client.get('/species/dendrophylax/lindenii/')
+ self.assertEqual(response.status_code, 200)
+ return response.content.decode('utf-8')
+
+
+class ConservationStatusDisplayTests(SpeciesPageTestCase):
+
+ def test_ranks_without_a_value_are_left_out(self):
+ self.taxon.global_rank = 'G3'
+ self.taxon.save()
+
+ page = self.get_species_page()
+
+ self.assertIn('Global Rank', page)
+ self.assertIn('Vulnerable', page)
+ self.assertNotIn('US Status', page)
+ self.assertNotIn('Canadian Status', page)
+ self.assertNotIn('N/A', page)
+
+ def test_regional_statuses_without_a_value_are_left_out(self):
+ self.taxon.global_rank = 'G3'
+ self.taxon.save()
+ RegionalConservationStatus.objects.create(
+ taxon=self.taxon,
+ region='fl',
+ status='E')
+
+ page = self.get_species_page()
+
+ self.assertIn('Conservation status for: Florida', page)
+ self.assertIn('Florida Status', page)
+ self.assertIn('Endangered', page)
+ self.assertNotIn('Florida Rank', page)
+ self.assertNotIn('
Wetland Status | ', page)
+ self.assertNotIn('N/A', page)
+
+ def test_a_species_without_any_status_shows_no_table(self):
+ page = self.get_species_page()
+
+ self.assertNotIn('
Florida', page)
+ self.assertNotIn('N/A', page)
+
+
+class FactsAboutDisplayTests(SpeciesPageTestCase):
+
+ def test_facts_about_is_left_out_when_empty(self):
+ page = self.get_species_page()
+
+ self.assertNotIn('Facts About', page)
+ self.assertNotIn('N/A', page)
+
+ def test_facts_about_is_shown_when_present(self):
+ self.taxon.factoid = 'Grows without leaves.'
+ self.taxon.save()
+
+ page = self.get_species_page()
+
+ self.assertIn('Facts About', page)
+ self.assertIn('Grows without leaves.', page)
diff --git a/goorchids/site/views.py b/goorchids/site/views.py
index 5a4327c..513a1ba 100644
--- a/goorchids/site/views.py
+++ b/goorchids/site/views.py
@@ -302,6 +302,64 @@ def _compare_character_values(a, b):
return 0 # default value (no sort)
+def _conservation_status_rows(taxon, conservation_status=None):
+ """Build the rows of one conservation status table.
+
+ Rows without a value are left out, so that the species page does not
+ display rows of "N/A" for the many taxa that have no rank or status
+ for a given jurisdiction.
+ """
+ rows = [
+ ('Global Rank', taxon.get_global_rank_display()),
+ ('US Status', taxon.get_us_status_display()),
+ ]
+ if conservation_status is not None:
+ region_name = conservation_status.get_region_display()
+ rows.append(('%s Rank' % region_name,
+ conservation_status.get_rank_display()))
+ rows.append(('%s Status' % region_name,
+ conservation_status.get_status_display()))
+ rows.append(('Canadian Status', taxon.get_ca_rank_display()))
+ if conservation_status is not None:
+ rows.append(('Wetland Status',
+ conservation_status.get_wetland_status_display()))
+
+ return [{'label': label, 'value': value} for label, value in rows
+ if value]
+
+
+def _conservation_status_tables(taxon):
+ """Build the conservation status tables for a species page.
+
+ The first table holds the statuses that apply everywhere, and is the
+ one shown before a region is selected; the rest hold the statuses for
+ a single region. A table with no rows to show is left out entirely.
+ """
+ tables = []
+
+ rows = _conservation_status_rows(taxon)
+ if rows:
+ tables.append({
+ 'region': '',
+ 'region_name': '',
+ 'heading': 'Conservation and Wetland Status',
+ 'rows': rows,
+ })
+
+ for conservation_status in taxon.regional_conservation_statuses.all():
+ rows = _conservation_status_rows(taxon, conservation_status)
+ if rows:
+ region_name = conservation_status.get_region_display()
+ tables.append({
+ 'region': conservation_status.region,
+ 'region_name': region_name,
+ 'heading': 'Conservation status for: %s' % region_name,
+ 'rows': rows,
+ })
+
+ return tables
+
+
def species_view(request, genus_slug, epithet):
COMPACT_MULTIVALUE_CHARACTERS = ['Habitat', 'New England state',
@@ -422,7 +480,8 @@ def species_view(request, genus_slug, epithet):
'brief_characteristics': preview_characters,
'all_characteristics': all_characteristics,
'epithet': epithet,
- 'native_to_north_america': native_to_north_america
+ 'native_to_north_america': native_to_north_america,
+ 'conservation_status_tables': _conservation_status_tables(taxon),
})