diff --git a/plants/templates/plants/identify_confirm.html b/plants/templates/plants/identify_confirm.html index 3fc8a26..39a0bae 100644 --- a/plants/templates/plants/identify_confirm.html +++ b/plants/templates/plants/identify_confirm.html @@ -1,5 +1,54 @@ {% extends "plants/base.html" %} {% block title %}Confirm species — PlantDB{% endblock %} {% block content %} -

Confirm step (coming soon)

+ +
+ +
Is this your plant?
+
+ +

Tap the correct species, then confirm.

+ +
+ {% csrf_token %} + + +
+ {% for m in matches %} +
+
+
{{ m.scientific_name }}
+ {% if m.common_names %} +
{{ m.common_names|join:", " }}
+ {% endif %} +
{{ m.family }}
+
+
+ + {% widthratio m.score 1 100 %}% + +
+
+ {% endfor %} +
+ + +
+ + {% endblock %} diff --git a/plants/tests/test_identify_views.py b/plants/tests/test_identify_views.py index 6d1146b..1792da2 100644 --- a/plants/tests/test_identify_views.py +++ b/plants/tests/test_identify_views.py @@ -73,3 +73,46 @@ def test_upload_post_plantnet_error_rerenders(client, settings): }) assert resp.status_code == 200 assert b'No match found' in resp.content + + +def _set_matches(client): + session = client.session + session['identify_matches'] = [ + {'scientific_name': 'Liatris spicata', 'common_names': ['Blazing star'], + 'score': 0.94, 'gbif_id': '123', 'family': 'Asteraceae', 'genus': 'Liatris'}, + {'scientific_name': 'Liatris pycnostachya', 'common_names': ['Prairie blazing star'], + 'score': 0.04, 'gbif_id': '456', 'family': 'Asteraceae', 'genus': 'Liatris'}, + ] + session['identify_image_paths'] = [{'path': 'plants/temp_identify/x_0.jpg', 'organ': 'flower'}] + session.save() + + +@pytest.mark.django_db +def test_confirm_get_with_session(client): + _set_matches(client) + resp = client.get(reverse('identify_confirm')) + assert resp.status_code == 200 + assert b'Liatris spicata' in resp.content + + +@pytest.mark.django_db +def test_confirm_post_stores_selection_and_redirects(client): + _set_matches(client) + with patch('plants.views.identify.vpc_search', return_value=[ + {'slug': 'liatris-spicata', 'common_name': 'Liatris spicata', 'scientific_name': 'Liatris spicata', 'thumbnail_url': '', 'source': 'vpc'}, + ]): + resp = client.post(reverse('identify_confirm'), {'match_idx': '0'}) + assert resp.status_code == 302 + assert reverse('identify_fields') in resp['Location'] + assert client.session['identify_selected']['scientific_name'] == 'Liatris spicata' + assert len(client.session['identify_vpc_results']) == 1 + + +@pytest.mark.django_db +def test_confirm_post_handles_vpc_failure(client): + _set_matches(client) + from plants.services.vpc import VPCError + with patch('plants.views.identify.vpc_search', side_effect=VPCError('down')): + resp = client.post(reverse('identify_confirm'), {'match_idx': '0'}) + assert resp.status_code == 302 + assert client.session['identify_vpc_results'] == []