jrrribeiro commited on
Commit
bb1c5f8
·
verified ·
1 Parent(s): 3607d7a

Sync app from GitHub Actions (feature/project-state-security-privacy-review)

Browse files
src/ui/app_factory.py CHANGED
@@ -1822,36 +1822,6 @@ def _species_status_dropdown_script() -> str:
1822
  """
1823
 
1824
 
1825
- def _corrected_species_required_script() -> str:
1826
- """Toggle the corrected-species error style from a lightweight HTML marker."""
1827
- return """
1828
- <script>
1829
- (() => {
1830
- const INPUT_ID = "bn-corrected-species-input";
1831
- const MARKER_ID = "bn-corrected-species-error-marker";
1832
-
1833
- function markerIsActive() {
1834
- const marker = document.getElementById(MARKER_ID);
1835
- const state = marker ? marker.querySelector("[data-error]") : null;
1836
- return !!state && state.dataset.error === "true";
1837
- }
1838
-
1839
- function applyCorrectedSpeciesErrorState() {
1840
- const root = document.getElementById(INPUT_ID);
1841
- if (!root) return;
1842
- root.classList.toggle("bn-corrected-species-error", markerIsActive());
1843
- }
1844
-
1845
- const observer = new MutationObserver(applyCorrectedSpeciesErrorState);
1846
- observer.observe(document.body, { childList: true, subtree: true, characterData: true });
1847
- window.addEventListener("focus", applyCorrectedSpeciesErrorState);
1848
- document.addEventListener("click", () => window.setTimeout(applyCorrectedSpeciesErrorState, 0), true);
1849
- applyCorrectedSpeciesErrorState();
1850
- })();
1851
- </script>
1852
- """
1853
-
1854
-
1855
  def _validate_hf_admin_storage_source_dataset(
1856
  *,
1857
  project: Project,
@@ -3005,17 +2975,24 @@ def _is_reject_correction_required_status(status_value: object) -> bool:
3005
  return str(status_value or "").strip().startswith(VALIDATION_REJECT_CORRECTION_REQUIRED_STATUS)
3006
 
3007
 
3008
- def _corrected_species_error_payload(active: bool) -> str:
3009
- value = "true" if active else "false"
3010
- return f'<span class="bn-corrected-species-error-state" data-error="{value}"></span>'
 
 
 
 
 
 
 
3011
 
3012
 
3013
- def _corrected_species_ui_after_validation(save_status: object) -> tuple[object, str]:
3014
  if _is_reject_correction_required_status(save_status):
3015
- return gr.update(), _corrected_species_error_payload(True)
3016
  if str(save_status or "").startswith("Validation saved"):
3017
- return gr.update(value=None), _corrected_species_error_payload(False)
3018
- return gr.update(), _corrected_species_error_payload(False)
3019
 
3020
 
3021
  def _save_selected_validation(
@@ -3675,7 +3652,7 @@ def create_app() -> gr.Blocks:
3675
  with gr.Blocks(
3676
  title="BirdNET-Validator-App",
3677
  css=APP_CSS,
3678
- head=CRITICAL_HEAD_HTML + _species_status_dropdown_script() + _corrected_species_required_script(),
3679
  fill_width=False,
3680
  elem_classes=["bn-shell"],
3681
  ) as wrapper:
@@ -5477,9 +5454,10 @@ def create_app() -> gr.Blocks:
5477
  value=None,
5478
  elem_id="bn-corrected-species-input",
5479
  )
5480
- corrected_species_error_marker = gr.HTML(
5481
- value=_corrected_species_error_payload(False),
5482
- elem_id="bn-corrected-species-error-marker",
 
5483
  )
5484
 
5485
  status = gr.Markdown(value="", elem_classes=["bn-status-strip"])
@@ -5735,12 +5713,12 @@ def create_app() -> gr.Blocks:
5735
  only_conflicts: bool,
5736
  ):
5737
  if not project_slug:
5738
- return "Select a project before validating", cache_key, None, rows, page, idx, "", "", gr.update(), _corrected_species_error_payload(False)
5739
  validator_name_value = _validator_name_from_session(session)
5740
  if not validator_name_value:
5741
- return "Login before validating", cache_key, None, rows, page, idx, "", "", gr.update(), _corrected_species_error_payload(False)
5742
  if not _can_access_project(session, project_slug):
5743
- return "Access denied for this project", cache_key, None, rows, page, idx, "", "", gr.update(), _corrected_species_error_payload(False)
5744
  result = _save_selected_validation_with_refresh(
5745
  validation_service=validation_service,
5746
  audio_service=audio_service,
@@ -5870,7 +5848,7 @@ def create_app() -> gr.Blocks:
5870
  value = (corrected_value or "").strip()
5871
 
5872
  if not project_slug:
5873
- return gr.update(choices=base_choices, value=value or None), custom_by_project, _corrected_species_error_payload(False)
5874
 
5875
  updated = {k: list(v) for k, v in (custom_by_project or {}).items()}
5876
  custom_values = updated.get(project_slug, [])
@@ -5879,7 +5857,7 @@ def create_app() -> gr.Blocks:
5879
  updated[project_slug] = custom_values
5880
 
5881
  final_choices = list(dict.fromkeys([*base_choices, *updated.get(project_slug, [])]))
5882
- return gr.update(choices=final_choices, value=value or None), updated, _corrected_species_error_payload(False)
5883
 
5884
  def toggle_favorite_detection(
5885
  project_slug: str,
@@ -6197,7 +6175,7 @@ def create_app() -> gr.Blocks:
6197
  corrected_species_input.change(
6198
  fn=save_corrected_species_option,
6199
  inputs=[selected_project_state, corrected_species_input, project_species_state, custom_corrected_species_state],
6200
- outputs=[corrected_species_input, custom_corrected_species_state, corrected_species_error_marker],
6201
  )
6202
 
6203
  approve_event = approve_btn.click(
@@ -6244,7 +6222,7 @@ def create_app() -> gr.Blocks:
6244
  pending_status_state,
6245
  conflict_detection_key_state,
6246
  corrected_species_input,
6247
- corrected_species_error_marker,
6248
  ],
6249
  )
6250
  reject_event = reject_btn.click(
@@ -6291,7 +6269,7 @@ def create_app() -> gr.Blocks:
6291
  pending_status_state,
6292
  conflict_detection_key_state,
6293
  corrected_species_input,
6294
- corrected_species_error_marker,
6295
  ],
6296
  )
6297
  uncertain_event = uncertain_btn.click(
@@ -6338,7 +6316,7 @@ def create_app() -> gr.Blocks:
6338
  pending_status_state,
6339
  conflict_detection_key_state,
6340
  corrected_species_input,
6341
- corrected_species_error_marker,
6342
  ],
6343
  )
6344
  skip_event = skip_btn.click(
@@ -6385,7 +6363,7 @@ def create_app() -> gr.Blocks:
6385
  pending_status_state,
6386
  conflict_detection_key_state,
6387
  corrected_species_input,
6388
- corrected_species_error_marker,
6389
  ],
6390
  )
6391
 
 
1822
  """
