From 6ff623d6635959db8205abb1259aea814b0079e4 Mon Sep 17 00:00:00 2001 From: eastagiletracker <310448263+eastagiletracker@users.noreply.github.com> Date: Thu, 13 Aug 2026 06:16:45 +0000 Subject: [PATCH] Hide empty values on the species page Conservation status rows, and the Facts About section, were rendered as N/A when the taxon had no value for them. Build the conservation status tables in the view instead, leaving out rows without a value, tables that would have no rows, and regions that have nothing to show. --- .../site/templates/gobotany/species.html | 75 +++-------- goorchids/site/tests/__init__.py | 0 .../tests/test_species_conservation_status.py | 126 ++++++++++++++++++ goorchids/site/views.py | 61 ++++++++- 4 files changed, 206 insertions(+), 56 deletions(-) create mode 100644 goorchids/site/tests/__init__.py create mode 100644 goorchids/site/tests/test_species_conservation_status.py diff --git a/goorchids/site/templates/gobotany/species.html b/goorchids/site/templates/gobotany/species.html index efdeb73..9d1dda8 100644 --- a/goorchids/site/templates/gobotany/species.html +++ b/goorchids/site/templates/gobotany/species.html @@ -84,16 +84,14 @@

{{ partner_heading }}

{% endif %} + {% if taxon.factoid.strip %}

Facts About

- {% if taxon.factoid|length > 0 %} - {{ taxon.factoid.strip|linebreaksbr }} - {% else %} - N/A - {% endif %} + {{ taxon.factoid.strip|linebreaksbr }}

+ {% endif %} {% if taxon.pollination %}
@@ -215,73 +213,40 @@

North American Conservation Status & Distribution

Conservation Status

+ {% if conservation_status_tables %}

Select a location to view conservation status:

- + {% for table in conservation_status_tables %} +
- + + {% for row in table.rows %} - - - - - - - - - - + + + {% endfor %}
Conservation and Wetland Status{{table.heading}}
Global Rank{{taxon.get_global_rank_display|default_if_none:"N/A"}}
US Status{{taxon.get_us_status_display|default_if_none:"N/A"}}
Canadian Status{{taxon.get_ca_rank_display|default_if_none:"N/A"}}{{row.label}}{{row.value}}
- {% for conservation_status in taxon.regional_conservation_statuses.all %} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - {% endfor %} + {% endfor %} + {% else %} +

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), })