1823
 
1824
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1825
  def _validate_hf_admin_storage_source_dataset(
1826
  *,
1827
  project: Project,
 
2975
  return str(status_value or "").strip().startswith(VALIDATION_REJECT_CORRECTION_REQUIRED_STATUS)
2976
 
2977
 
2978
+ CORRECTED_SPECIES_REQUIRED_ALERT_HTML = """
2979
+ <div class="bn-corrected-species-alert">
2980
+ <strong>Corrected species required</strong>
2981
+ <span>Choose the corrected species, Noise, or Undetermined before rejecting this segment.</span>
2982
+ </div>
2983
+ """
2984
+
2985
+
2986
+ def _corrected_species_error_update(active: bool) -> object:
2987
+ return gr.update(visible=bool(active))
2988
 
2989
 
2990
+ def _corrected_species_ui_after_validation(save_status: object) -> tuple[object, object]:
2991
  if _is_reject_correction_required_status(save_status):
2992
+ return gr.update(), _corrected_species_error_update(True)
2993
  if str(save_status or "").startswith("Validation saved"):
2994
+ return gr.update(value=None), _corrected_species_error_update(False)
2995
+ return gr.update(), _corrected_species_error_update(False)
2996
 
2997
 
2998
  def _save_selected_validation(
 
3652
  with gr.Blocks(
3653
  title="BirdNET-Validator-App",
3654
  css=APP_CSS,
3655
+ head=CRITICAL_HEAD_HTML + _species_status_dropdown_script(),
3656
  fill_width=False,
3657
  elem_classes=["bn-shell"],
3658
  ) as wrapper:
 
5454
  value=None,
5455
  elem_id="bn-corrected-species-input",
5456
  )
5457
+ corrected_species_error_panel = gr.HTML(
5458
+ value=CORRECTED_SPECIES_REQUIRED_ALERT_HTML,
5459
+ visible=False,
5460
+ elem_id="bn-corrected-species-error-panel",
5461
  )
5462
 
5463
  status = gr.Markdown(value="", elem_classes=["bn-status-strip"])
 
5713
  only_conflicts: bool,
5714
  ):
5715
  if not project_slug:
5716
+ return "Select a project before validating", cache_key, None, rows, page, idx, "", "", gr.update(), _corrected_species_error_update(False)
5717
  validator_name_value = _validator_name_from_session(session)
5718
  if not validator_name_value:
5719
+ return "Login before validating", cache_key, None, rows, page, idx, "", "", gr.update(), _corrected_species_error_update(False)
5720
  if not _can_access_project(session, project_slug):
5721
+ return "Access denied for this project", cache_key, None, rows, page, idx, "", "", gr.update(), _corrected_species_error_update(False)
5722
  result = _save_selected_validation_with_refresh(
5723
  validation_service=validation_service,
5724
  audio_service=audio_service,
 
5848
  value = (corrected_value or "").strip()
5849
 
5850
  if not project_slug:
5851
+ return gr.update(choices=base_choices, value=value or None), custom_by_project, _corrected_species_error_update(False)
5852
 
5853
  updated = {k: list(v) for k, v in (custom_by_project or {}).items()}
5854
  custom_values = updated.get(project_slug, [])
 
5857
  updated[project_slug] = custom_values
5858
 
5859
  final_choices = list(dict.fromkeys([*base_choices, *updated.get(project_slug, [])]))
5860
+ return gr.update(choices=final_choices, value=value or None), updated, _corrected_species_error_update(False)
5861
 
5862
  def toggle_favorite_detection(
5863
  project_slug: str,
 
6175
  corrected_species_input.change(
6176
  fn=save_corrected_species_option,
6177
  inputs=[selected_project_state, corrected_species_input, project_species_state, custom_corrected_species_state],
6178
+ outputs=[corrected_species_input, custom_corrected_species_state, corrected_species_error_panel],
6179
  )
6180
 
6181
  approve_event = approve_btn.click(
 
6222
  pending_status_state,
6223
  conflict_detection_key_state,
6224
  corrected_species_input,
6225
+ corrected_species_error_panel,
6226
  ],
6227
  )
6228
  reject_event = reject_btn.click(
 
6269
  pending_status_state,
6270
  conflict_detection_key_state,
6271
  corrected_species_input,
6272
+ corrected_species_error_panel,
6273
  ],
6274
  )
6275
  uncertain_event = uncertain_btn.click(
 
6316
  pending_status_state,
6317
  conflict_detection_key_state,
6318
  corrected_species_input,
6319
+ corrected_species_error_panel,
6320
  ],
6321
  )
6322
  skip_event = skip_btn.click(
 
6363
  pending_status_state,
6364
  conflict_detection_key_state,
6365
  corrected_species_input,
6366
+ corrected_species_error_panel,
6367
  ],
6368
  )
6369
 
src/ui/theme.py CHANGED
@@ -1687,28 +1687,31 @@ button.bn-report-download-action-blue:hover {
1687
  box-shadow: 0 0 0 1px rgba(22, 163, 74, 0.35) !important;
1688
  }
1689
 
1690
- #bn-corrected-species-error-marker {
1691
- display: none !important;
 
1692
  }
1693
 
1694
- #bn-corrected-species-input.bn-corrected-species-error,
1695
- #bn-corrected-species-input.bn-corrected-species-error > div,
1696
- #bn-corrected-species-input.bn-corrected-species-error .wrap,
1697
- #bn-corrected-species-input.bn-corrected-species-error .wrap-inner {
1698
- border-color: #ef4444 !important;
1699
- background: #fff5f5 !important;
1700
- box-shadow: 0 0 0 1px rgba(239, 68, 68, 0.32) !important;
 
1701
  }
1702
 
1703
- #bn-corrected-species-input.bn-corrected-species-error label,
1704
- #bn-corrected-species-input.bn-corrected-species-error .label-wrap {
1705
- color: #b42318 !important;
 
 
1706
  }
1707
 
1708
- #bn-corrected-species-input.bn-corrected-species-error input,
1709
- #bn-corrected-species-input.bn-corrected-species-error select,
1710
- #bn-corrected-species-input.bn-corrected-species-error textarea {
1711
- background: #fff5f5 !important;
1712
  }
1713
 
1714
  textarea,
 
1687
  box-shadow: 0 0 0 1px rgba(22, 163, 74, 0.35) !important;
1688
  }
1689
 
1690
+ #bn-corrected-species-error-panel {
1691
+ margin-top: -6px !important;
1692
+ margin-bottom: 8px !important;
1693
  }
1694
 
1695
+ .bn-corrected-species-alert {
1696
+ border: 1px solid #ef4444;
1697
+ border-left: 6px solid #dc2626;
1698
+ border-radius: 8px;
1699
+ background: #fff5f5;
1700
+ color: #7f1d1d;
1701
+ padding: 12px 14px;
1702
+ box-shadow: 0 10px 24px rgba(127, 29, 29, 0.08);
1703
  }
1704
 
1705
+ .bn-corrected-species-alert strong {
1706
+ display: block;
1707
+ color: #991b1b;
1708
+ font-weight: 800;
1709
+ margin-bottom: 3px;
1710
  }
1711
 
1712
+ .bn-corrected-species-alert span {
1713
+ color: #7f1d1d;
1714
+ font-size: 0.94rem;
 
1715
  }
1716
 
1717
  textarea,
tests/unit/test_app_factory_audio_helpers.py CHANGED
@@ -48,9 +48,9 @@ from src.ui.app_factory import (
48
  _species_status_dropdown_script,
49
  _species_status_payload,
50
  _species_dropdown_choices,
51
- _corrected_species_error_payload,
52
- _corrected_species_required_script,
53
  _corrected_species_ui_after_validation,
 
54
  VALIDATION_REJECT_CORRECTION_REQUIRED_STATUS,
55
  )
56
  from src.auth.auth_service import AuthService
@@ -1171,26 +1171,26 @@ def test_reject_validation_saves_corrected_species_when_provided() -> None:
1171
 
1172
 
1173
  def test_corrected_species_ui_state_marks_error_and_resets_after_save() -> None:
1174
- blocked_update, blocked_marker = _corrected_species_ui_after_validation(VALIDATION_REJECT_CORRECTION_REQUIRED_STATUS)
1175
- saved_update, saved_marker = _corrected_species_ui_after_validation("Validation saved: dkey -> negative")
1176
- conflict_update, conflict_marker = _corrected_species_ui_after_validation("Concurrency conflict: stale")
1177
 
1178
  assert isinstance(blocked_update, dict)
1179
- assert 'data-error="true"' in blocked_marker
1180
  assert saved_update["value"] is None
1181
- assert 'data-error="false"' in saved_marker
1182
  assert "value" not in conflict_update
1183
- assert 'data-error="false"' in conflict_marker
1184
 
1185
 
1186
- def test_corrected_species_error_script_targets_marker_and_input() -> None:
1187
- script = _corrected_species_required_script()
1188
- payload = _corrected_species_error_payload(True)
1189
 
1190
- assert "bn-corrected-species-input" in script
1191
- assert "bn-corrected-species-error-marker" in script
1192
- assert "bn-corrected-species-error" in script
1193
- assert 'data-error="true"' in payload
1194
 
1195
 
1196
  def test_save_selected_validation_advances_to_highest_confidence_pending_row() -> None:
 
48
  _species_status_dropdown_script,
49
  _species_status_payload,
50
  _species_dropdown_choices,
51
+ _corrected_species_error_update,
 
52
  _corrected_species_ui_after_validation,
53
+ CORRECTED_SPECIES_REQUIRED_ALERT_HTML,
54
  VALIDATION_REJECT_CORRECTION_REQUIRED_STATUS,
55
  )
56
  from src.auth.auth_service import AuthService
 
1171
 
1172
 
1173
  def test_corrected_species_ui_state_marks_error_and_resets_after_save() -> None:
1174
+ blocked_update, blocked_panel_update = _corrected_species_ui_after_validation(VALIDATION_REJECT_CORRECTION_REQUIRED_STATUS)
1175
+ saved_update, saved_panel_update = _corrected_species_ui_after_validation("Validation saved: dkey -> negative")
1176
+ conflict_update, conflict_panel_update = _corrected_species_ui_after_validation("Concurrency conflict: stale")
1177
 
1178
  assert isinstance(blocked_update, dict)
1179
+ assert blocked_panel_update["visible"] is True
1180
  assert saved_update["value"] is None
1181
+ assert saved_panel_update["visible"] is False
1182
  assert "value" not in conflict_update
1183
+ assert conflict_panel_update["visible"] is False
1184
 
1185
 
1186
+ def test_corrected_species_error_panel_uses_native_visibility_update() -> None:
1187
+ active_update = _corrected_species_error_update(True)
1188
+ inactive_update = _corrected_species_error_update(False)
1189
 
1190
+ assert active_update["visible"] is True
1191
+ assert inactive_update["visible"] is False
1192
+ assert "bn-corrected-species-alert" in CORRECTED_SPECIES_REQUIRED_ALERT_HTML
1193
+ assert "Corrected species required" in CORRECTED_SPECIES_REQUIRED_ALERT_HTML
1194
 
1195
 
1196
  def test_save_selected_validation_advances_to_highest_confidence_pending_row() -> None: